Skip to content

Commit 13659d6

Browse files
committed
Consistent support for @order annotation as alternative to Ordered interface
Issue: SPR-12806
1 parent f5b4e18 commit 13659d6

File tree

12 files changed

+141
-115
lines changed

12 files changed

+141
-115
lines changed

spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJProxyFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,7 +27,7 @@
2727
import org.springframework.aop.framework.AopConfigException;
2828
import org.springframework.aop.framework.ProxyCreatorSupport;
2929
import org.springframework.aop.support.AopUtils;
30-
import org.springframework.core.OrderComparator;
30+
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
3131
import org.springframework.util.Assert;
3232
import org.springframework.util.ClassUtils;
3333

@@ -121,7 +121,7 @@ private void addAdvisorsFromAspectInstanceFactory(MetadataAwareAspectInstanceFac
121121
List<Advisor> advisors = this.aspectFactory.getAdvisors(instanceFactory);
122122
advisors = AopUtils.findAdvisorsThatCanApply(advisors, getTargetClass());
123123
AspectJProxyUtils.makeAdvisorChainAspectJCapableIfNecessary(advisors);
124-
OrderComparator.sort(advisors);
124+
AnnotationAwareOrderComparator.sort(advisors);
125125
addAdvisors(advisors);
126126
}
127127

spring-aop/src/main/java/org/springframework/aop/aspectj/autoproxy/AspectJPrecedenceComparator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,7 @@
2121
import org.springframework.aop.Advisor;
2222
import org.springframework.aop.aspectj.AspectJAopUtils;
2323
import org.springframework.aop.aspectj.AspectJPrecedenceInformation;
24-
import org.springframework.core.OrderComparator;
24+
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
2525
import org.springframework.util.Assert;
2626

2727
/**
@@ -62,7 +62,7 @@ class AspectJPrecedenceComparator implements Comparator<Advisor> {
6262
* Create a default AspectJPrecedenceComparator.
6363
*/
6464
public AspectJPrecedenceComparator() {
65-
this.advisorComparator = OrderComparator.INSTANCE;
65+
this.advisorComparator = AnnotationAwareOrderComparator.INSTANCE;
6666
}
6767

6868
/**

spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,7 +44,7 @@
4444
import org.springframework.beans.factory.FactoryBean;
4545
import org.springframework.beans.factory.FactoryBeanNotInitializedException;
4646
import org.springframework.beans.factory.ListableBeanFactory;
47-
import org.springframework.core.OrderComparator;
47+
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
4848
import org.springframework.util.ClassUtils;
4949
import org.springframework.util.ObjectUtils;
5050

@@ -525,7 +525,7 @@ private void addGlobalAdvisor(ListableBeanFactory beanFactory, String prefix) {
525525
beans.add(bean);
526526
names.put(bean, name);
527527
}
528-
OrderComparator.sort(beans);
528+
AnnotationAwareOrderComparator.sort(beans);
529529
for (Object bean : beans) {
530530
String name = names.get(bean);
531531
if (name.startsWith(prefix)) {

spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAdvisorAutoProxyCreator.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,7 +23,7 @@
2323
import org.springframework.aop.support.AopUtils;
2424
import org.springframework.beans.factory.BeanFactory;
2525
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
26-
import org.springframework.core.OrderComparator;
26+
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
2727

2828
/**
2929
* Generic auto proxy creator that builds AOP proxies for specific beans
@@ -138,10 +138,11 @@ protected boolean isEligibleAdvisorBean(String beanName) {
138138
* @param advisors the source List of Advisors
139139
* @return the sorted List of Advisors
140140
* @see org.springframework.core.Ordered
141-
* @see org.springframework.core.OrderComparator
141+
* @see org.springframework.core.annotation.Order
142+
* @see org.springframework.core.annotation.AnnotationAwareOrderComparator
142143
*/
143144
protected List<Advisor> sortAdvisors(List<Advisor> advisors) {
144-
OrderComparator.sort(advisors);
145+
AnnotationAwareOrderComparator.sort(advisors);
145146
return advisors;
146147
}
147148

spring-context/src/main/java/org/springframework/context/support/PostProcessorRegistrationDelegate.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.Collection;
21+
import java.util.Collections;
22+
import java.util.Comparator;
2123
import java.util.HashSet;
2224
import java.util.LinkedList;
2325
import java.util.List;
@@ -35,6 +37,7 @@
3537
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
3638
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
3739
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
40+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
3841
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
3942
import org.springframework.beans.factory.support.RootBeanDefinition;
4043
import org.springframework.context.ApplicationListener;
@@ -90,7 +93,7 @@ public static void invokeBeanFactoryPostProcessors(
9093
processedBeans.add(ppName);
9194
}
9295
}
93-
OrderComparator.sort(priorityOrderedPostProcessors);
96+
sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
9497
registryPostProcessors.addAll(priorityOrderedPostProcessors);
9598
invokeBeanDefinitionRegistryPostProcessors(priorityOrderedPostProcessors, registry);
9699

