First of all, this is not a stubborn question, and I read most of the related questions in SO. I am looking for advice if a solution is implemented below - the correct approach / method.
I read a lot of tutorials on how to implement DI in a jersey-based web application, and most of them recommend it to create a beans.xml in WEB-INF/* to enable CDI, but I am wondering if using Jersey AbstractBinder to achieve that same result?
I have a jersey-webapp that has web.xml
<servlet> <servlet-name>Test Jersey</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.test.config.AppConfig</param-value> </init-param>
And com.test.config.AppConfig as it should
public class AppConfig extends ResourceConfig { public AppConfig() { AbstractBinder binder = new AbstractBinder() { @Override protected void configure() { bind(Impl.class).to(Interface.class).in(Singleton.class); } }; register(binder); register(MultiPartFeature.class); packages("...");
and then I annotate the interfaces and the implementation is injected
@Inject private SomeInterface someInterface;
The work is just fine. No matter what I want to be entered, I include it in the binder , and then I specify the injection point , and it is entered.
There is no beans.xml directory in the WEB-INF/ directory, and I wonder if AbstractBinder uses the AppConfig inside, which extends ResourceConfig , to eliminate the need to declare beans.xml ?
Adding beans.xml will probably allow you to scan classes that would pave the way for DI when we annotate classes using @Component or @ManagedBean .
Despite this, I would be glad to hear your feedback / recommendations / suggestions / recommendations on whether
- Stick to the existing solution (shown above) for DI in Jersey, because ....?
- Switch to annotating classes (which you must enter) and use
beans.xml annotation-discovery, because ...? - Jersey uses
HK2 by default, is it worth using a different DI container or is HK2 good enough? - What is your opinion on Jersey Spring DI versus JavaEE 6 CDI for DI purposes only?
Are there many guides that state that CDI is not supported by Tomcat? but worked above using AbstractBinder , and I assume it is because I programmatically bind? Any comments.