ensure BindingAnnotations are treated as Qualifiers in Spring

This commit is contained in:
Taylor Wicksell
2019-05-03 14:55:22 -07:00
committed by Taylor Wicksell
parent 6fec10d19c
commit 0ecc98d4cb
3 changed files with 76 additions and 23 deletions

View File

@@ -55,6 +55,7 @@ import org.springframework.context.ApplicationContextException;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.guice.module.SpringModule;
@@ -140,9 +141,10 @@ class ModuleRegistryConfiguration
bean.setResourceDescription(SpringModule.SPRING_GUICE_SOURCE);
}
bean.setAttribute(SpringModule.SPRING_GUICE_SOURCE, true);
if (key.getAnnotation() != null) {
if (key.getAnnotationType() != null) {
bean.addQualifier(new AutowireCandidateQualifier(Qualifier.class,
getValueAttributeForNamed(key.getAnnotation())));
getValueAttributeForNamed(key)));
bean.addQualifier(new AutowireCandidateQualifier(key.getAnnotationType(), getValueAttributeForNamed(key)));
}
registry.registerBeanDefinition(extractName(key), bean);
}
@@ -151,7 +153,7 @@ class ModuleRegistryConfiguration
private String extractName(Key<?> key) {
final String className = key.getTypeLiteral().getType().getTypeName();
String valueAttribute = getValueAttributeForNamed(key.getAnnotation());
String valueAttribute = getValueAttributeForNamed(key);
if (valueAttribute != null) {
return valueAttribute + "_" + className;
}
@@ -160,12 +162,15 @@ class ModuleRegistryConfiguration
}
}
private String getValueAttributeForNamed(Annotation annotation) {
if (annotation instanceof Named) {
return ((Named) annotation).value();
private String getValueAttributeForNamed(Key<?> key) {
if (key.getAnnotation() instanceof Named) {
return ((Named) key.getAnnotation()).value();
}
else if (annotation instanceof javax.inject.Named) {
return ((javax.inject.Named) annotation).value();
else if (key.getAnnotation() instanceof javax.inject.Named) {
return ((javax.inject.Named) key.getAnnotation()).value();
}
else if (key.getAnnotationType() != null){
return key.getAnnotationType().getName();
}
else {
return null;

View File

@@ -15,8 +15,11 @@
*/
package org.springframework.guice.module;
import com.google.inject.BindingAnnotation;
import com.google.inject.Injector;
import com.google.inject.Key;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@@ -40,9 +43,11 @@ import java.util.Map;
class GuiceAutowireCandidateResolver extends ContextAnnotationAutowireCandidateResolver {
private Provider<Injector> injectorProvider;
private final Log logger = LogFactory.getLog(getClass());
public GuiceAutowireCandidateResolver(Provider<Injector> injectorProvider) {
this.injectorProvider = injectorProvider;
addQualifierType(BindingAnnotation.class);
}
@Override
@@ -102,13 +107,18 @@ class GuiceAutowireCandidateResolver extends ContextAnnotationAutowireCandidateR
public void releaseTarget(Object target) {
}
};
ProxyFactory pf = new ProxyFactory();
pf.setTargetSource(ts);
Class<?> dependencyType = descriptor.getDependencyType();
if (dependencyType.isInterface()) {
pf.addInterface(dependencyType);
try {
ProxyFactory pf = new ProxyFactory();
pf.setTargetSource(ts);
Class<?> dependencyType = descriptor.getDependencyType();
if (dependencyType.isInterface()) {
pf.addInterface(dependencyType);
}
return pf.getProxy(beanFactory.getBeanClassLoader());
} catch(Exception e) {
logger.debug("Failed to build lazy resolution proxy to Guice", e);
}
return pf.getProxy(beanFactory.getBeanClassLoader());
return null;
}
private boolean isCollectionType(Class<?> type) {