You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Autowiring should support ordered collection driven by Order annotation or Ordered interface [SPR-5574] #10245
has: votes-jiraIssues migrated from JIRA with more than 10 votes at the time of importin: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement
Let suppose that there are beans of a given type T and these beans are marked by interface Ordered or annotation Order. It does make really sense to preserve their order according to Ordered/Order marker if they are collected. See following code for illustration.
@Component @Order(value=1)
public class BusinessServiceA implements BusinessService{}
@Component @Order(value=2)
public class BusinessServiceB implements BusinessService{}
@Component
public class ServiceRegistry { @Autowired
public List<BusinessService> services;
}
Finally implemented as a core feature for 4.0: However, not in the BeanFactory's resolveDependency itself but rather in AutowiredAnnotationBeanPostProcessor, in order to limit its applicability to @Autowired/@Inject. Programmatic callers of resolveDependency can call AnnotationAwareOrderComparator.sortIfNecessary(...) as they see fit.
I'm afraid not, since 3.2.x is in maintenance mode already... with the upcoming 3.2.7 being the last planned release in that line, just fixing a few remaining bugs.
As this will not be backported i add here the steps i took to implement it in an ExtendedBeanFactory for 3.2 at the time. (Upgrading from Spring 3.2 to 4.0 was a breeze by the way! 1h of work. Thanks for that :) )
-> I removed the OrderedWired Annotation in this code, so arrays and collections are always sorted.
create ExtendedBeanFactory:
public class ExtendedBeanFactory extends DefaultListableBeanFactory {
private static final Logger log = LoggerFactory.getLogger(ExtendedBeanFactory.class);
public ExtendedBeanFactory() {
super();
}
public ExtendedBeanFactory(BeanFactory parentBeanFactory) {
super(parentBeanFactory);
}
@Override
protected Object doResolveDependency(DependencyDescriptor descriptor, Class<?> type, String beanName, Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException {
Object result = super.doResolveDependency(descriptor, type, beanName, autowiredBeanNames, typeConverter);
if (result != null) {
if (type.isArray()) {
Object[] resultArray = (Object[]) result;
if (log.isDebugEnabled()) {
log.debug("Sorting results for {}.{}", descriptor.getField().getDeclaringClass(), //$NON-NLS-1$
descriptor.getDependencyName());
}
Arrays.sort(resultArray, AnnotationAwareOrderComparator.INSTANCE);
return resultArray;
} else if (List.class.isAssignableFrom(type)) {
List<?> resultList = (List<?>) super.doResolveDependency(descriptor, type, beanName, autowiredBeanNames,
typeConverter);
if (log.isDebugEnabled()) {
log.debug("Sorting results for {}.{}", descriptor.getField().getDeclaringClass(), //$NON-NLS-1$
descriptor.getDependencyName());
}
Collections.sort(resultList, AnnotationAwareOrderComparator.INSTANCE);
return resultList;
}
}
return result;
}
}
create ExtendedXmlWebApplicationContext:
public class ExtendedXmlWebApplicationContext extends XmlWebApplicationContext {
@Override
protected DefaultListableBeanFactory createBeanFactory() {
return new ExtendedBeanFactory(getInternalParentBeanFactory());
}
}
create ExtendedDispatcherServlet
public class ExtendedDispatcherServlet extends DispatcherServlet {
public ExtendedDispatcherServlet() {
super();
setContextClass(ExtendedXmlWebApplicationContext.class);
}
public ExtendedDispatcherServlet(WebApplicationContext webApplicationContext) {
super(webApplicationContext);
setContextClass(ExtendedXmlWebApplicationContext.class);
}
}
For test integration this was a bit more tricky as i needed to override the AbstractDelegatingSmartContextLoader. If this code is desired I can post this too.
As said, upgrading to 4.0 was a simple excercise, so if you can upgrade i would go for that solution.
If I want to sort a list of beans of type A for example, I don't think it's enough to implement Ordered interface, I move to 4.0.0 but the getOrder method it's not call, I have to declare bean of type BeanPostProcessor in order to be call getOrder method from OrderComparator class.
This issue has been closed. If you have a question, please raise it on StackOverflow with the #spring tag. What you are describing should work out-of-the box: both Ordered implementations and annotated class with @Order are used to order the collection to auto wire.
has: votes-jiraIssues migrated from JIRA with more than 10 votes at the time of importin: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement
Activity
spring-projects-issues commentedon Jul 11, 2011
David Erickson commented
Would love to see this implemented, have run into use cases needing this!!
spring-projects-issues commentedon May 27, 2013
Martin Frey commented
Here is a version that I have implemented at the time I needed that feature in an ExtendedBeanFactory:
And this would be wired like this at the moment:
spring-projects-issues commentedon Aug 22, 2013
Juergen Hoeller commented
Finally implemented as a core feature for 4.0: However, not in the BeanFactory's resolveDependency itself but rather in AutowiredAnnotationBeanPostProcessor, in order to limit its applicability to
@Autowired/@Inject. Programmatic callers of resolveDependency can call AnnotationAwareOrderComparator.sortIfNecessary(...) as they see fit.Juergen
spring-projects-issues commentedon Jan 6, 2014
Ittai Zeidman commented
@Juergen,Any chance this will be backported to 3.2.x?
spring-projects-issues commentedon Jan 7, 2014
Juergen Hoeller commented
I'm afraid not, since 3.2.x is in maintenance mode already... with the upcoming 3.2.7 being the last planned release in that line, just fixing a few remaining bugs.
That said, is there anything preventing you from upgrading to the 4.0 line? The upcoming 4.0.1 fixes all known regressions from 3.2.x, so it should be a very smooth upgrade... as long as the minimal system requirements are met (see https://github.com/spring-projects/spring-framework/wiki/Migrating-from-earlier-versions-of-the-spring-framework).
Juergen
spring-projects-issues commentedon Jan 7, 2014
Martin Frey commented
As this will not be backported i add here the steps i took to implement it in an ExtendedBeanFactory for 3.2 at the time. (Upgrading from Spring 3.2 to 4.0 was a breeze by the way! 1h of work. Thanks for that :) )
-> I removed the OrderedWired Annotation in this code, so arrays and collections are always sorted.
For test integration this was a bit more tricky as i needed to override the AbstractDelegatingSmartContextLoader. If this code is desired I can post this too.
As said, upgrading to 4.0 was a simple excercise, so if you can upgrade i would go for that solution.
spring-projects-issues commentedon Jan 15, 2014
Donnchadh O Donnabhain commented
Anybody using Spring WebFlow would be blocked from upgrading to Spring 4.0 until SWF 2.3.3, which includes SWF-1600, is released.
spring-projects-issues commentedon Aug 11, 2014
sorin commented
If I want to sort a list of beans of type A for example, I don't think it's enough to implement Ordered interface, I move to 4.0.0 but the getOrder method it's not call, I have to declare bean of type BeanPostProcessor in order to be call getOrder method from OrderComparator class.
I's normal this behavior ?
thanks
Sorin
spring-projects-issues commentedon Aug 11, 2014
Stéphane Nicoll commented
Hi,
This issue has been closed. If you have a question, please raise it on StackOverflow with the
#springtag. What you are describing should work out-of-the box: bothOrderedimplementations and annotated class with@Orderare used to order the collection to auto wire.Thanks.