提交 e8a082fb 编写于 作者: J Juergen Hoeller

Clarified setAutowireCandidate semantics (plus attribute reordering in BeanDefinition)

Issue: SPR-15072
上级 ccabff6b
...@@ -79,10 +79,7 @@ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement { ...@@ -79,10 +79,7 @@ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
int ROLE_INFRASTRUCTURE = 2; int ROLE_INFRASTRUCTURE = 2;
/** // Modifiable attributes
* Return the name of the parent definition of this bean definition, if any.
*/
String getParentName();
/** /**
* Set the name of the parent definition of this bean definition, if any. * Set the name of the parent definition of this bean definition, if any.
...@@ -90,46 +87,40 @@ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement { ...@@ -90,46 +87,40 @@ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
void setParentName(String parentName); void setParentName(String parentName);
/** /**
* Return the current bean class name of this bean definition. * Return the name of the parent definition of this bean definition, if any.
* <p>Note that this does not have to be the actual class name used at runtime, in
* case of a child definition overriding/inheriting the class name from its parent.
* Hence, do <i>not</i> consider this to be the definitive bean type at runtime but
* rather only use it for parsing purposes at the individual bean definition level.
*/ */
String getBeanClassName(); String getParentName();
/** /**
* Override the bean class name of this bean definition. * Specify the bean class name of this bean definition.
* <p>The class name can be modified during bean factory post-processing, * <p>The class name can be modified during bean factory post-processing,
* typically replacing the original class name with a parsed variant of it. * typically replacing the original class name with a parsed variant of it.
* @see #setParentName
* @see #setFactoryBeanName
* @see #setFactoryMethodName
*/ */
void setBeanClassName(String beanClassName); void setBeanClassName(String beanClassName);
/** /**
* Return the factory bean name, if any. * Return the current bean class name of this bean definition.
*/ * <p>Note that this does not have to be the actual class name used at runtime, in
String getFactoryBeanName(); * case of a child definition overriding/inheriting the class name from its parent.
* Also, this may just be the class that a factory method is called on, or it may
/** * even be empty in case of a factory bean reference that a method is called on.
* Specify the factory bean to use, if any. * Hence, do <i>not</i> consider this to be the definitive bean type at runtime but
*/ * rather only use it for parsing purposes at the individual bean definition level.
void setFactoryBeanName(String factoryBeanName); * @see #getParentName()
* @see #getFactoryBeanName()
/** * @see #getFactoryMethodName()
* Return a factory method, if any.
*/ */
String getFactoryMethodName(); String getBeanClassName();
/** /**
* Specify a factory method, if any. This method will be invoked with * Override the target scope of this bean, specifying a new scope name.
* constructor arguments, or with no arguments if none are specified. * @see #SCOPE_SINGLETON
* The method will be invoked on the specified factory bean, if any, * @see #SCOPE_PROTOTYPE
* or otherwise as a static method on the local bean class.
* @param factoryMethodName static factory method name,
* or {@code null} if normal constructor creation should be used
* @see #getBeanClassName()
*/ */
void setFactoryMethodName(String factoryMethodName); void setScope(String scope);
/** /**
* Return the name of the current target scope for this bean, * Return the name of the current target scope for this bean,
...@@ -138,11 +129,11 @@ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement { ...@@ -138,11 +129,11 @@ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
String getScope(); String getScope();
/** /**
* Override the target scope of this bean, specifying a new scope name. * Set whether this bean should be lazily initialized.
* @see #SCOPE_SINGLETON * <p>If {@code false}, the bean will get instantiated on startup by bean
* @see #SCOPE_PROTOTYPE * factories that perform eager initialization of singletons.
*/ */
void setScope(String scope); void setLazyInit(boolean lazyInit);
/** /**
* Return whether this bean should be lazily initialized, i.e. not * Return whether this bean should be lazily initialized, i.e. not
...@@ -151,11 +142,10 @@ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement { ...@@ -151,11 +142,10 @@ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
boolean isLazyInit(); boolean isLazyInit();
/** /**
* Set whether this bean should be lazily initialized. * Set the names of the beans that this bean depends on being initialized.
* <p>If {@code false}, the bean will get instantiated on startup by bean * The bean factory will guarantee that these beans get initialized first.
* factories that perform eager initialization of singletons.
*/ */
void setLazyInit(boolean lazyInit); void setDependsOn(String... dependsOn);
/** /**
* Return the bean names that this bean depends on. * Return the bean names that this bean depends on.
...@@ -163,10 +153,13 @@ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement { ...@@ -163,10 +153,13 @@ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
String[] getDependsOn(); String[] getDependsOn();
/** /**
* Set the names of the beans that this bean depends on being initialized. * Set whether this bean is a candidate for getting autowired into some other bean.
* The bean factory will guarantee that these beans get initialized first. * <p>Note that this flag is designed to only affect type-based autowiring.
* It does not affect explicit references by name, which will get resolved even
* if the specified bean is not marked as an autowire candidate. As a consequence,
* autowiring by name will nevertheless inject a bean if the name matches.
*/ */
void setDependsOn(String... dependsOn); void setAutowireCandidate(boolean autowireCandidate);
/** /**
* Return whether this bean is a candidate for getting autowired into some other bean. * Return whether this bean is a candidate for getting autowired into some other bean.
...@@ -174,24 +167,43 @@ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement { ...@@ -174,24 +167,43 @@ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
boolean isAutowireCandidate(); boolean isAutowireCandidate();
/** /**
* Set whether this bean is a candidate for getting autowired into some other bean. * Set whether this bean is a primary autowire candidate.
* <p>If this value is {@code true} for exactly one bean among multiple
* matching candidates, it will serve as a tie-breaker.
*/ */
void setAutowireCandidate(boolean autowireCandidate); void setPrimary(boolean primary);
/** /**
* Return whether this bean is a primary autowire candidate. * Return whether this bean is a primary autowire candidate.
* If this value is true for exactly one bean among multiple
* matching candidates, it will serve as a tie-breaker.
*/ */
boolean isPrimary(); boolean isPrimary();
/** /**
* Set whether this bean is a primary autowire candidate. * Specify the factory bean to use, if any.
* <p>If this value is true for exactly one bean among multiple * This the name of the bean to call the specified factory method on.
* matching candidates, it will serve as a tie-breaker. * @see #setFactoryMethodName
*/ */
void setPrimary(boolean primary); void setFactoryBeanName(String factoryBeanName);
/**
* Return the factory bean name, if any.
*/
String getFactoryBeanName();
/**
* Specify a factory method, if any. This method will be invoked with
* constructor arguments, or with no arguments if none are specified.
* The method will be invoked on the specified factory bean, if any,
* or otherwise as a static method on the local bean class.
* @see #setFactoryBeanName
* @see #setBeanClassName
*/
void setFactoryMethodName(String factoryMethodName);
/**
* Return a factory method, if any.
*/
String getFactoryMethodName();
/** /**
* Return the constructor argument values for this bean. * Return the constructor argument values for this bean.
...@@ -208,6 +220,8 @@ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement { ...@@ -208,6 +220,8 @@ public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
MutablePropertyValues getPropertyValues(); MutablePropertyValues getPropertyValues();
// Read-only attributes
/** /**
* Return whether this a <b>Singleton</b>, with a single, shared instance * Return whether this a <b>Singleton</b>, with a single, shared instance
* returned on all calls. * returned on all calls.
......
...@@ -49,6 +49,7 @@ import org.springframework.util.StringUtils; ...@@ -49,6 +49,7 @@ import org.springframework.util.StringUtils;
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Rob Harrop * @author Rob Harrop
* @author Mark Fisher * @author Mark Fisher
* @see GenericBeanDefinition
* @see RootBeanDefinition * @see RootBeanDefinition
* @see ChildBeanDefinition * @see ChildBeanDefinition
*/ */
...@@ -153,25 +154,24 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -153,25 +154,24 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
private boolean primary = false; private boolean primary = false;
private final Map<String, AutowireCandidateQualifier> qualifiers = private final Map<String, AutowireCandidateQualifier> qualifiers = new LinkedHashMap<>(0);
new LinkedHashMap<>(0);
private Supplier<?> instanceSupplier;
private boolean nonPublicAccessAllowed = true; private boolean nonPublicAccessAllowed = true;
private boolean lenientConstructorResolution = true; private boolean lenientConstructorResolution = true;
private String factoryBeanName;
private String factoryMethodName;
private ConstructorArgumentValues constructorArgumentValues; private ConstructorArgumentValues constructorArgumentValues;
private MutablePropertyValues propertyValues; private MutablePropertyValues propertyValues;
private MethodOverrides methodOverrides = new MethodOverrides(); private MethodOverrides methodOverrides = new MethodOverrides();
private Supplier<?> instanceSupplier;
private String factoryBeanName;
private String factoryMethodName;
private String initMethodName; private String initMethodName;
private String destroyMethodName; private String destroyMethodName;
...@@ -213,14 +213,14 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -213,14 +213,14 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
protected AbstractBeanDefinition(BeanDefinition original) { protected AbstractBeanDefinition(BeanDefinition original) {
setParentName(original.getParentName()); setParentName(original.getParentName());
setBeanClassName(original.getBeanClassName()); setBeanClassName(original.getBeanClassName());
setFactoryBeanName(original.getFactoryBeanName());
setFactoryMethodName(original.getFactoryMethodName());
setScope(original.getScope()); setScope(original.getScope());
setAbstract(original.isAbstract()); setAbstract(original.isAbstract());
setLazyInit(original.isLazyInit()); setLazyInit(original.isLazyInit());
setRole(original.getRole()); setFactoryBeanName(original.getFactoryBeanName());
setFactoryMethodName(original.getFactoryMethodName());
setConstructorArgumentValues(new ConstructorArgumentValues(original.getConstructorArgumentValues())); setConstructorArgumentValues(new ConstructorArgumentValues(original.getConstructorArgumentValues()));
setPropertyValues(new MutablePropertyValues(original.getPropertyValues())); setPropertyValues(new MutablePropertyValues(original.getPropertyValues()));
setRole(original.getRole());
setSource(original.getSource()); setSource(original.getSource());
copyAttributesFrom(original); copyAttributesFrom(original);
...@@ -233,16 +233,16 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -233,16 +233,16 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
setDependencyCheck(originalAbd.getDependencyCheck()); setDependencyCheck(originalAbd.getDependencyCheck());
setDependsOn(originalAbd.getDependsOn()); setDependsOn(originalAbd.getDependsOn());
setAutowireCandidate(originalAbd.isAutowireCandidate()); setAutowireCandidate(originalAbd.isAutowireCandidate());
copyQualifiersFrom(originalAbd);
setPrimary(originalAbd.isPrimary()); setPrimary(originalAbd.isPrimary());
copyQualifiersFrom(originalAbd);
setInstanceSupplier(originalAbd.getInstanceSupplier());
setNonPublicAccessAllowed(originalAbd.isNonPublicAccessAllowed()); setNonPublicAccessAllowed(originalAbd.isNonPublicAccessAllowed());
setLenientConstructorResolution(originalAbd.isLenientConstructorResolution()); setLenientConstructorResolution(originalAbd.isLenientConstructorResolution());
setInstanceSupplier(originalAbd.getInstanceSupplier()); setMethodOverrides(new MethodOverrides(originalAbd.getMethodOverrides()));
setInitMethodName(originalAbd.getInitMethodName()); setInitMethodName(originalAbd.getInitMethodName());
setEnforceInitMethod(originalAbd.isEnforceInitMethod()); setEnforceInitMethod(originalAbd.isEnforceInitMethod());
setDestroyMethodName(originalAbd.getDestroyMethodName()); setDestroyMethodName(originalAbd.getDestroyMethodName());
setEnforceDestroyMethod(originalAbd.isEnforceDestroyMethod()); setEnforceDestroyMethod(originalAbd.isEnforceDestroyMethod());
setMethodOverrides(new MethodOverrides(originalAbd.getMethodOverrides()));
setSynthetic(originalAbd.isSynthetic()); setSynthetic(originalAbd.isSynthetic());
setResource(originalAbd.getResource()); setResource(originalAbd.getResource());
} }
...@@ -272,20 +272,20 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -272,20 +272,20 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
if (StringUtils.hasLength(other.getBeanClassName())) { if (StringUtils.hasLength(other.getBeanClassName())) {
setBeanClassName(other.getBeanClassName()); setBeanClassName(other.getBeanClassName());
} }
if (StringUtils.hasLength(other.getScope())) {
setScope(other.getScope());
}
setAbstract(other.isAbstract());
setLazyInit(other.isLazyInit());
if (StringUtils.hasLength(other.getFactoryBeanName())) { if (StringUtils.hasLength(other.getFactoryBeanName())) {
setFactoryBeanName(other.getFactoryBeanName()); setFactoryBeanName(other.getFactoryBeanName());
} }
if (StringUtils.hasLength(other.getFactoryMethodName())) { if (StringUtils.hasLength(other.getFactoryMethodName())) {
setFactoryMethodName(other.getFactoryMethodName()); setFactoryMethodName(other.getFactoryMethodName());
} }
if (StringUtils.hasLength(other.getScope())) {
setScope(other.getScope());
}
setAbstract(other.isAbstract());
setLazyInit(other.isLazyInit());
setRole(other.getRole());
getConstructorArgumentValues().addArgumentValues(other.getConstructorArgumentValues()); getConstructorArgumentValues().addArgumentValues(other.getConstructorArgumentValues());
getPropertyValues().addPropertyValues(other.getPropertyValues()); getPropertyValues().addPropertyValues(other.getPropertyValues());
setRole(other.getRole());
setSource(other.getSource()); setSource(other.getSource());
copyAttributesFrom(other); copyAttributesFrom(other);
...@@ -294,15 +294,16 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -294,15 +294,16 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
if (otherAbd.hasBeanClass()) { if (otherAbd.hasBeanClass()) {
setBeanClass(otherAbd.getBeanClass()); setBeanClass(otherAbd.getBeanClass());
} }
setAutowireCandidate(otherAbd.isAutowireCandidate());
setAutowireMode(otherAbd.getAutowireMode()); setAutowireMode(otherAbd.getAutowireMode());
copyQualifiersFrom(otherAbd);
setPrimary(otherAbd.isPrimary());
setDependencyCheck(otherAbd.getDependencyCheck()); setDependencyCheck(otherAbd.getDependencyCheck());
setDependsOn(otherAbd.getDependsOn()); setDependsOn(otherAbd.getDependsOn());
setAutowireCandidate(otherAbd.isAutowireCandidate());
setPrimary(otherAbd.isPrimary());
copyQualifiersFrom(otherAbd);
setInstanceSupplier(otherAbd.getInstanceSupplier());
setNonPublicAccessAllowed(otherAbd.isNonPublicAccessAllowed()); setNonPublicAccessAllowed(otherAbd.isNonPublicAccessAllowed());
setLenientConstructorResolution(otherAbd.isLenientConstructorResolution()); setLenientConstructorResolution(otherAbd.isLenientConstructorResolution());
setInstanceSupplier(otherAbd.getInstanceSupplier()); getMethodOverrides().addOverrides(otherAbd.getMethodOverrides());
if (StringUtils.hasLength(otherAbd.getInitMethodName())) { if (StringUtils.hasLength(otherAbd.getInitMethodName())) {
setInitMethodName(otherAbd.getInitMethodName()); setInitMethodName(otherAbd.getInitMethodName());
setEnforceInitMethod(otherAbd.isEnforceInitMethod()); setEnforceInitMethod(otherAbd.isEnforceInitMethod());
...@@ -311,7 +312,6 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -311,7 +312,6 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
setDestroyMethodName(otherAbd.getDestroyMethodName()); setDestroyMethodName(otherAbd.getDestroyMethodName());
setEnforceDestroyMethod(otherAbd.isEnforceDestroyMethod()); setEnforceDestroyMethod(otherAbd.isEnforceDestroyMethod());
} }
getMethodOverrides().addOverrides(otherAbd.getMethodOverrides());
setSynthetic(otherAbd.isSynthetic()); setSynthetic(otherAbd.isSynthetic());
setResource(otherAbd.getResource()); setResource(otherAbd.getResource());
} }
...@@ -336,10 +336,25 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -336,10 +336,25 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
/** /**
* Return whether this definition specifies a bean class. * Specify the bean class name of this bean definition.
*/ */
public boolean hasBeanClass() { @Override
return (this.beanClass instanceof Class); public void setBeanClassName(String beanClassName) {
this.beanClass = beanClassName;
}
/**
* Return the current bean class name of this bean definition.
*/
@Override
public String getBeanClassName() {
Object beanClassObject = this.beanClass;
if (beanClassObject instanceof Class) {
return ((Class<?>) beanClassObject).getName();
}
else {
return (String) beanClassObject;
}
} }
/** /**
...@@ -367,20 +382,11 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -367,20 +382,11 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
return (Class<?>) beanClassObject; return (Class<?>) beanClassObject;
} }
@Override /**
public void setBeanClassName(String beanClassName) { * Return whether this definition specifies a bean class.
this.beanClass = beanClassName; */
} public boolean hasBeanClass() {
return (this.beanClass instanceof Class);
@Override
public String getBeanClassName() {
Object beanClassObject = this.beanClass;
if (beanClassObject instanceof Class) {
return ((Class<?>) beanClassObject).getName();
}
else {
return (String) beanClassObject;
}
} }
/** /**
...@@ -401,7 +407,6 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -401,7 +407,6 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
return resolvedClass; return resolvedClass;
} }
/** /**
* Set the name of the target scope for the bean. * Set the name of the target scope for the bean.
* <p>The default is singleton status, although this is only applied once * <p>The default is singleton status, although this is only applied once
...@@ -483,7 +488,6 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -483,7 +488,6 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
return this.lazyInit; return this.lazyInit;
} }
/** /**
* Set the autowire mode. This determines whether any automagical detection * Set the autowire mode. This determines whether any automagical detection
* and setting of bean references will happen. Default is AUTOWIRE_NO, * and setting of bean references will happen. Default is AUTOWIRE_NO,
...@@ -574,6 +578,12 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -574,6 +578,12 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
/** /**
* Set whether this bean is a candidate for getting autowired into some other bean. * Set whether this bean is a candidate for getting autowired into some other bean.
* <p>Note that this flag is designed to only affect type-based autowiring.
* It does not affect explicit references by name, which will get resolved even
* if the specified bean is not marked as an autowire candidate. As a consequence,
* autowiring by name will nevertheless inject a bean if the name matches.
* @see #AUTOWIRE_BY_TYPE
* @see #AUTOWIRE_BY_NAME
*/ */
@Override @Override
public void setAutowireCandidate(boolean autowireCandidate) { public void setAutowireCandidate(boolean autowireCandidate) {
...@@ -590,7 +600,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -590,7 +600,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
/** /**
* Set whether this bean is a primary autowire candidate. * Set whether this bean is a primary autowire candidate.
* If this value is true for exactly one bean among multiple * <p>If this value is {@code true} for exactly one bean among multiple
* matching candidates, it will serve as a tie-breaker. * matching candidates, it will serve as a tie-breaker.
*/ */
@Override @Override
...@@ -600,8 +610,6 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -600,8 +610,6 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
/** /**
* Return whether this bean is a primary autowire candidate. * Return whether this bean is a primary autowire candidate.
* If this value is true for exactly one bean among multiple
* matching candidates, it will serve as a tie-breaker.
*/ */
@Override @Override
public boolean isPrimary() { public boolean isPrimary() {
...@@ -648,6 +656,27 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -648,6 +656,27 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
this.qualifiers.putAll(source.qualifiers); this.qualifiers.putAll(source.qualifiers);
} }
/**
* Specify a callback for creating an instance of the bean,
* as an alternative to a declaratively specified factory method.
* <p>If such a callback is set, it will override any other constructor
* or factory method metadata. However, bean property population and
* potential annotation-driven injection will still apply as usual.
* @since 5.0
* @see #setConstructorArgumentValues(ConstructorArgumentValues)
* @see #setPropertyValues(MutablePropertyValues)
*/
public void setInstanceSupplier(Supplier<?> instanceSupplier) {
this.instanceSupplier = instanceSupplier;
}
/**
* Return a callback for creating an instance of the bean, if any.
* @since 5.0
*/
public Supplier<?> getInstanceSupplier() {
return this.instanceSupplier;
}
/** /**
* Specify whether to allow access to non-public constructors and methods, * Specify whether to allow access to non-public constructors and methods,
...@@ -688,6 +717,45 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -688,6 +717,45 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
return this.lenientConstructorResolution; return this.lenientConstructorResolution;
} }
/**
* Specify the factory bean to use, if any.
* This the name of the bean to call the specified factory method on.
* @see #setFactoryMethodName
*/
@Override
public void setFactoryBeanName(String factoryBeanName) {
this.factoryBeanName = factoryBeanName;
}
/**
* Return the factory bean name, if any.
*/
@Override
public String getFactoryBeanName() {
return this.factoryBeanName;
}
/**
* Specify a factory method, if any. This method will be invoked with
* constructor arguments, or with no arguments if none are specified.
* The method will be invoked on the specified factory bean, if any,
* or otherwise as a static method on the local bean class.
* @see #setFactoryBeanName
* @see #setBeanClassName
*/
@Override
public void setFactoryMethodName(String factoryMethodName) {
this.factoryMethodName = factoryMethodName;
}
/**
* Return a factory method, if any.
*/
@Override
public String getFactoryMethodName() {
return this.factoryMethodName;
}
/** /**
* Specify constructor argument values for this bean. * Specify constructor argument values for this bean.
*/ */
...@@ -742,49 +810,6 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -742,49 +810,6 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
return this.methodOverrides; return this.methodOverrides;
} }
/**
* Specify a callback for creating an instance of the bean,
* as an alternative to a declaratively specified factory method.
* <p>If such a callback is set, it will override any other constructor
* or factory method metadata. However, bean property population and
* potential annotation-driven injection will still apply as usual.
* @since 5.0
* @see #setConstructorArgumentValues(ConstructorArgumentValues)
* @see #setPropertyValues(MutablePropertyValues)
*/
public void setInstanceSupplier(Supplier<?> instanceSupplier) {
this.instanceSupplier = instanceSupplier;
}
/**
* Return a callback for creating an instance of the bean, if any.
* @since 5.0
*/
public Supplier<?> getInstanceSupplier() {
return this.instanceSupplier;
}
@Override
public void setFactoryBeanName(String factoryBeanName) {
this.factoryBeanName = factoryBeanName;
}
@Override
public String getFactoryBeanName() {
return this.factoryBeanName;
}
@Override
public void setFactoryMethodName(String factoryMethodName) {
this.factoryMethodName = factoryMethodName;
}
@Override
public String getFactoryMethodName() {
return this.factoryMethodName;
}
/** /**
* Set the name of the initializer method. The default is {@code null} * Set the name of the initializer method. The default is {@code null}
* in which case there is no initializer method. * in which case there is no initializer method.
...@@ -849,7 +874,6 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -849,7 +874,6 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
return this.enforceDestroyMethod; return this.enforceDestroyMethod;
} }
/** /**
* Set whether this bean definition is 'synthetic', that is, not defined * Set whether this bean definition is 'synthetic', that is, not defined
* by the application itself (for example, an infrastructure bean such * by the application itself (for example, an infrastructure bean such
...@@ -882,7 +906,6 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -882,7 +906,6 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
return this.role; return this.role;
} }
/** /**
* Set a human-readable description of this bean definition. * Set a human-readable description of this bean definition.
*/ */
...@@ -890,6 +913,9 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -890,6 +913,9 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
this.description = description; this.description = description;
} }
/**
* Return a human-readable description of this bean definition.
*/
@Override @Override
public String getDescription() { public String getDescription() {
return this.description; return this.description;
...@@ -918,6 +944,10 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -918,6 +944,10 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
this.resource = new DescriptiveResource(resourceDescription); this.resource = new DescriptiveResource(resourceDescription);
} }
/**
* Return a description of the resource that this bean definition
* came from (for the purpose of showing context in case of errors).
*/
@Override @Override
public String getResourceDescription() { public String getResourceDescription() {
return (this.resource != null ? this.resource.getDescription() : null); return (this.resource != null ? this.resource.getDescription() : null);
...@@ -930,6 +960,12 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -930,6 +960,12 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
this.resource = new BeanDefinitionResource(originatingBd); this.resource = new BeanDefinitionResource(originatingBd);
} }
/**
* Return the originating BeanDefinition, or {@code null} if none.
* Allows for retrieving the decorated bean definition, if any.
* <p>Note that this method returns the immediate originator. Iterate through the
* originator chain to find the original BeanDefinition as defined by the user.
*/
@Override @Override
public BeanDefinition getOriginatingBeanDefinition() { public BeanDefinition getOriginatingBeanDefinition() {
return (this.resource instanceof BeanDefinitionResource ? return (this.resource instanceof BeanDefinitionResource ?
...@@ -1008,7 +1044,6 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess ...@@ -1008,7 +1044,6 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
*/ */
public abstract AbstractBeanDefinition cloneBeanDefinition(); public abstract AbstractBeanDefinition cloneBeanDefinition();
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
if (this == other) { if (this == other) {
......
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2016 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.
...@@ -53,10 +53,7 @@ public class ChildBeanDefinition extends AbstractBeanDefinition { ...@@ -53,10 +53,7 @@ public class ChildBeanDefinition extends AbstractBeanDefinition {
* configured through its bean properties and configuration methods. * configured through its bean properties and configuration methods.
* @param parentName the name of the parent bean * @param parentName the name of the parent bean
* @see #setBeanClass * @see #setBeanClass
* @see #setBeanClassName
* @see #setScope * @see #setScope
* @see #setAutowireMode
* @see #setDependencyCheck
* @see #setConstructorArgumentValues * @see #setConstructorArgumentValues
* @see #setPropertyValues * @see #setPropertyValues
*/ */
...@@ -174,9 +171,7 @@ public class ChildBeanDefinition extends AbstractBeanDefinition { ...@@ -174,9 +171,7 @@ public class ChildBeanDefinition extends AbstractBeanDefinition {
@Override @Override
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder("Child bean with parent '"); return "Child bean with parent '" + this.parentName + "': " + super.toString();
sb.append(this.parentName).append("': ").append(super.toString());
return sb.toString();
} }
} }
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2016 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.
...@@ -45,10 +45,7 @@ public class GenericBeanDefinition extends AbstractBeanDefinition { ...@@ -45,10 +45,7 @@ public class GenericBeanDefinition extends AbstractBeanDefinition {
* Create a new GenericBeanDefinition, to be configured through its bean * Create a new GenericBeanDefinition, to be configured through its bean
* properties and configuration methods. * properties and configuration methods.
* @see #setBeanClass * @see #setBeanClass
* @see #setBeanClassName
* @see #setScope * @see #setScope
* @see #setAutowireMode
* @see #setDependencyCheck
* @see #setConstructorArgumentValues * @see #setConstructorArgumentValues
* @see #setPropertyValues * @see #setPropertyValues
*/ */
......
...@@ -103,10 +103,7 @@ public class RootBeanDefinition extends AbstractBeanDefinition { ...@@ -103,10 +103,7 @@ public class RootBeanDefinition extends AbstractBeanDefinition {
* Create a new RootBeanDefinition, to be configured through its bean * Create a new RootBeanDefinition, to be configured through its bean
* properties and configuration methods. * properties and configuration methods.
* @see #setBeanClass * @see #setBeanClass
* @see #setBeanClassName
* @see #setScope * @see #setScope
* @see #setAutowireMode
* @see #setDependencyCheck
* @see #setConstructorArgumentValues * @see #setConstructorArgumentValues
* @see #setPropertyValues * @see #setPropertyValues
*/ */
...@@ -131,7 +128,7 @@ public class RootBeanDefinition extends AbstractBeanDefinition { ...@@ -131,7 +128,7 @@ public class RootBeanDefinition extends AbstractBeanDefinition {
* @param instanceSupplier the supplier to construct a bean instance, * @param instanceSupplier the supplier to construct a bean instance,
* as an alternative to a declaratively specified factory method * as an alternative to a declaratively specified factory method
* @since 5.0 * @since 5.0
* @see #setInstanceSupplier(Supplier) * @see #setInstanceSupplier
*/ */
public <T> RootBeanDefinition(Class<T> beanClass, Supplier<T> instanceSupplier) { public <T> RootBeanDefinition(Class<T> beanClass, Supplier<T> instanceSupplier) {
super(); super();
...@@ -147,7 +144,7 @@ public class RootBeanDefinition extends AbstractBeanDefinition { ...@@ -147,7 +144,7 @@ public class RootBeanDefinition extends AbstractBeanDefinition {
* @param instanceSupplier the supplier to construct a bean instance, * @param instanceSupplier the supplier to construct a bean instance,
* as an alternative to a declaratively specified factory method * as an alternative to a declaratively specified factory method
* @since 5.0 * @since 5.0
* @see #setInstanceSupplier(Supplier) * @see #setInstanceSupplier
*/ */
public <T> RootBeanDefinition(Class<T> beanClass, String scope, Supplier<T> instanceSupplier) { public <T> RootBeanDefinition(Class<T> beanClass, String scope, Supplier<T> instanceSupplier) {
super(); super();
......
...@@ -2002,6 +2002,14 @@ makes that specific bean definition unavailable to the autowiring infrastructure ...@@ -2002,6 +2002,14 @@ makes that specific bean definition unavailable to the autowiring infrastructure
(including annotation style configurations such as <<beans-autowired-annotation, (including annotation style configurations such as <<beans-autowired-annotation,
`@Autowired`>>). `@Autowired`>>).
[NOTE]
====
The `autowire-candidate` attribute is designed to only affect type-based autowiring.
It does not affect explicit references by name, which will get resolved even if the
specified bean is not marked as an autowire candidate. As a consequence, autowiring
by name will nevertheless inject a bean if the name matches.
====
You can also limit autowire candidates based on pattern-matching against bean names. The You can also limit autowire candidates based on pattern-matching against bean names. The
top-level `<beans/>` element accepts one or more patterns within its top-level `<beans/>` element accepts one or more patterns within its
`default-autowire-candidates` attribute. For example, to limit autowire candidate status `default-autowire-candidates` attribute. For example, to limit autowire candidate status
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册