Always use the Named key when looking up a Guice object in the GuiceAutowireCandidateResolver.
This commit is contained in:
@@ -112,16 +112,14 @@ class GuiceAutowireCandidateResolver extends ContextAnnotationAutowireCandidateR
|
||||
public Object getTarget() {
|
||||
Object target = null;
|
||||
if (this.isGuiceResolvable.isPresent() && this.isGuiceResolvable.get()) {
|
||||
target = GuiceAutowireCandidateResolver.this.injectorProvider.get()
|
||||
.getInstance(Key.get(descriptor.getResolvableType().getType()));
|
||||
target = targetGuiceObject();
|
||||
}
|
||||
else {
|
||||
try {
|
||||
target = beanFactory.doResolveDependency(descriptor, beanName, null, null);
|
||||
}
|
||||
catch (NoSuchBeanDefinitionException ex) {
|
||||
Key<?> key = guiceInstanceResolverKey();
|
||||
target = GuiceAutowireCandidateResolver.this.injectorProvider.get().getInstance(key);
|
||||
target = targetGuiceObject();
|
||||
this.isGuiceResolvable = Optional.of(true);
|
||||
}
|
||||
}
|
||||
@@ -132,6 +130,11 @@ class GuiceAutowireCandidateResolver extends ContextAnnotationAutowireCandidateR
|
||||
return target;
|
||||
}
|
||||
|
||||
private Object targetGuiceObject() {
|
||||
Key<?> key = guiceInstanceResolverKey();
|
||||
return GuiceAutowireCandidateResolver.this.injectorProvider.get().getInstance(key);
|
||||
}
|
||||
|
||||
private Key<?> guiceInstanceResolverKey() {
|
||||
Type type = descriptor.getResolvableType().getType();
|
||||
|
||||
|
||||
@@ -71,6 +71,26 @@ public class PartialInjectionTests {
|
||||
assertThat(example.getUnnamedMessage()).isEqualTo("apple");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldResolveNamedComponentOnSecondInjectWhenUsingConstructorInjection() {
|
||||
Injector injector = guiceInjectorWithSpringBean(ConstructorInjectionExample.class);
|
||||
|
||||
ConstructorInjectionExample example = injector.getInstance(ConstructorInjectionExample.class);
|
||||
|
||||
example.getNamedMessage();
|
||||
assertThat(example.getNamedMessage()).isEqualTo("banana");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldResolveNamedComponentOnSecondInjectWhenUsingSetterInjection() {
|
||||
Injector injector = guiceInjectorWithSpringBean(SetterInjectionExample.class);
|
||||
|
||||
SetterInjectionExample example = injector.getInstance(SetterInjectionExample.class);
|
||||
|
||||
example.getNamedMessage();
|
||||
assertThat(example.getNamedMessage()).isEqualTo("banana");
|
||||
}
|
||||
|
||||
private Injector guiceInjectorWithSpringBean(Class<?> classForContext) {
|
||||
Class<?>[] components = new Class<?>[] { classForContext };
|
||||
BeanFactoryProvider beanFactoryProvider = BeanFactoryProvider.from(components);
|
||||
|
||||
Reference in New Issue
Block a user