@@ -103,7 +106,7 @@ public static void invokeBeanFactoryPostProcessors(
103106
processedBeans.add(ppName);
104107
}
105108
}
106-
OrderComparator.sort(orderedPostProcessors);
109+
sortPostProcessors(beanFactory, orderedPostProcessors);
107110
registryPostProcessors.addAll(orderedPostProcessors);
108111
invokeBeanDefinitionRegistryPostProcessors(orderedPostProcessors, registry);
109112

@@ -159,15 +162,15 @@ else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
159162
}
160163

161164
// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
162-
OrderComparator.sort(priorityOrderedPostProcessors);
165+
sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
163166
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
164167

165168
// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
166169
List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
167170
for (String postProcessorName : orderedPostProcessorNames) {
168171
orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
169172
}
170-
OrderComparator.sort(orderedPostProcessors);
173+
sortPostProcessors(beanFactory, orderedPostProcessors);
171174
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
172175

173176
// Finally, invoke all other BeanFactoryPostProcessors.
@@ -212,7 +215,7 @@ else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
212215
}
213216

214217
// First, register the BeanPostProcessors that implement PriorityOrdered.
215-
OrderComparator.sort(priorityOrderedPostProcessors);
218+
sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
216219
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
217220

218221
// Next, register the BeanPostProcessors that implement Ordered.
@@ -224,7 +227,7 @@ else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
224227
internalPostProcessors.add(pp);
225228
}
226229
}
227-
OrderComparator.sort(orderedPostProcessors);
230+
sortPostProcessors(beanFactory, orderedPostProcessors);
228231
registerBeanPostProcessors(beanFactory, orderedPostProcessors);
229232

230233
// Now, register all regular BeanPostProcessors.
@@ -239,12 +242,23 @@ else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
239242
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
240243

241244
// Finally, re-register all internal BeanPostProcessors.
242-
OrderComparator.sort(internalPostProcessors);
245+
sortPostProcessors(beanFactory, internalPostProcessors);
243246
registerBeanPostProcessors(beanFactory, internalPostProcessors);
244247

245248
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
246249
}
247250

251+
private static void sortPostProcessors(ConfigurableListableBeanFactory beanFactory, List<?> postProcessors) {
252+
Comparator<Object> comparatorToUse = null;
253+
if (beanFactory instanceof DefaultListableBeanFactory) {
254+
comparatorToUse = ((DefaultListableBeanFactory) beanFactory).getDependencyComparator();
255+
}
256+
if (comparatorToUse == null) {
257+
comparatorToUse = OrderComparator.INSTANCE;
258+
}
259+
Collections.sort(postProcessors, comparatorToUse);
260+
}
261+
248262
/**
249263
* Invoke the given BeanDefinitionRegistryPostProcessor beans.
250264
*/

spring-tx/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,7 +28,7 @@
2828
import org.apache.commons.logging.LogFactory;
2929

3030
import org.springframework.core.NamedThreadLocal;
31-
import org.springframework.core.OrderComparator;
31+
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
3232
import org.springframework.util.Assert;
3333

