Search code examples
javaxmlejbwebspherecode-injection

Ejb injection null pointer


I have a websphere application server for my project in RAD and i am treying to inject objects with ejb.Here are my files

LoginServlet

@WebServlet("/login")
public class LoginServlet extends HttpServlet {  

    @EJB UserService userService;

    public void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  

        response.setContentType("text/html");  
        PrintWriter out = response.getWriter();  

        String username=request.getParameter("username");  
        String password=request.getParameter("password");  

        String a = userService.authorize(username, password);

            if(username.equals("qwe"))
            {  
                RequestDispatcher rd=request.getRequestDispatcher("servlet2");  
                rd.forward(request, response);  
            }  
            else
            {  
                out.print("Sorry UserName or Password Error!");  
                RequestDispatcher rd=request.getRequestDispatcher("/index.html");  
                rd.include(request, response);  

            }  
        }  

    }  

UserService (also tried @ejb instead of @Inject for the dao)

@Stateless
@Interceptors(ApplicationEntityManagerInterceptor.class)
public class UserService{

    @Inject 
    protected UserDAO userDAO;


    public String authorize(String username,String password)
    {
        return this.userDAO.authenticate(username, password);   
    }

}

UserDAO

@Stateless
@Interceptors(ApplicationEntityManagerInterceptor.class)
public class UserDAO extends GenericDAOJpaImpl<User, String>
{

    public String authenticate(String username,String password)
    {
         Map<String, Object> params = new HashMap<String, Object>();
         params.put("username", username);
         params.put("password", password);          
         User user= findSingleByNamedQueryAndNamedParams(User.AUTHENTICATE, params);
         return "TRUE";
    }

}

The interceptor

public class ApplicationEntityManagerInterceptor
{
    @AroundInvoke
    public Object joinTransaction(InvocationContext context) throws Exception
    {
        Object emAux = context.getParameters()[0];
        if(emAux instanceof EntityManager){
        EntityManager em = (EntityManager) emAux;
        em.joinTransaction();
        }
        return context.proceed();
    }

}

And inside web.xml

<ejb-local-ref>
    <ejb-ref-name>gr.mess.web.servlets.LoginServlet/userService</ejb-ref-name>
    <ejb-ref-type>Session</ejb-ref-type>
    <local>gr.mess.services.UserService</local>
    <injection-target>
        <injection-target-class>gr.mess.web.servlets.LoginServlet</injection-target-class>
        <injection-target-name>userService</injection-target-name>
    </injection-target>
</ejb-local-ref>

My problem is that the userService is created normaly but the DAO object inside the service is null.What should i do.I created this based on another project but that project works normally and i dont get it.In the other project the DAO is not any xml for any kind of configuration

UPDATE the GenericDAOJpaImpl @Stateless @Interceptors(ApplicationEntityManagerInterceptor.class) public class GenericDAOJpaImpl {

protected Class<T> entityClass;

@PersistenceContext(unitName = "TEST_SQL")
protected EntityManager em;

public GenericDAOJpaImpl() {
    ParameterizedType genericSuperclass = (ParameterizedType) getClass()
         .getGenericSuperclass();
    this.entityClass = (Class<T>) genericSuperclass
         .getActualTypeArguments()[0];
}

public T create(T t) ..
public T load(PK id) ..
public T update(T t)..
public void delete(T t) ..
public T save(T t) ..
public Class<T> getEntityClass()..
public Query createNamedQuery(String name)..
@SuppressWarnings("unchecked")..
public List<T> findByNamedQuery(final String name, Object... params)..
public T findSingleByNamedQuery(final String name, Object... params)..
public List<T> findByNamedQueryAndNamedParams(final String name, final Map<String, ? extends Object> params)..
..

Update 2: I fixed my problem by doing the following creating a META-INF folder on the ear this time and adding three xml files named application.xml beans.xml and ibm-application-bnd.xml and adding the @Inject annontation above the userDAO


Solution

  • You are using CDI injection here.
    - what version of CDI/JEE/WebSphere are you using here?
    - Do you have a beans.xml file in META-INF of the jar or is CDI scanning implict (Depends on WAS/CDI version and setup) ?
    - Does "UserDAO"qualifies as a CDI bean (ie what is the GenericDAOJpaImplclass?

    You could add some tracing info in a method annoted with @PostConctruct in UserDAO to see if it is created by the CDI container