Support for @Order at the bean declaration level
This commit introduces OrderProvider and OrderProviderComparator, two interfaces designed to externalize how a collection of element is sorted according to their order value. FactoryAwareOrderProvider is an OrderProvider implementation that knows about the objects to order and the corresponding BeanFactory instance. This allows to retrieve additional metadata about the actual instances to sort, such as its factory method. A @Bean method can now holds an additional @Order to define the order value that this bean should have when injected as part of a collection or array. Issue: SPR-11310
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2002-2014 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.beans.factory.support;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
import org.springframework.core.annotation.DefaultOrderProviderComparator;
|
||||
|
||||
/**
|
||||
* The default {@link Comparator} to use to order dependencies. Extend
|
||||
* from {@link DefaultOrderProviderComparator} so that the bean factory
|
||||
* has the ability to provide an {@link org.springframework.core.annotation.OrderProvider}
|
||||
* that is aware of more bean metadata, if any.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 4.1
|
||||
* @see org.springframework.core.annotation.OrderProviderComparator
|
||||
* @see org.springframework.core.annotation.OrderProvider
|
||||
* @see DefaultListableBeanFactory#setDependencyComparator(java.util.Comparator)
|
||||
*/
|
||||
public class DefaultDependencyComparator extends DefaultOrderProviderComparator implements Comparator<Object> {
|
||||
|
||||
/**
|
||||
* Shared default instance of DefaultDependencyComparator.
|
||||
*/
|
||||
public static final DefaultDependencyComparator INSTANCE = new DefaultDependencyComparator();
|
||||
|
||||
private final Comparator<Object> comparator;
|
||||
|
||||
public DefaultDependencyComparator(Comparator<Object> comparator) {
|
||||
this.comparator = comparator;
|
||||
}
|
||||
|
||||
public DefaultDependencyComparator() {
|
||||
this(AnnotationAwareOrderComparator.INSTANCE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(Object o1, Object o2) {
|
||||
return comparator.compare(o1, o2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -32,6 +32,7 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -60,6 +61,7 @@ import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.config.DependencyDescriptor;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.annotation.OrderProviderComparator;
|
||||
import org.springframework.core.annotation.OrderUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
@@ -895,7 +897,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
|
||||
Object result = converter.convertIfNecessary(matchingBeans.values(), type);
|
||||
if (this.dependencyComparator != null && result instanceof Object[]) {
|
||||
Arrays.sort((Object[]) result, this.dependencyComparator);
|
||||
sortArray((Object[]) result, matchingBeans);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -922,7 +924,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
|
||||
Object result = converter.convertIfNecessary(matchingBeans.values(), type);
|
||||
if (this.dependencyComparator != null && result instanceof List) {
|
||||
Collections.sort((List<?>) result, this.dependencyComparator);
|
||||
sortList((List<?>) result, matchingBeans);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -983,6 +985,32 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
}
|
||||
}
|
||||
|
||||
private void sortArray(Object[] items, Map<String, Object> matchingBeans) {
|
||||
if (this.dependencyComparator instanceof OrderProviderComparator) {
|
||||
((OrderProviderComparator) this.dependencyComparator)
|
||||
.sortArray(items, createFactoryAwareOrderProvider(matchingBeans));
|
||||
} else {
|
||||
Arrays.sort(items, this.dependencyComparator);
|
||||
}
|
||||
}
|
||||
|
||||
private void sortList(List<?> items, Map<String, Object> matchingBeans) {
|
||||
if (this.dependencyComparator instanceof OrderProviderComparator) {
|
||||
((OrderProviderComparator) this.dependencyComparator)
|
||||
.sortList(items, createFactoryAwareOrderProvider(matchingBeans));
|
||||
} else {
|
||||
Collections.sort(items, this.dependencyComparator);
|
||||
}
|
||||
}
|
||||
|
||||
private FactoryAwareOrderProvider createFactoryAwareOrderProvider(Map<String, Object> beans) {
|
||||
IdentityHashMap<Object, String> instancesToBeanNames = new IdentityHashMap<Object, String>();
|
||||
for (Map.Entry<String, Object> entry : beans.entrySet()) {
|
||||
instancesToBeanNames.put(entry.getValue(), entry.getKey());
|
||||
}
|
||||
return new FactoryAwareOrderProvider(instancesToBeanNames, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find bean instances that match the required type.
|
||||
* Called during autowiring for the specified bean.
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 2002-2014 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.beans.factory.support;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.annotation.OrderProvider;
|
||||
|
||||
/**
|
||||
* An {@link OrderProvider} implementation 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 retrieve the {@link Order} value defined on it. This
|
||||
* essentially allows for the following construct:
|
||||
*
|
||||
* <pre class="code">
|
||||
* @Configuration
|
||||
* public class AppConfig {
|
||||
*
|
||||
* @Bean
|
||||
* @Order(5)
|
||||
* public MyService myService() {
|
||||
* return new MyService();
|
||||
* }
|
||||
* }</pre>
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 4.1
|
||||
*/
|
||||
public class FactoryAwareOrderProvider implements OrderProvider {
|
||||
|
||||
private final Map<Object, String> instancesToBeanNames;
|
||||
|
||||
private final ConfigurableListableBeanFactory beanFactory;
|
||||
|
||||
public FactoryAwareOrderProvider(Map<Object, String> instancesToBeanNames,
|
||||
ConfigurableListableBeanFactory beanFactory) {
|
||||
this.instancesToBeanNames = instancesToBeanNames;
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getOrder(Object obj) {
|
||||
Method factoryMethod = getFactoryMethod(instancesToBeanNames.get(obj));
|
||||
if (factoryMethod != null) {
|
||||
Order order = AnnotationUtils.getAnnotation(factoryMethod, Order.class);
|
||||
if (order != null) {
|
||||
return order.value();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Method getFactoryMethod(String beanName) {
|
||||
if (beanName != null && beanFactory.containsBeanDefinition(beanName)) {
|
||||
BeanDefinition bd = beanFactory.getMergedBeanDefinition(beanName);
|
||||
if (bd instanceof RootBeanDefinition) {
|
||||
return ((RootBeanDefinition) bd).getResolvedFactoryMethod();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user