revised static annotation check
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -241,7 +241,7 @@ public interface BeanFactory {
|
||||
* @see #getBean
|
||||
* @see #isTypeMatch
|
||||
*/
|
||||
Class getType(String name) throws NoSuchBeanDefinitionException;
|
||||
Class<?> getType(String name) throws NoSuchBeanDefinitionException;
|
||||
|
||||
/**
|
||||
* Return the aliases for the given bean name, if any.
|
||||
|
||||
@@ -24,6 +24,8 @@ import java.util.Set;
|
||||
|
||||
import org.springframework.beans.SimpleTypeConverter;
|
||||
import org.springframework.beans.TypeConverter;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
||||
import org.springframework.beans.factory.config.DependencyDescriptor;
|
||||
import org.springframework.beans.factory.support.AutowireCandidateQualifier;
|
||||
@@ -47,12 +49,14 @@ import org.springframework.util.ObjectUtils;
|
||||
* @see Qualifier
|
||||
* @see Value
|
||||
*/
|
||||
public class QualifierAnnotationAutowireCandidateResolver implements AutowireCandidateResolver {
|
||||
public class QualifierAnnotationAutowireCandidateResolver implements AutowireCandidateResolver, BeanFactoryAware {
|
||||
|
||||
private final Set<Class<? extends Annotation>> qualifierTypes;
|
||||
|
||||
private Class<? extends Annotation> valueAnnotationType = Value.class;
|
||||
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new QualifierAnnotationAutowireCandidateResolver
|
||||
@@ -112,11 +116,15 @@ public class QualifierAnnotationAutowireCandidateResolver implements AutowireCan
|
||||
this.valueAnnotationType = valueAnnotationType;
|
||||
}
|
||||
|
||||
public void setBeanFactory(BeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine whether the provided bean definition is an autowire candidate.
|
||||
* <p>To be considered a candidate the bean's <em>autowire-candidate</em>
|
||||
* attribute must not have been set to 'false'. Also if an annotation on
|
||||
* attribute must not have been set to 'false'. Also, if an annotation on
|
||||
* the field or parameter to be autowired is recognized by this bean factory
|
||||
* as a <em>qualifier</em>, the bean must 'match' against the annotation as
|
||||
* well as any attributes it may contain. The bean definition must contain
|
||||
@@ -195,10 +203,18 @@ public class QualifierAnnotationAutowireCandidateResolver implements AutowireCan
|
||||
if (bd.getResolvedFactoryMethod() != null) {
|
||||
targetAnnotation = bd.getResolvedFactoryMethod().getAnnotation(type);
|
||||
}
|
||||
if (targetAnnotation == null && bd.hasBeanClass()) {
|
||||
if (targetAnnotation == null) {
|
||||
// look for matching annotation on the target class
|
||||
Class<?> beanClass = bd.getBeanClass();
|
||||
targetAnnotation = beanClass.getAnnotation(type);
|
||||
Class<?> beanType = null;
|
||||
if (this.beanFactory != null) {
|
||||
beanType = this.beanFactory.getType(bdHolder.getBeanName());
|
||||
}
|
||||
else if (bd.hasBeanClass()) {
|
||||
beanType = bd.getBeanClass();
|
||||
}
|
||||
if (beanType != null) {
|
||||
targetAnnotation = ClassUtils.getUserClass(beanType).getAnnotation(type);
|
||||
}
|
||||
}
|
||||
if (targetAnnotation != null && targetAnnotation.equals(annotation)) {
|
||||
return true;
|
||||
|
||||
@@ -473,7 +473,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
|
||||
}
|
||||
}
|
||||
|
||||
public Class getType(String name) throws NoSuchBeanDefinitionException {
|
||||
public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
|
||||
String beanName = transformedBeanName(name);
|
||||
|
||||
// Check manually registered singletons.
|
||||
|
||||
@@ -181,7 +181,7 @@ class BeanDefinitionValueResolver {
|
||||
TypedStringValue typedStringValue = (TypedStringValue) value;
|
||||
Object valueObject = evaluate(typedStringValue.getValue());
|
||||
try {
|
||||
Class resolvedTargetType = resolveTargetType(typedStringValue);
|
||||
Class<?> resolvedTargetType = resolveTargetType(typedStringValue);
|
||||
if (resolvedTargetType != null) {
|
||||
return this.typeConverter.convertIfNecessary(valueObject, resolvedTargetType);
|
||||
}
|
||||
@@ -222,7 +222,7 @@ class BeanDefinitionValueResolver {
|
||||
* @throws ClassNotFoundException if the specified type cannot be resolved
|
||||
* @see TypedStringValue#resolveTargetType
|
||||
*/
|
||||
protected Class resolveTargetType(TypedStringValue value) throws ClassNotFoundException {
|
||||
protected Class<?> resolveTargetType(TypedStringValue value) throws ClassNotFoundException {
|
||||
if (value.hasTargetType()) {
|
||||
return value.getTargetType();
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.BeanCurrentlyInCreationException;
|
||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||
import org.springframework.beans.factory.CannotLoadBeanClassException;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
@@ -180,6 +181,9 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
*/
|
||||
public void setAutowireCandidateResolver(AutowireCandidateResolver autowireCandidateResolver) {
|
||||
Assert.notNull(autowireCandidateResolver, "AutowireCandidateResolver must not be null");
|
||||
if (autowireCandidateResolver instanceof BeanFactoryAware) {
|
||||
((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(this);
|
||||
}
|
||||
this.autowireCandidateResolver = autowireCandidateResolver;
|
||||
}
|
||||
|
||||
@@ -414,16 +418,19 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
beanName = BeanFactoryUtils.transformedBeanName(beanName);
|
||||
}
|
||||
|
||||
if (!containsBeanDefinition(beanName)) {
|
||||
if (containsSingleton(beanName)) {
|
||||
return isAutowireCandidate(beanName, new RootBeanDefinition(getType(beanName)), descriptor);
|
||||
}
|
||||
else if (getParentBeanFactory() instanceof ConfigurableListableBeanFactory) {
|
||||
// No bean definition found in this factory -> delegate to parent.
|
||||
return ((ConfigurableListableBeanFactory) getParentBeanFactory()).isAutowireCandidate(beanName, descriptor);
|
||||
}
|
||||
if (containsBeanDefinition(beanName)) {
|
||||
return isAutowireCandidate(beanName, getMergedLocalBeanDefinition(beanName), descriptor);
|
||||
}
|
||||
else if (containsSingleton(beanName)) {
|
||||
return isAutowireCandidate(beanName, new RootBeanDefinition(getType(beanName)), descriptor);
|
||||
}
|
||||
else if (getParentBeanFactory() instanceof ConfigurableListableBeanFactory) {
|
||||
// No bean definition found in this factory -> delegate to parent.
|
||||
return ((ConfigurableListableBeanFactory) getParentBeanFactory()).isAutowireCandidate(beanName, descriptor);
|
||||
}
|
||||
else {
|
||||
return true;
|
||||
}
|
||||
return isAutowireCandidate(beanName, getMergedLocalBeanDefinition(beanName), descriptor);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -58,7 +58,7 @@ public class SimpleInstantiationStrategy implements InstantiationStrategy {
|
||||
throw new BeanInstantiationException(clazz, "No default constructor found", ex);
|
||||
}
|
||||
}
|
||||
return BeanUtils.instantiateClass(constructorToUse, null);
|
||||
return BeanUtils.instantiateClass(constructorToUse);
|
||||
}
|
||||
else {
|
||||
// Must generate CGLIB subclass.
|
||||
|
||||
@@ -143,7 +143,7 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
|
||||
return (targetType == null || (type != null && targetType.isAssignableFrom(type)));
|
||||
}
|
||||
|
||||
public Class getType(String name) throws NoSuchBeanDefinitionException {
|
||||
public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
|
||||
String beanName = BeanFactoryUtils.transformedBeanName(name);
|
||||
|
||||
Object bean = this.beans.get(beanName);
|
||||
|
||||
Reference in New Issue
Block a user