Is constructor injection supported in GlassFish 3.1's implementation of CDI for managed beans? I have a @Singleton
EJB into which I want to inject another managed bean (contained in the same EJB module) using constructor injection. Field injection does work. But with constructor injection I get a NullPointerException
from AbstractSingletonContainer
.
This does work:
@Singleton
public class FooBean implements Foo {
@Inject private BarBean bar;
}
This does not work:
@Singleton
public class FooBean implements Foo {
private final BarBean bar;
@Inject
public FooBean(BarBean bar) {
this.bar = bar;
}
}
CDI does support direct field injection, initializer method parameter injection and constructor parameter injection. From the CDI 1.0 specification:
I wonder if your problem could be related to WELD-141 though.
References
Constructor injection is supported in GlassFish 3.x but you must provide a default constructor anyway to satisfy EJB specs.
This will work:
but Glassfish (this part is container dependant) will call the default constructor before the Injected one.