Can I use CDI constructor injection for EJBs?

Arend v. Reinersdorff picture Arend v. Reinersdorff · Feb 7, 2012 · Viewed 14.3k times · Source

I want to do something like this:

@Stateless
public class GreeterEjb {


    private final Greeter greeter;


    @Inject
    public GreeterEjb(Greeter greeter) {
        this.greeter = greeter;
    }


    public String greet() {
        return greeter.greet();
    }
}

I tried it with Glassfish 3.1.1 and JBoss 7.0.2 with mixed results. Under some circumstances it works, under other circumstances it doesn't. See this thread in the Glassfisch forum if you are interested in the details.

The EJB 3.1 spec, section 4.9.2 Bean Classes says:

The class must have a public constructor that takes no parameters.

That sounds like constructor injection is not allowed for EJBs.

BUT the CDI spec says at the start of section 3 that Session Beans are supported by CDI. Section 3.2 then talks at length about CDI and EJBs but never mentions anything about constructor injection not working. Which makes me think that it should be allowed.

So, do the specs allow CDI constructor injection for EJBs or not?

Answer

Arend v. Reinersdorff picture Arend v. Reinersdorff · Feb 8, 2012

Kris and Pete Muir have finally convinced me: The EJB must have a public no-arg constructor even if another constructor is used for injection. Weird to use two constructors at the same time, but it works. Thanks guys.

Successfully tested on Glassfish 3.1.1, JBoss 7.0.2 and TomEE 1.0.0-beta-2.

@Stateless
public class GreeterEjb {

    private final Greeter greeter;


    @Inject
    public GreeterEjb(Greeter greeter) {
        this.greeter = greeter;
    }


    // public no-arg constructor required for EJBs
    // injection still works fine with the @Inject constructor
    public GreeterEjb() {
        this.greeter = null;
    }


    public String greet() {
        return greeter.greet();
    }
}