How to dependecy inject an EJB3 constructor?
Asked Answered
T

1

2

According to EJB3 DI documentation it is possible to inject fields and setters. But how to inject a bean constructor ?

Tremml answered 12/11, 2013 at 15:4 Comment(2)
what do you mean with " to inject a bean constructor"?Handclap
A bean is a class a class has a constructor. To be more testable to class must accept its dependencies via its constructor. Can EJB resolve classes dependencies like Guice and Spring DI does ?Tremml
C
3

The EJB specification does not support constructor injection. The EJB programming model only uses the no-arg constructor, and can then perform field or setter method injection after the instance has been constructed.

That said, EJB 3.1 is part of EE 6, which includes CDI. If your EJB module is a CDI BDA (bean deployment archive) because it includes beans.xml, then you can use CDI constructor injection.

Claudication answered 14/11, 2013 at 1:46 Comment(2)
Aha I got it. But being not able to inject the constructor is very bad design !Tremml
If your design requires constructor injection, then you can use CDI. If this answered your question, please accept.Claudication

© 2022 - 2024 — McMap. All rights reserved.