Introduce ORDER_ATTRIBUTE on AbstractBeanDefinition
This commit allows to define a bean order programmatically at bean definition level (functional equivalent of `@Order`). If specified, the order attribute defined at bean definition level overrides potential values set with `@Order`. See gh-30849
This commit is contained in:
@@ -51,6 +51,7 @@ import org.springframework.util.StringUtils;
|
||||
* @author Juergen Hoeller
|
||||
* @author Rob Harrop
|
||||
* @author Mark Fisher
|
||||
* @author Sebastien Deleuze
|
||||
* @see GenericBeanDefinition
|
||||
* @see RootBeanDefinition
|
||||
* @see ChildBeanDefinition
|
||||
@@ -139,6 +140,18 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
|
||||
*/
|
||||
public static final String PREFERRED_CONSTRUCTORS_ATTRIBUTE = "preferredConstructors";
|
||||
|
||||
/**
|
||||
* 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 the sort order for the targeted bean.
|
||||
* This is analogous to the {@code @Order} annotation.
|
||||
* @since 6.1.2
|
||||
* @see org.springframework.core.annotation.Order
|
||||
* @see org.springframework.core.Ordered
|
||||
*/
|
||||
public static final String ORDER_ATTRIBUTE = "order";
|
||||
|
||||
/**
|
||||
* Constant that indicates the container should attempt to infer the
|
||||
* {@link #setDestroyMethodName destroy method name} for a bean as opposed to
|
||||
|
||||
@@ -70,6 +70,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.config.DependencyDescriptor;
|
||||
import org.springframework.beans.factory.config.NamedBeanHolder;
|
||||
import org.springframework.core.OrderComparator;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.annotation.MergedAnnotation;
|
||||
import org.springframework.core.annotation.MergedAnnotations;
|
||||
@@ -110,6 +111,7 @@ import org.springframework.util.StringUtils;
|
||||
* @author Chris Beams
|
||||
* @author Phillip Webb
|
||||
* @author Stephane Nicoll
|
||||
* @author Sebastien Deleuze
|
||||
* @since 16 April 2001
|
||||
* @see #registerBeanDefinition
|
||||
* @see #addBeanPostProcessor
|
||||
@@ -2230,7 +2232,9 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
* that is aware of the bean metadata of the instances to sort.
|
||||
* <p>Lookup for the method factory of an instance to sort, if any, and let the
|
||||
* comparator retrieve the {@link org.springframework.core.annotation.Order}
|
||||
* value defined on it. This essentially allows for the following construct:
|
||||
* value defined on it.
|
||||
* <p>As of 6.1.2, this class takes the {@link AbstractBeanDefinition#ORDER_ATTRIBUTE}
|
||||
* attribute into account.
|
||||
*/
|
||||
private class FactoryAwareOrderSourceProvider implements OrderComparator.OrderSourceProvider {
|
||||
|
||||
@@ -2249,7 +2253,17 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
}
|
||||
try {
|
||||
RootBeanDefinition beanDefinition = (RootBeanDefinition) getMergedBeanDefinition(beanName);
|
||||
List<Object> sources = new ArrayList<>(2);
|
||||
List<Object> sources = new ArrayList<>(3);
|
||||
Object orderAttribute = beanDefinition.getAttribute(AbstractBeanDefinition.ORDER_ATTRIBUTE);
|
||||
if (orderAttribute != null) {
|
||||
if (orderAttribute instanceof Integer order) {
|
||||
sources.add((Ordered) () -> order);
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException("Invalid value type for attribute '" +
|
||||
AbstractBeanDefinition.ORDER_ATTRIBUTE + "': " + orderAttribute.getClass().getName());
|
||||
}
|
||||
}
|
||||
Method factoryMethod = beanDefinition.getResolvedFactoryMethod();
|
||||
if (factoryMethod != null) {
|
||||
sources.add(factoryMethod);
|
||||
|
||||
Reference in New Issue
Block a user