make bean names unique when considering binding annotations

translate binding annotations into Spring @Qualifiers
This commit is contained in:
Taylor Wicksell
2018-08-21 14:38:01 -07:00
committed by Taylor Wicksell
parent 0153079327
commit 2207c4f1d9
5 changed files with 147 additions and 5 deletions

View File

@@ -20,6 +20,8 @@ import org.springframework.guice.BindingAnnotationTests.SomeDependencyWithNamedA
import org.springframework.guice.BindingAnnotationTests.SomeDependencyWithQualifierOnProvider;
import org.springframework.guice.BindingAnnotationTests.SomeDependencyWithQualifierOnProviderWhichImplementsSomeInterface;
import org.springframework.guice.BindingAnnotationTests.SomeInterface;
import org.springframework.guice.BindingAnnotationTests.SomeNamedDepWithType1;
import org.springframework.guice.BindingAnnotationTests.SomeNamedDepWithType2;
import org.springframework.guice.annotation.EnableGuiceModules;
import com.google.inject.BindingAnnotation;
@@ -61,6 +63,12 @@ public class BindingAnnotationTests {
SomeInterface someInterface = injector.getInstance(Key.get(SomeInterface.class, SomeQualifierAnnotation.class));
assertNotNull(someInterface);
//Check different types with same @Named
assertNotNull(injector.getInstance(SomeNamedDepWithType1.class));
assertNotNull(injector.getInstance(SomeNamedDepWithType2.class));
assertNotNull(injector.getInstance(Key.get(SomeNamedDepWithType1.class, Names.named("sameNameDifferentType"))));
assertNotNull(injector.getInstance(Key.get(SomeNamedDepWithType2.class, Names.named("sameNameDifferentType"))));
context.close();
}
@@ -71,6 +79,8 @@ public class BindingAnnotationTests {
public static class SomeDependencyWithGuiceNamedAnnotationOnProvider {}
public static interface SomeInterface{}
public static class SomeDependencyWithQualifierOnProviderWhichImplementsSomeInterface implements SomeInterface {}
public static class SomeNamedDepWithType1 {}
public static class SomeNamedDepWithType2 {}
}
@Qualifier
@@ -129,4 +139,16 @@ class BindingAnnotationTestsConfig {
public SomeInterface someInterface() {
return new SomeDependencyWithQualifierOnProviderWhichImplementsSomeInterface();
}
@Bean
@Named("sameNameDifferentType")
public SomeNamedDepWithType1 someNamedDepWithType1() {
return new SomeNamedDepWithType1();
}
@Bean
@Named("sameNameDifferentType")
public SomeNamedDepWithType2 someNamedDepWithType2() {
return new SomeNamedDepWithType2();
}
}