3
votes

I wrote very simple web application with one stateless Ejb, the interesting moment comes when I implement interface - ejb dependency injection does not work:

web.xml

<?xml version="1.0" ?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
  <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>simpleController</servlet-name>
        <servlet-class>com.SimpleController</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>simpleController</servlet-name>
        <url-pattern>*.job</url-pattern>
    </servlet-mapping>
</web-app>

Servlet class

package com;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SimpleController extends HttpServlet {

    @EJB
    private SimpleBean bean;

    @Override
    protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().println(bean.getSomeString());
    }

    @Override
    protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}

bean

package com;

import javax.ejb.Stateless;

@Stateless
public class SimpleBean /*implements Simple*/ {
    public String getSomeString(){
        return "axaxaxa!";
    }
}

inteface

package com;

import javax.ejb.Remote;

//@Local
@Remote
public interface Simple {
    String getSomeString();
}

So when I uncoment "implements Simple" I got NullPointerException (with @Remote or @Local annotation in interface), another case I get "axaxaxa!". Why it happens? I read the Ejb class must implement intefaces.

I use TommEEcas Application Server.

2

2 Answers

5
votes

Problem solves by changing bean variable type (to interface type instead of class type)

Version with NullPointerException

private SimpleBean bean;

Working version

private Simple bean;
-1
votes

Before you troubleshoot too much, simply try restarting your container (i.e. GlassFish, JBoss, etc.).