17
votes

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?

2
Whey do you want to do this? Why not the normal injection ?Santosh
Have you tried adding no-args constructor as wellKris
@Kris Yes. With an additional no-args constructor it works. Which I find very strange.Arend v. Reinersdorff
@Santosch 1) Constructor injection is possible for non-EJB classes, so I'm interested if it works for EJBs as well. 2) Open a question "Why is constructor injection better than setter or method injection?" and I'll give you more reasons ;-)Arend v. Reinersdorff
Thats the point, the spec requires you to provide no-args constructor so if you want to add other ways of constructing your bean you have to remember to satisfy the container needsKris

2 Answers

21
votes

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();
    }
}
8
votes

Constructor injection of EJBs is required in Java EE 6 ONLY IF CDI is enabled for the jar. If this not working in an appserver, file a bug.

Please also file an issue here - http://java.net/jira/browse/EJB_SPEC - to have the EJB language spec fixed (it's wrong).

This is tested in the CDITCK - https://github.com/jboss/cdi-tck/blob/master/impl/src/main/java/org/jboss/cdi/tck/tests/implementation/enterprise/definition/ExplicitConstructorSessionBean.java - but not for no-interface-views, so please raise an issue in https://issues.jboss.org/browse/CDITCK and we can add a test for your case.