2

I cannot get @EJB dependency injection to work. I use Linux and Maven as a build tool.

For the source code, IDE and app server I like to use the alternatives that makes it as simple as possible. I gave it a try with Glassfish 3.1.2.2, NetBeans 7.2 but no luck. I haven't done any configuration in Glassfish.

Here is some example source code, but any code that works will be helpful. Also any ideas about how to debug these kind of problems will be appreciated. It seems like every beginner is having them...

LinkResource.java

package se.xyz.webapp;
import javax.ejb.EJB;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import se.xyz.server.LinkService;

@Path("links/{username}")
public class LinkResource {
    @EJB
    LinkService service;
    @GET
    @Produces("text/plain")
    public String link(@PathParam("username") @DefaultValue("NoName") String name ) {
       return service.store(name); // Always nullpointer exception here!
   }     
}

LinkServiceImpl.java

package se.xyz.server;

import javax.ejb.Stateless;

@Stateless
public class LinkServiceImpl implements LinkService {
    public String store(String name)
    {
        return "From eJB";
    }
}

LinkService.java

package se.xyz.server;

import javax.ejb.Local;

@Local
public interface LinkService {
    public String store(String name);
}

The webapp is showing but the variable service is always null. It's not so important to get this code to work, but just if I could get any DI to work. In a distant future I like to persist too, however I would like to do it manually instead of getting a huge working app from an architype. My goal is to understand what I'm doing... Any help would be appreciated.

3 Answers 3

2

The problem is that the class in which you are trying to inject is a JAX-RS resource.

JAX-RS resources are a bit of an oversight in Java EE where it concerns the alignment of managed bean types. When Java EE 6 was created it just happened to be that JAX-RS (and JSF 2) finished early, while CDI and the overarching "managed bean" concept finished late.

JAX-RS is a container managed type of bean, but unfortunately not of the official "managed bean" variety, and it thus does not support @EJB directly.

You can make it a CDI managed bean and then use @Inject instead of @EJB.

1

If you want to inject a reference to your LinkService EJB into the LinkResource resource, then your LinkResource must be a managed component, in other words a stateless session bean. If you add a @Stateless annotation in your LinkResource class, you should be fine.

1

Context and Dependency injection needs to be enabled per project. In netbeans right click your project and choose New > Other

Under categories select Contexts and Dependency injection then select beans.xml under File types.

Then click Next and then Finish.

Or you can manually create the beans.xml file in Web Pages/WEB-INF folder. Contents should be:

<?xml version="1.0" encoding="UTF-8"?>
<beans 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/beans_1_0.xsd">
</beans>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.