dagger-servlet icon indicating copy to clipboard operation
dagger-servlet copied to clipboard

Dagger 2 support

Open husayt opened this issue 11 years ago • 3 comments

Is there any plan to support Dagger 2? Thanks

husayt avatar Dec 06 '14 03:12 husayt

Since Dagger 2 has no dynamism, I'm not even sure this will be possible. I will spend some time looking into possible way to do this. I am also open to accepting a pull request for this if you know of a way to implement it.

johnlcox avatar Dec 08 '14 15:12 johnlcox

sorry for buggin you, but ... any thoughts? thanks

bademux avatar Mar 18 '15 23:03 bademux

Since Dagger 2 removes the ability to use ObjectGraph.get(), implementing this won't be straight forward.

Based on what I've read about Dagger 2 the way this would have to work is that all servlet and jersey injectable classes would need to have the @Component annotation. Then anywhere a class is needed dynamically for the servlet framework or jersey the factory class would have to be loaded dynamically from a String.

For example if there was an OAuth servlet filter that was being injected and the servlet framework asked for the class com.example.OAuthFilter then the String com.example.Dagger_OAuthFilter would have to be built by taking the package of OAuthFilter and then appending Dagger_ followed by OAuthFilter. The code to do this might look something like the below example.

String className = // determine class name: com.example.Dagger_OAuthFilter in this case
Class clazz = Class.forName(className);
Method createMethod = clazz.getDeclaredMethod("create", null);
Object injectedObj = createMethod.invoke(null, null);

This is also complicated by the fact that this could only support Dagger generated implementations that have the create method.

johnlcox avatar Mar 28 '15 18:03 johnlcox