Introduce PREFERRED_CONSTRUCTORS_ATTRIBUTE on AbstractBeanDefinition
Closes gh-30917
This commit is contained in:
@@ -1942,6 +1942,10 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
|
||||
@Override
|
||||
@Nullable
|
||||
public Constructor<?>[] getPreferredConstructors() {
|
||||
Constructor<?>[] fromAttribute = super.getPreferredConstructors();
|
||||
if (fromAttribute != null) {
|
||||
return fromAttribute;
|
||||
}
|
||||
return ConstructorResolver.determinePreferredConstructors(getBeanClass());
|
||||
}
|
||||
|
||||
|
||||
@@ -125,6 +125,20 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
|
||||
*/
|
||||
public static final int DEPENDENCY_CHECK_ALL = 3;
|
||||
|
||||
/**
|
||||
* The name of an attribute that can be
|
||||
* {@link org.springframework.core.AttributeAccessor#setAttribute set} on a
|
||||
* {@link org.springframework.beans.factory.config.BeanDefinition} so that
|
||||
* bean definitions can indicate one or more preferred constructors. This is
|
||||
* analogous to {@code @Autowired} annotated constructors on the bean class.
|
||||
* <p>The attribute value may be a single {@link java.lang.reflect.Constructor}
|
||||
* reference or an array thereof.
|
||||
* @since 6.1
|
||||
* @see org.springframework.beans.factory.annotation.Autowired
|
||||
* @see org.springframework.beans.factory.support.RootBeanDefinition#getPreferredConstructors()
|
||||
*/
|
||||
public static final String PREFERRED_CONSTRUCTORS_ATTRIBUTE = "preferredConstructors";
|
||||
|
||||
/**
|
||||
* Constant that indicates the container should attempt to infer the
|
||||
* {@link #setDestroyMethodName destroy method name} for a bean as opposed to
|
||||
|
||||
@@ -1698,13 +1698,17 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
|
||||
*/
|
||||
ResolvableType getTypeForFactoryBeanFromAttributes(AttributeAccessor attributes) {
|
||||
Object attribute = attributes.getAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE);
|
||||
if (attribute == null) {
|
||||
return ResolvableType.NONE;
|
||||
}
|
||||
if (attribute instanceof ResolvableType resolvableType) {
|
||||
return resolvableType;
|
||||
}
|
||||
if (attribute instanceof Class<?> clazz) {
|
||||
return ResolvableType.forClass(clazz);
|
||||
}
|
||||
return ResolvableType.NONE;
|
||||
throw new IllegalArgumentException("Invalid value type for attribute '" +
|
||||
FactoryBean.OBJECT_TYPE_ATTRIBUTE + "': " + attribute.getClass());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -384,13 +384,28 @@ public class RootBeanDefinition extends AbstractBeanDefinition {
|
||||
/**
|
||||
* Determine preferred constructors to use for default construction, if any.
|
||||
* Constructor arguments will be autowired if necessary.
|
||||
* <p>As of 6.1, the default implementation of this method takes the
|
||||
* {@link #PREFERRED_CONSTRUCTORS_ATTRIBUTE} attribute into account.
|
||||
* Subclasses are encouraged to preserve this through a {@code super} call,
|
||||
* either before or after their own preferred constructor determination.
|
||||
* @return one or more preferred constructors, or {@code null} if none
|
||||
* (in which case the regular no-arg default constructor will be called)
|
||||
* @since 5.1
|
||||
*/
|
||||
@Nullable
|
||||
public Constructor<?>[] getPreferredConstructors() {
|
||||
return null;
|
||||
Object attribute = getAttribute(PREFERRED_CONSTRUCTORS_ATTRIBUTE);
|
||||
if (attribute == null) {
|
||||
return null;
|
||||
}
|
||||
if (attribute instanceof Constructor<?> constructor) {
|
||||
return new Constructor<?>[] {constructor};
|
||||
}
|
||||
if (attribute instanceof Constructor<?>[]) {
|
||||
return (Constructor<?>[]) attribute;
|
||||
}
|
||||
throw new IllegalArgumentException("Invalid value type for attribute '" +
|
||||
PREFERRED_CONSTRUCTORS_ATTRIBUTE + "': " + attribute.getClass());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user