@Value values may use ${...} placeholders (driven by PropertyPlaceholderConfigurer); @Autowired uses field/parameter name as fallback qualifier value (SPR-5152)
This commit is contained in:
parent
2524ca33d5
commit
b85d45725d
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -181,6 +181,12 @@ public interface ConfigurableBeanFactory extends HierarchicalBeanFactory, Single
|
||||||
*/
|
*/
|
||||||
TypeConverter getTypeConverter();
|
TypeConverter getTypeConverter();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a String resolver for embedded values such as annotation attributes.
|
||||||
|
* @param valueResolver the String resolver to apply to embedded values
|
||||||
|
*/
|
||||||
|
void addEmbeddedValueResolver(StringValueResolver valueResolver);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a new BeanPostProcessor that will get applied to beans created
|
* Add a new BeanPostProcessor that will get applied to beans created
|
||||||
* by this factory. To be invoked during factory configuration.
|
* by this factory. To be invoked during factory configuration.
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -21,6 +21,7 @@ import java.lang.reflect.Field;
|
||||||
|
|
||||||
import org.springframework.core.GenericCollectionTypeResolver;
|
import org.springframework.core.GenericCollectionTypeResolver;
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
|
import org.springframework.core.ParameterNameDiscoverer;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -127,6 +128,26 @@ public class DependencyDescriptor {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize parameter name discovery for the underlying method parameter, if any.
|
||||||
|
* <p>This method does not actually try to retrieve the parameter name at
|
||||||
|
* this point; it just allows discovery to happen when the application calls
|
||||||
|
* {@link #getDependencyName()} (if ever).
|
||||||
|
*/
|
||||||
|
public void initParameterNameDiscovery(ParameterNameDiscoverer parameterNameDiscoverer) {
|
||||||
|
if (this.methodParameter != null) {
|
||||||
|
this.methodParameter.initParameterNameDiscovery(parameterNameDiscoverer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine the name of the wrapped parameter/field.
|
||||||
|
* @return the declared name (never <code>null</code>)
|
||||||
|
*/
|
||||||
|
public String getDependencyName() {
|
||||||
|
return (this.field != null ? this.field.getName() : this.methodParameter.getParameterName());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine the declared (non-generic) type of the wrapped parameter/field.
|
* Determine the declared (non-generic) type of the wrapped parameter/field.
|
||||||
* @return the declared type (never <code>null</code>)
|
* @return the declared type (never <code>null</code>)
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -273,6 +273,9 @@ public class PropertyPlaceholderConfigurer extends PropertyResourceConfigurer
|
||||||
|
|
||||||
// New in Spring 2.5: resolve placeholders in alias target names and aliases as well.
|
// New in Spring 2.5: resolve placeholders in alias target names and aliases as well.
|
||||||
beanFactoryToProcess.resolveAliases(valueResolver);
|
beanFactoryToProcess.resolveAliases(valueResolver);
|
||||||
|
|
||||||
|
// New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes.
|
||||||
|
beanFactoryToProcess.addEmbeddedValueResolver(valueResolver);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -23,6 +23,7 @@ import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -58,11 +59,13 @@ import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||||
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
|
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
|
||||||
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
|
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
|
||||||
import org.springframework.beans.factory.config.Scope;
|
import org.springframework.beans.factory.config.Scope;
|
||||||
|
import org.springframework.beans.factory.config.DependencyProxyBuilder;
|
||||||
import org.springframework.core.DecoratingClassLoader;
|
import org.springframework.core.DecoratingClassLoader;
|
||||||
import org.springframework.core.NamedThreadLocal;
|
import org.springframework.core.NamedThreadLocal;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.util.StringValueResolver;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract base class for {@link org.springframework.beans.factory.BeanFactory}
|
* Abstract base class for {@link org.springframework.beans.factory.BeanFactory}
|
||||||
|
@ -123,6 +126,9 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
|
||||||
private final Map<Class, Class<PropertyEditor>> customEditors =
|
private final Map<Class, Class<PropertyEditor>> customEditors =
|
||||||
new HashMap<Class, Class<PropertyEditor>>(4);
|
new HashMap<Class, Class<PropertyEditor>>(4);
|
||||||
|
|
||||||
|
/** String resolvers to apply e.g. to annotation attribute values */
|
||||||
|
private final List<StringValueResolver> embeddedValueResolvers = new LinkedList<StringValueResolver>();
|
||||||
|
|
||||||
/** BeanPostProcessors to apply in createBean */
|
/** BeanPostProcessors to apply in createBean */
|
||||||
private final List<BeanPostProcessor> beanPostProcessors = new ArrayList<BeanPostProcessor>();
|
private final List<BeanPostProcessor> beanPostProcessors = new ArrayList<BeanPostProcessor>();
|
||||||
|
|
||||||
|
@ -650,6 +656,24 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addEmbeddedValueResolver(StringValueResolver valueResolver) {
|
||||||
|
Assert.notNull(valueResolver, "StringValueResolver must not be null");
|
||||||
|
this.embeddedValueResolvers.add(valueResolver);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve the given embedded value, e.g. an annotation attribute.
|
||||||
|
* @param value the value to resolve
|
||||||
|
* @return the resolved value (may be the original value as-is)
|
||||||
|
*/
|
||||||
|
protected String resolveEmbeddedValue(String value) {
|
||||||
|
String result = value;
|
||||||
|
for (StringValueResolver resolver : this.embeddedValueResolvers) {
|
||||||
|
result = resolver.resolveStringValue(result);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
|
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
|
||||||
Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
|
Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
|
||||||
this.beanPostProcessors.add(beanPostProcessor);
|
this.beanPostProcessors.add(beanPostProcessor);
|
||||||
|
|
|
@ -44,6 +44,7 @@ import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
||||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
import org.springframework.beans.factory.config.DependencyDescriptor;
|
import org.springframework.beans.factory.config.DependencyDescriptor;
|
||||||
|
import org.springframework.core.ParameterNameDiscoverer;
|
||||||
import org.springframework.core.annotation.AnnotationUtils;
|
import org.springframework.core.annotation.AnnotationUtils;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
|
@ -88,6 +89,9 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||||
/** Whether to allow eager class loading even for lazy-init beans */
|
/** Whether to allow eager class loading even for lazy-init beans */
|
||||||
private boolean allowEagerClassLoading = true;
|
private boolean allowEagerClassLoading = true;
|
||||||
|
|
||||||
|
/** Resolver strategy for method parameter names */
|
||||||
|
private ParameterNameDiscoverer parameterNameDiscoverer;
|
||||||
|
|
||||||
/** Resolver to use for checking if a bean definition is an autowire candidate */
|
/** Resolver to use for checking if a bean definition is an autowire candidate */
|
||||||
private AutowireCandidateResolver autowireCandidateResolver = new SimpleAutowireCandidateResolver();
|
private AutowireCandidateResolver autowireCandidateResolver = new SimpleAutowireCandidateResolver();
|
||||||
|
|
||||||
|
@ -148,6 +152,17 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||||
this.allowEagerClassLoading = allowEagerClassLoading;
|
this.allowEagerClassLoading = allowEagerClassLoading;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the ParameterNameDiscoverer to use for resolving method parameter
|
||||||
|
* names if needed (e.g. for default qualifier values on autowired methods).
|
||||||
|
* <p>Default is none. A typical candidate is
|
||||||
|
* {@link org.springframework.core.LocalVariableTableParameterNameDiscoverer},
|
||||||
|
* which implies an ASM dependency and hence isn't set as the default.
|
||||||
|
*/
|
||||||
|
public void setParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDiscoverer) {
|
||||||
|
this.parameterNameDiscoverer = parameterNameDiscoverer;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a custom autowire candidate resolver for this BeanFactory to use
|
* Set a custom autowire candidate resolver for this BeanFactory to use
|
||||||
* when deciding whether a bean definition should be considered as a
|
* when deciding whether a bean definition should be considered as a
|
||||||
|
@ -581,12 +596,14 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||||
public Object resolveDependency(DependencyDescriptor descriptor, String beanName,
|
public Object resolveDependency(DependencyDescriptor descriptor, String beanName,
|
||||||
Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException {
|
Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException {
|
||||||
|
|
||||||
|
descriptor.initParameterNameDiscovery(this.parameterNameDiscoverer);
|
||||||
Class type = descriptor.getDependencyType();
|
Class type = descriptor.getDependencyType();
|
||||||
|
|
||||||
Object value = getAutowireCandidateResolver().getSuggestedValue(descriptor);
|
Object value = getAutowireCandidateResolver().getSuggestedValue(descriptor);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
if (value instanceof String) {
|
if (value instanceof String) {
|
||||||
value = evaluateBeanDefinitionString((String) value, getMergedBeanDefinition(beanName));
|
String strVal = resolveEmbeddedValue((String) value);
|
||||||
|
value = evaluateBeanDefinitionString(strVal, getMergedBeanDefinition(beanName));
|
||||||
}
|
}
|
||||||
return typeConverter.convertIfNecessary(value, type);
|
return typeConverter.convertIfNecessary(value, type);
|
||||||
}
|
}
|
||||||
|
@ -665,7 +682,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (matchingBeans.size() > 1) {
|
if (matchingBeans.size() > 1) {
|
||||||
String primaryBeanName = determinePrimaryCandidate(matchingBeans, type);
|
String primaryBeanName = determinePrimaryCandidate(matchingBeans, descriptor);
|
||||||
if (primaryBeanName == null) {
|
if (primaryBeanName == null) {
|
||||||
throw new NoSuchBeanDefinitionException(type,
|
throw new NoSuchBeanDefinitionException(type,
|
||||||
"expected single matching bean but found " + matchingBeans.size() + ": " + matchingBeans.keySet());
|
"expected single matching bean but found " + matchingBeans.size() + ": " + matchingBeans.keySet());
|
||||||
|
@ -730,19 +747,26 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||||
* @param type the required type
|
* @param type the required type
|
||||||
* @return the name of the primary candidate, or <code>null</code> if none found
|
* @return the name of the primary candidate, or <code>null</code> if none found
|
||||||
*/
|
*/
|
||||||
protected String determinePrimaryCandidate(Map<String, Object> candidateBeans, Class type) {
|
protected String determinePrimaryCandidate(Map<String, Object> candidateBeans, DependencyDescriptor descriptor) {
|
||||||
String primaryBeanName = null;
|
String primaryBeanName = null;
|
||||||
|
String fallbackBeanName = null;
|
||||||
for (Map.Entry<String, Object> entry : candidateBeans.entrySet()) {
|
for (Map.Entry<String, Object> entry : candidateBeans.entrySet()) {
|
||||||
String candidateBeanName = entry.getKey();
|
String candidateBeanName = entry.getKey();
|
||||||
if (isPrimary(candidateBeanName, entry.getValue())) {
|
Object beanInstance = entry.getValue();
|
||||||
|
if (isPrimary(candidateBeanName, beanInstance)) {
|
||||||
if (primaryBeanName != null) {
|
if (primaryBeanName != null) {
|
||||||
throw new NoSuchBeanDefinitionException(type,
|
throw new NoSuchBeanDefinitionException(descriptor.getDependencyType(),
|
||||||
"more than one 'primary' bean found among candidates: " + candidateBeans.keySet());
|
"more than one 'primary' bean found among candidates: " + candidateBeans.keySet());
|
||||||
}
|
}
|
||||||
primaryBeanName = candidateBeanName;
|
primaryBeanName = candidateBeanName;
|
||||||
}
|
}
|
||||||
|
if (primaryBeanName == null &&
|
||||||
|
(this.resolvableDependencies.values().contains(beanInstance) ||
|
||||||
|
matchesBeanName(candidateBeanName, descriptor.getDependencyName()))) {
|
||||||
|
fallbackBeanName = candidateBeanName;
|
||||||
}
|
}
|
||||||
return primaryBeanName;
|
}
|
||||||
|
return (primaryBeanName != null ? primaryBeanName : fallbackBeanName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -756,14 +780,20 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||||
if (containsBeanDefinition(beanName)) {
|
if (containsBeanDefinition(beanName)) {
|
||||||
return getMergedLocalBeanDefinition(beanName).isPrimary();
|
return getMergedLocalBeanDefinition(beanName).isPrimary();
|
||||||
}
|
}
|
||||||
if (this.resolvableDependencies.values().contains(beanInstance)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
BeanFactory parentFactory = getParentBeanFactory();
|
BeanFactory parentFactory = getParentBeanFactory();
|
||||||
return (parentFactory instanceof DefaultListableBeanFactory &&
|
return (parentFactory instanceof DefaultListableBeanFactory &&
|
||||||
((DefaultListableBeanFactory) parentFactory).isPrimary(beanName, beanInstance));
|
((DefaultListableBeanFactory) parentFactory).isPrimary(beanName, beanInstance));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether the given candidate name matches the bean name or the aliases
|
||||||
|
* stored in this bean definition.
|
||||||
|
*/
|
||||||
|
protected boolean matchesBeanName(String beanName, String candidateName) {
|
||||||
|
return (candidateName != null &&
|
||||||
|
(candidateName.equals(beanName) || ObjectUtils.containsElement(getAliases(beanName), candidateName)));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Raise a NoSuchBeanDefinitionException for an unresolvable dependency.
|
* Raise a NoSuchBeanDefinitionException for an unresolvable dependency.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -24,6 +24,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.ApplicationContextException;
|
import org.springframework.context.ApplicationContextException;
|
||||||
|
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for {@link org.springframework.context.ApplicationContext}
|
* Base class for {@link org.springframework.context.ApplicationContext}
|
||||||
|
@ -93,7 +94,7 @@ public abstract class AbstractRefreshableApplicationContext extends AbstractAppl
|
||||||
* @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding
|
* @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding
|
||||||
*/
|
*/
|
||||||
public void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding) {
|
public void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding) {
|
||||||
this.allowBeanDefinitionOverriding = Boolean.valueOf(allowBeanDefinitionOverriding);
|
this.allowBeanDefinitionOverriding = allowBeanDefinitionOverriding;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -104,7 +105,7 @@ public abstract class AbstractRefreshableApplicationContext extends AbstractAppl
|
||||||
* @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowCircularReferences
|
* @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowCircularReferences
|
||||||
*/
|
*/
|
||||||
public void setAllowCircularReferences(boolean allowCircularReferences) {
|
public void setAllowCircularReferences(boolean allowCircularReferences) {
|
||||||
this.allowCircularReferences = Boolean.valueOf(allowCircularReferences);
|
this.allowCircularReferences = allowCircularReferences;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -201,6 +202,7 @@ public abstract class AbstractRefreshableApplicationContext extends AbstractAppl
|
||||||
if (this.allowCircularReferences != null) {
|
if (this.allowCircularReferences != null) {
|
||||||
beanFactory.setAllowCircularReferences(this.allowCircularReferences);
|
beanFactory.setAllowCircularReferences(this.allowCircularReferences);
|
||||||
}
|
}
|
||||||
|
beanFactory.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer());
|
||||||
beanFactory.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
|
beanFactory.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -26,6 +26,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.core.io.ResourceLoader;
|
import org.springframework.core.io.ResourceLoader;
|
||||||
import org.springframework.core.io.support.ResourcePatternResolver;
|
import org.springframework.core.io.support.ResourcePatternResolver;
|
||||||
|
@ -98,6 +99,7 @@ public class GenericApplicationContext extends AbstractApplicationContext implem
|
||||||
*/
|
*/
|
||||||
public GenericApplicationContext() {
|
public GenericApplicationContext() {
|
||||||
this.beanFactory = new DefaultListableBeanFactory();
|
this.beanFactory = new DefaultListableBeanFactory();
|
||||||
|
this.beanFactory.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer());
|
||||||
this.beanFactory.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
|
this.beanFactory.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,22 +17,22 @@
|
||||||
package org.springframework.beans.factory.xml;
|
package org.springframework.beans.factory.xml;
|
||||||
|
|
||||||
import static java.lang.String.format;
|
import static java.lang.String.format;
|
||||||
import static org.junit.Assert.*;
|
|
||||||
import static org.springframework.util.ClassUtils.convertClassNameToResourcePath;
|
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
import java.lang.annotation.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanCreationException;
|
import org.springframework.beans.factory.BeanCreationException;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver;
|
import org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver;
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionReader;
|
import org.springframework.beans.factory.support.BeanDefinitionReader;
|
||||||
import org.springframework.context.support.StaticApplicationContext;
|
import org.springframework.context.support.StaticApplicationContext;
|
||||||
|
import static org.springframework.util.ClassUtils.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Mark Fisher
|
* @author Mark Fisher
|
||||||
|
@ -86,6 +86,30 @@ public final class QualifierAnnotationTests {
|
||||||
assertTrue(testBean.myProps != null && testBean.myProps.isEmpty());
|
assertTrue(testBean.myProps != null && testBean.myProps.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testQualifiedByFieldName() {
|
||||||
|
StaticApplicationContext context = new StaticApplicationContext();
|
||||||
|
BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
|
||||||
|
reader.loadBeanDefinitions(CONFIG_LOCATION);
|
||||||
|
context.registerSingleton("testBean", QualifiedByFieldNameTestBean.class);
|
||||||
|
context.refresh();
|
||||||
|
QualifiedByFieldNameTestBean testBean = (QualifiedByFieldNameTestBean) context.getBean("testBean");
|
||||||
|
Person person = testBean.getLarry();
|
||||||
|
assertEquals("LarryBean", person.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testQualifiedByParameterName() {
|
||||||
|
StaticApplicationContext context = new StaticApplicationContext();
|
||||||
|
BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
|
||||||
|
reader.loadBeanDefinitions(CONFIG_LOCATION);
|
||||||
|
context.registerSingleton("testBean", QualifiedByParameterNameTestBean.class);
|
||||||
|
context.refresh();
|
||||||
|
QualifiedByParameterNameTestBean testBean = (QualifiedByParameterNameTestBean) context.getBean("testBean");
|
||||||
|
Person person = testBean.getLarry();
|
||||||
|
assertEquals("LarryBean", person.getName());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQualifiedByAlias() {
|
public void testQualifiedByAlias() {
|
||||||
StaticApplicationContext context = new StaticApplicationContext();
|
StaticApplicationContext context = new StaticApplicationContext();
|
||||||
|
@ -203,6 +227,32 @@ public final class QualifierAnnotationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static class QualifiedByFieldNameTestBean {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Person larryBean;
|
||||||
|
|
||||||
|
public Person getLarry() {
|
||||||
|
return larryBean;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static class QualifiedByParameterNameTestBean {
|
||||||
|
|
||||||
|
private Person larryBean;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public void setLarryBean(Person larryBean) {
|
||||||
|
this.larryBean = larryBean;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Person getLarry() {
|
||||||
|
return larryBean;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private static class QualifiedByAliasTestBean {
|
private static class QualifiedByAliasTestBean {
|
||||||
|
|
||||||
@Autowired @Qualifier("stooge")
|
@Autowired @Qualifier("stooge")
|
||||||
|
|
|
@ -16,6 +16,8 @@
|
||||||
|
|
||||||
package org.springframework.context.expression;
|
package org.springframework.context.expression;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
@ -26,6 +28,7 @@ import org.springframework.beans.factory.ObjectFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
|
||||||
import org.springframework.beans.factory.config.Scope;
|
import org.springframework.beans.factory.config.Scope;
|
||||||
import org.springframework.beans.factory.support.AutowireCandidateQualifier;
|
import org.springframework.beans.factory.support.AutowireCandidateQualifier;
|
||||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||||
|
@ -70,6 +73,12 @@ public class ApplicationContextExpressionTests {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
|
||||||
|
Properties placeholders = new Properties();
|
||||||
|
placeholders.setProperty("code", "123");
|
||||||
|
ppc.setProperties(placeholders);
|
||||||
|
ac.addBeanFactoryPostProcessor(ppc);
|
||||||
|
|
||||||
GenericBeanDefinition bd0 = new GenericBeanDefinition();
|
GenericBeanDefinition bd0 = new GenericBeanDefinition();
|
||||||
bd0.setBeanClass(TestBean.class);
|
bd0.setBeanClass(TestBean.class);
|
||||||
bd0.getPropertyValues().addPropertyValue("name", "myName");
|
bd0.getPropertyValues().addPropertyValue("name", "myName");
|
||||||
|
@ -88,7 +97,7 @@ public class ApplicationContextExpressionTests {
|
||||||
bd2.setScope("myScope");
|
bd2.setScope("myScope");
|
||||||
bd2.getPropertyValues().addPropertyValue("name", "{ XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ }");
|
bd2.getPropertyValues().addPropertyValue("name", "{ XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ }");
|
||||||
bd2.getPropertyValues().addPropertyValue("age", "#{mySpecialAttr}");
|
bd2.getPropertyValues().addPropertyValue("age", "#{mySpecialAttr}");
|
||||||
bd2.getPropertyValues().addPropertyValue("country", "#{systemProperties.country}");
|
bd2.getPropertyValues().addPropertyValue("country", "${code} #{systemProperties.country}");
|
||||||
ac.registerBeanDefinition("tb2", bd2);
|
ac.registerBeanDefinition("tb2", bd2);
|
||||||
|
|
||||||
GenericBeanDefinition bd3 = new GenericBeanDefinition();
|
GenericBeanDefinition bd3 = new GenericBeanDefinition();
|
||||||
|
@ -124,30 +133,30 @@ public class ApplicationContextExpressionTests {
|
||||||
TestBean tb2 = ac.getBean("tb2", TestBean.class);
|
TestBean tb2 = ac.getBean("tb2", TestBean.class);
|
||||||
assertEquals("{ XXXmyNameYYY42ZZZ }", tb2.getName());
|
assertEquals("{ XXXmyNameYYY42ZZZ }", tb2.getName());
|
||||||
assertEquals(42, tb2.getAge());
|
assertEquals(42, tb2.getAge());
|
||||||
assertEquals("UK", tb2.getCountry());
|
assertEquals("123 UK", tb2.getCountry());
|
||||||
|
|
||||||
ValueTestBean tb3 = ac.getBean("tb3", ValueTestBean.class);
|
ValueTestBean tb3 = ac.getBean("tb3", ValueTestBean.class);
|
||||||
assertEquals("XXXmyNameYYY42ZZZ", tb3.name);
|
assertEquals("XXXmyNameYYY42ZZZ", tb3.name);
|
||||||
assertEquals(42, tb3.age);
|
assertEquals(42, tb3.age);
|
||||||
assertEquals("UK", tb3.country);
|
assertEquals("123 UK", tb3.country);
|
||||||
assertSame(tb0, tb3.tb);
|
assertSame(tb0, tb3.tb);
|
||||||
|
|
||||||
ConstructorValueTestBean tb4 = ac.getBean("tb4", ConstructorValueTestBean.class);
|
ConstructorValueTestBean tb4 = ac.getBean("tb4", ConstructorValueTestBean.class);
|
||||||
assertEquals("XXXmyNameYYY42ZZZ", tb4.name);
|
assertEquals("XXXmyNameYYY42ZZZ", tb4.name);
|
||||||
assertEquals(42, tb4.age);
|
assertEquals(42, tb4.age);
|
||||||
assertEquals("UK", tb4.country);
|
assertEquals("123 UK", tb4.country);
|
||||||
assertSame(tb0, tb4.tb);
|
assertSame(tb0, tb4.tb);
|
||||||
|
|
||||||
MethodValueTestBean tb5 = ac.getBean("tb5", MethodValueTestBean.class);
|
MethodValueTestBean tb5 = ac.getBean("tb5", MethodValueTestBean.class);
|
||||||
assertEquals("XXXmyNameYYY42ZZZ", tb5.name);
|
assertEquals("XXXmyNameYYY42ZZZ", tb5.name);
|
||||||
assertEquals(42, tb5.age);
|
assertEquals(42, tb5.age);
|
||||||
assertEquals("UK", tb5.country);
|
assertEquals("123 UK", tb5.country);
|
||||||
assertSame(tb0, tb5.tb);
|
assertSame(tb0, tb5.tb);
|
||||||
|
|
||||||
PropertyValueTestBean tb6 = ac.getBean("tb6", PropertyValueTestBean.class);
|
PropertyValueTestBean tb6 = ac.getBean("tb6", PropertyValueTestBean.class);
|
||||||
assertEquals("XXXmyNameYYY42ZZZ", tb6.name);
|
assertEquals("XXXmyNameYYY42ZZZ", tb6.name);
|
||||||
assertEquals(42, tb6.age);
|
assertEquals(42, tb6.age);
|
||||||
assertEquals("UK", tb6.country);
|
assertEquals("123 UK", tb6.country);
|
||||||
assertSame(tb0, tb6.tb);
|
assertSame(tb0, tb6.tb);
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
|
@ -197,7 +206,7 @@ public class ApplicationContextExpressionTests {
|
||||||
@Autowired @Value("#{mySpecialAttr}")
|
@Autowired @Value("#{mySpecialAttr}")
|
||||||
public int age;
|
public int age;
|
||||||
|
|
||||||
@Value("#{systemProperties.country}")
|
@Value("${code} #{systemProperties.country}")
|
||||||
public String country;
|
public String country;
|
||||||
|
|
||||||
@Qualifier("original")
|
@Qualifier("original")
|
||||||
|
@ -220,7 +229,7 @@ public class ApplicationContextExpressionTests {
|
||||||
@Value("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ") String name,
|
@Value("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ") String name,
|
||||||
@Value("#{mySpecialAttr}") int age,
|
@Value("#{mySpecialAttr}") int age,
|
||||||
@Qualifier("original") TestBean tb,
|
@Qualifier("original") TestBean tb,
|
||||||
@Value("#{systemProperties.country}") String country) {
|
@Value("${code} #{systemProperties.country}") String country) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.age = age;
|
this.age = age;
|
||||||
this.country = country;
|
this.country = country;
|
||||||
|
@ -244,7 +253,7 @@ public class ApplicationContextExpressionTests {
|
||||||
@Qualifier("original") TestBean tb,
|
@Qualifier("original") TestBean tb,
|
||||||
@Value("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ") String name,
|
@Value("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ") String name,
|
||||||
@Value("#{mySpecialAttr}") int age,
|
@Value("#{mySpecialAttr}") int age,
|
||||||
@Value("#{systemProperties.country}") String country) {
|
@Value("${code} #{systemProperties.country}") String country) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.age = age;
|
this.age = age;
|
||||||
this.country = country;
|
this.country = country;
|
||||||
|
@ -273,7 +282,7 @@ public class ApplicationContextExpressionTests {
|
||||||
this.age = age;
|
this.age = age;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Value("#{systemProperties.country}")
|
@Value("${code} #{systemProperties.country}")
|
||||||
public void setCountry(String country) {
|
public void setCountry(String country) {
|
||||||
this.country = country;
|
this.country = country;
|
||||||
}
|
}
|
||||||
|
|
|
@ -148,6 +148,13 @@ public class MethodParameter {
|
||||||
return this.constructor;
|
return this.constructor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the class that declares the underlying Method or Constructor.
|
||||||
|
*/
|
||||||
|
public Class getDeclaringClass() {
|
||||||
|
return (this.method != null ? this.method.getDeclaringClass() : this.constructor.getDeclaringClass());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the index of the method/constructor parameter.
|
* Return the index of the method/constructor parameter.
|
||||||
* @return the parameter index (never negative)
|
* @return the parameter index (never negative)
|
||||||
|
|
Loading…
Reference in New Issue