Guice Assisted Inject with multiple implementations of interface
See original GitHub issueFrom nishantmishra78 on June 04, 2014 10:44:53
I have an interface A with multiple implementations say B, C and D. I am using guice assisted inject to create these classes. Classes B and C use the same assister parameters(say b, c) in constructor while D has one different parameter(say d). The way I implementing this is have a factory AFactory with 2 methods: create(int b, int c) and createD(int d) In my module, I have created a PrivateModule to bind the concrete class to factory.
The code looks like this:
public static PrivateModule getAFactoryModule(final String factoryBindingKey,final Class<? extends A> targetClassToCreate) { return new PrivateModule() {
@
Override
public void configure() {
install(new FactoryModuleBuilder().implement(
A.class, targetClassToCreate).build(
AFactory.class));
bind(AFactory.class).annotatedWith(Names.named(factoryBindingKey)).to( Key.get(AFactory.class)); expose(Key.get(AFactory.class, Names.named(factoryBindingKey))); } }; } I call PrivateModule like this:
install(getAFactoryModule(“B”, B.class)); install(getAFactoryModule(“C”, C.class)); install(getAFactoryModule(“D”, D.class)); But, this gives an error saying:
com.guice.test.B has @
AssistedInject constructors but none of them match the parameters in method com.guice.test.AFactory.createD(). Unable to create assisted inject
com.guice.test.C has @
AssistedInject constructors but none of them match the parameters in method com.guice.test.AFactory.createD(). Unable to create assisted inject
com.guice.test.D has @
AssistedInject constructors but none of them match the parameters in method com.guice.test.AFactory.create(). Unable to create assisted inject
It seems Guice is trying to use different create methods than what is expected. Any idea how this can be resolved? Any pointers would be appreciated!
Thanks!
_Original issue: http://code.google.com/p/google-guice/issues/detail?id=809_
Issue Analytics
- State:
- Created 9 years ago
- Comments:8
Top GitHub Comments
From sberlin on June 04, 2014 07:58:52
When implementing the factory, assistedinject has to be able to match every method it sees. You’re installing the factory 4 times, and in each it’s saying it doesn’t know how to implement one of the methods in the factory.
Do you actually need B & C to be using the exact same method? The easiest way to do this is to do something like: interface Factory {
@
Named(“A”) Foo createA();@
Named(“B”) Foo createB(int a, int b);@
Named(“C”) Foo createC(int a, int b);@
Named(“D”) Foo createD(int a); }and then install it using:
install(new FactoryModuleBuilder() .implement(Keys.get(Foo.class, Names.named(“A”)), A.class) .implement(Keys.get(Foo.class, Names.named(“B”)), B.class) .implement(Keys.get(Foo.class, Names.named(“C”)), C.class) .implement(Keys.get(Foo.class, Names.named(“D”)), D.class) .build(AFactory.class));
… and not mess with PrivateModules at all. Doing it this way will associate each ‘create’ method with a different implementation.
From nishantmishra78 on June 05, 2014 06:05:12
Ok - thanks for your response.
I would prefer to go with approach 1, the problem I see with second approach is that it requires to unnecessarily create D along with B and C and this would grow with new implementations. Also, in case I need another implementation of A which requires different parameters than B/C or D then this approach will get messy.
If there are no better ways, I will go with option 1.
Thanks!