17

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?

Arend v. Reinersdorff
  • 4,110
  • 2
  • 36
  • 40

2 Answers2

21

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();
    }
}
Community
  • 1
  • 1
Arend v. Reinersdorff
  • 4,110
  • 2
  • 36
  • 40
  • 2
    Small tip might be to have the no-arg constructor throw an IllegalStateException since it isn't intended to actually be used. I think TomEE allows for the no-arg constructor to be omitted if you have an @Inject constructor. If it doesn't, it will shortly :) – David Blevins Feb 08 '12 at 20:54
  • For TomEE that would be "shortly" ;-) And wouldn't it break the current EJB spec to not require a no-arg constructor? – Arend v. Reinersdorff Feb 09 '12 at 06:42
  • 9
    Yes, I also thought about throwing an exception in the constructor. Turns out the constructor is really called and throwing an exception prevents instantiating the EJB. – Arend v. Reinersdorff Feb 09 '12 at 06:49
  • I think JBoss AS will allow you to omit the no-arg constructor as well. If it dosen't, it should, so tell me and I'll get it fixed. – Pete Muir Feb 14 '12 at 20:33
  • @Pete Muir JBoss 7.0.2 also requires a no-arg constructor, at least for the no-interface view. You can try my example without the no-arg constructor. – Arend v. Reinersdorff Feb 15 '12 at 09:38
  • @vdenotaris Not sure I understand you correctly. The idea is to not pass a Greeter instance manually but to let the CDI container inject it via the `@Inject` constructor. – Arend v. Reinersdorff Jun 05 '14 at 15:24
  • Wildfly calls no-arg constructor once and injected constructor for each call. So if you make three calls, 4 objects will be instantiated: 1 no-arg and 3 injected. – Željko Trogrlić Apr 05 '17 at 13:34
  • @ŽeljkoTrogrlić Constructors should be called on EJB and proxy creation, not on later method calls. I tried it on Wildfly 10.1.0 and noticed two calls of the no-arg constructor. But it was the same without constructor injection. – Arend v. Reinersdorff Apr 11 '17 at 20:41
  • Check the answer on this question https://stackoverflow.com/questions/19932624/how-to-dependecy-inject-an-ejb3-constructor for an extra bit of information. – Jefferson Lima Jul 28 '17 at 20:58
8

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.

Pete Muir
  • 232
  • 1
  • 3
  • Thanks a lot. That's exactly the answer I was looking for :-) Follow-up question: Why the no-arg constructor in ExplicitConstructorSessionBean? If I add a no-arg constructor to my no-interface-view EJB then constructor injection works. Is a no-arg constructor somehow required even if the @Inject constructor is used? – Arend v. Reinersdorff Feb 07 '12 at 19:37
  • 1
    The no-arg constructor is a requirement of the EJB specification. The two specifications aren't as closely aligned as they could be. – Brett Kail Feb 07 '12 at 23:30
  • 1
    OK, then I'm just missing the no-arg constructor and it's not a bug. Thanks. Posted this as an answer. – Arend v. Reinersdorff Feb 08 '12 at 11:23
  • @Pete Great to see you on stackoverflow :) – David Blevins Feb 08 '12 at 19:57