Reputation: 370
I have a problem with Guice field injection. Consider following hierarchy of classes.
abstract class Base{
@Inject
protected MyService myService;
}
class A extends Base{
@Inject
private AnotherService anotherService;
}
class B extends Base{
...
}
I want to have two different instances of MyService at runtime - one to inject in all objects of class A and the other for objects of class B. I know how to achieve that behaviour with constructor injecton:
bind(MyService.class).annotatedWith(Names.named("forA")).to(MyServiceImpl.class).in(Singleton.class);
bind(MyService.class).annotatedWith(Names.named("forB")).to(MyServiceImpl.class).in(Singleton.class);
class A extends Base{
@Inject @Named(value = "forA")
public A(MyService service1, AnotherService service2) {
this.myService = service1;
this.anotherService = service2;
}
The problem is that migration to constructor injection will be pretty complicated, so I would like to stick with the field injection. Is it possible to tune field injection like I want?
Upvotes: 0
Views: 653
Reputation: 3737
I would use Custom Injections from Guice
to achieve this.
Firstly I will create an annotation to annotate the MyService which I want to be injected based on super class type.
@BindingAnnotation
@interface MyServiceInject {
}
Then I would annotate my MyService field with the annotation.
static abstract class Base {
@MyServiceInject
protected MyService myService;
}
Now we need a custom TypeListener
which will be called everytime we encounter an injection point.
This is how we will create it.
static class ClassBasedMyServiceInjectionListener implements TypeListener {
@Override
public <I> void hear(TypeLiteral<I> typeLiteral, TypeEncounter<I> encounter) {
Class<?> clazz = typeLiteral.getRawType();
while (clazz != null) {
for (Field field : clazz.getDeclaredFields()) {
if (field.getType() == MyService.class && field.isAnnotationPresent(MyServiceInject.class)) { //if type of field is MyService and it has MyServiceInject annotation
encounter.register(new ClassBasedMyServiceInjector<>(field,
typeLiteral.getType(),
encounter.getProvider(MyServiceA.class),
encounter.getProvider(MyServiceB.class)
)
); //Now register a MemberInjector for this encounter.
}
}
clazz = clazz.getSuperclass();
}
}
}
Now we need the MemberInjector which will look like this.
static class ClassBasedMyServiceInjector<T> implements MembersInjector<T> {
private final Field field;
private final Type superClassType;
private final Provider<MyServiceA> myServiceAProvider;
private final Provider<MyServiceB> myServiceBProvider;
ClassBasedMyServiceInjector(Field field, Type superClassType, Provider<MyServiceA> myServiceAProvider, Provider<MyServiceB> myServiceBProvider) {
this.field = field;
this.superClassType = superClassType;
this.myServiceAProvider = myServiceAProvider;
this.myServiceBProvider = myServiceBProvider;
field.setAccessible(true);
}
public void injectMembers(T t) { //this will be called when guice wants to inject members
try {
if (superClassType == A.class) {//if super class is of type A
field.set(t, myServiceAProvider.get()); //inject MyServiceA
} else if (superClassType == B.class) { //if super class is of type B
field.set(t, myServiceBProvider.get()); //inject MyServiceB
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
Finally I would bind our custom TypeListener
in our module's configure method like this.
@Override
protected void configure() {
bindListener(Matchers.any(), new ClassBasedMyServiceInjectionListener());
}
Hope this helps.
Upvotes: 1