3434
/**
@@ -314,7 +314,7 @@ public static List<TransactionSynchronization> getSynchronizations() throws Ille
314314
else {
315315
// Sort lazily here, not in registerSynchronization.
316316
List<TransactionSynchronization> sortedSynchs = new ArrayList<TransactionSynchronization>(synchs);
317-
OrderComparator.sort(sortedSynchs);
317+
AnnotationAwareOrderComparator.sort(sortedSynchs);
318318
return Collections.unmodifiableList(sortedSynchs);
319319
}
320320
}

spring-webmvc-portlet/src/main/java/org/springframework/web/portlet/DispatcherPortlet.java

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,7 +48,7 @@
4848
import org.springframework.beans.factory.BeanInitializationException;
4949
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
5050
import org.springframework.context.ApplicationContext;
51-
import org.springframework.core.OrderComparator;
51+
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
5252
import org.springframework.core.io.ClassPathResource;
5353
import org.springframework.core.io.support.PropertiesLoaderUtils;
5454
import org.springframework.core.style.StylerUtils;
@@ -66,9 +66,8 @@
6666
* controllers. Dispatches to registered handlers for processing a portlet request.
6767
*
6868
* <p>This portlet is very flexible: It can be used with just about any workflow,
69-
* with the installation of the appropriate adapter classes. It offers the
70-
* following functionality that distinguishes it from other request-driven
71-
* portlet MVC frameworks:
69+
* with the installation of the appropriate adapter classes. It offers the following
70+
* functionality that distinguishes it from other request-driven Portlet MVC frameworks:
7271
*
7372
* <ul>
7473
* <li>It is based around a JavaBeans configuration mechanism.
@@ -77,8 +76,8 @@
7776
* as part of an application - to control the routing of requests to handler objects.
7877
* Default is a {@link org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping}.
7978
* HandlerMapping objects can be defined as beans in the portlet's application context,
80-
* implementing the HandlerMapping interface, overriding the default HandlerMapping if present.
81-
* HandlerMappings can be given any bean name (they are tested by type).
79+
* implementing the HandlerMapping interface, overriding the default HandlerMapping
80+
* if present. HandlerMappings can be given any bean name (they are tested by type).
8281
*
8382
* <li>It can use any {@link HandlerAdapter}; this allows for using any handler interface.
8483
* The default adapter is {@link org.springframework.web.portlet.mvc.SimpleControllerHandlerAdapter}
@@ -102,27 +101,24 @@
102101
* (they are tested by type).
103102
*
104103
* <li>The dispatcher's strategy for resolving multipart requests is determined by a
105-
* {@link org.springframework.web.portlet.multipart.PortletMultipartResolver} implementation.
106-
* An implementations for Jakarta Commons FileUpload is included:
104+
* {@link org.springframework.web.portlet.multipart.PortletMultipartResolver}
105+
* implementation. An implementations for Apache Commons FileUpload is included:
107106
* {@link org.springframework.web.portlet.multipart.CommonsPortletMultipartResolver}.
108107
* The MultipartResolver bean name is "portletMultipartResolver"; default is none.
109108
* </ul>
110109
*
111-
* <p><b>NOTE: The {@code @RequestMapping} annotation will only be processed
112-
* if a corresponding {@code HandlerMapping} (for type level annotations)
113-
* and/or {@code HandlerAdapter} (for method level annotations)
114-
* is present in the dispatcher.</b> This is the case by default.
115-
* However, if you are defining custom {@code HandlerMappings} or
116-
* {@code HandlerAdapters}, then you need to make sure that a
117-
* corresponding custom {@code DefaultAnnotationHandlerMapping}
118-
* and/or {@code AnnotationMethodHandlerAdapter} is defined as well
119-
* - provided that you intend to use {@code @RequestMapping}.
110+
* <p><b>NOTE: The {@code @RequestMapping} annotation will only be processed if a
111+
* corresponding {@code HandlerMapping} (for type-level annotations) and/or
112+
* {@code HandlerAdapter} (for method-level annotations) is present in the dispatcher.</b>
113+
* This is the case by default. However, if you are defining custom {@code HandlerMappings}
114+
* or {@code HandlerAdapters}, then you need to make sure that a corresponding custom
115+
* {@code DefaultAnnotationHandlerMapping} and/or {@code AnnotationMethodHandlerAdapter}
116+
* is defined as well - provided that you intend to use {@code @RequestMapping}.
120117
*
121118
* <p><b>A web application can define any number of DispatcherPortlets.</b>
122-
* Each portlet will operate in its own namespace, loading its own application
123-
* context with mappings, handlers, etc. Only the root application context
124-
* as loaded by {@link org.springframework.web.context.ContextLoaderListener},
125-
* if any, will be shared.
119+
* Each portlet will operate in its own namespace, loading its own application context
120+
* with mappings, handlers, etc. Only the root application context as loaded by
121+
* {@link org.springframework.web.context.ContextLoaderListener}, if any, will be shared.
126122
*
127123
* <p>Thanks to Rainer Schmitz, Nick Lothian and Eric Dalquist for their suggestions!
128124
*
@@ -401,7 +397,7 @@ private void initHandlerMappings(ApplicationContext context) {
401397
if (!matchingBeans.isEmpty()) {
402398
this.handlerMappings = new ArrayList<HandlerMapping>(matchingBeans.values());
403399
// We keep HandlerMappings in sorted order.
404-
OrderComparator.sort(this.handlerMappings);
400+
AnnotationAwareOrderComparator.sort(this.handlerMappings);
405401
}
406402
}
407403
else {
@@ -439,7 +435,7 @@ private void initHandlerAdapters(ApplicationContext context) {
439435
if (!matchingBeans.isEmpty()) {
440436
this.handlerAdapters = new ArrayList<HandlerAdapter>(matchingBeans.values());
441437
// We keep HandlerAdapters in sorted order.
442-
OrderComparator.sort(this.handlerAdapters);
438+
AnnotationAwareOrderComparator.sort(this.handlerAdapters);
443439
}
444440
}
445441
else {
@@ -477,7 +473,7 @@ private void initHandlerExceptionResolvers(ApplicationContext context) {
477473
if (!matchingBeans.isEmpty()) {
478474
this.handlerExceptionResolvers = new ArrayList<HandlerExceptionResolver>(matchingBeans.values());
479475
// We keep HandlerExceptionResolvers in sorted order.
480-
OrderComparator.sort(this.handlerExceptionResolvers);
476+
AnnotationAwareOrderComparator.sort(this.handlerExceptionResolvers);
481477
}
482478
}
483479
else {
@@ -516,7 +512,7 @@ private void initViewResolvers(ApplicationContext context) {
516512
if (!matchingBeans.isEmpty()) {
517513
this.viewResolvers = new ArrayList<ViewResolver>(matchingBeans.values());
518514
// We keep ViewResolvers in sorted order.
519-
OrderComparator.sort(this.viewResolvers);
515+
AnnotationAwareOrderComparator.sort(this.viewResolvers);
520516
}
521517
}
522518
else {

0 commit comments

Comments
 (0)