Browse Source

Consistent support for @Order annotation as alternative to Ordered interface

Issue: SPR-12806
pull/755/head
Juergen Hoeller 11 years ago
parent
commit
13659d645b
  1. 6
      spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJProxyFactory.java
  2. 6
      spring-aop/src/main/java/org/springframework/aop/aspectj/autoproxy/AspectJPrecedenceComparator.java
  3. 6
      spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java
  4. 9
      spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAdvisorAutoProxyCreator.java
  5. 28
      spring-context/src/main/java/org/springframework/context/support/PostProcessorRegistrationDelegate.java
  6. 6
      spring-tx/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java
  7. 48
      spring-webmvc-portlet/src/main/java/org/springframework/web/portlet/DispatcherPortlet.java
  8. 126
      spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java
  9. 4
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java
  10. 7
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java
  11. 4
      spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlProvider.java
  12. 6
      spring-webmvc/src/main/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolver.java

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -27,7 +27,7 @@ import org.springframework.aop.aspectj.AspectJProxyUtils;
import org.springframework.aop.framework.AopConfigException; import org.springframework.aop.framework.AopConfigException;
import org.springframework.aop.framework.ProxyCreatorSupport; import org.springframework.aop.framework.ProxyCreatorSupport;
import org.springframework.aop.support.AopUtils; import org.springframework.aop.support.AopUtils;
import org.springframework.core.OrderComparator; import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
@ -121,7 +121,7 @@ public class AspectJProxyFactory extends ProxyCreatorSupport {
List<Advisor> advisors = this.aspectFactory.getAdvisors(instanceFactory); List<Advisor> advisors = this.aspectFactory.getAdvisors(instanceFactory);
advisors = AopUtils.findAdvisorsThatCanApply(advisors, getTargetClass()); advisors = AopUtils.findAdvisorsThatCanApply(advisors, getTargetClass());
AspectJProxyUtils.makeAdvisorChainAspectJCapableIfNecessary(advisors); AspectJProxyUtils.makeAdvisorChainAspectJCapableIfNecessary(advisors);
OrderComparator.sort(advisors); AnnotationAwareOrderComparator.sort(advisors);
addAdvisors(advisors); addAdvisors(advisors);
} }

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -21,7 +21,7 @@ import java.util.Comparator;
import org.springframework.aop.Advisor; import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJAopUtils; import org.springframework.aop.aspectj.AspectJAopUtils;
import org.springframework.aop.aspectj.AspectJPrecedenceInformation; import org.springframework.aop.aspectj.AspectJPrecedenceInformation;
import org.springframework.core.OrderComparator; import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
@ -62,7 +62,7 @@ class AspectJPrecedenceComparator implements Comparator<Advisor> {
* Create a default AspectJPrecedenceComparator. * Create a default AspectJPrecedenceComparator.
*/ */
public AspectJPrecedenceComparator() { public AspectJPrecedenceComparator() {
this.advisorComparator = OrderComparator.INSTANCE; this.advisorComparator = AnnotationAwareOrderComparator.INSTANCE;
} }
/** /**

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -44,7 +44,7 @@ import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.FactoryBeanNotInitializedException; import org.springframework.beans.factory.FactoryBeanNotInitializedException;
import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.core.OrderComparator; import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
@ -525,7 +525,7 @@ public class ProxyFactoryBean extends ProxyCreatorSupport
beans.add(bean); beans.add(bean);
names.put(bean, name); names.put(bean, name);
} }
OrderComparator.sort(beans); AnnotationAwareOrderComparator.sort(beans);
for (Object bean : beans) { for (Object bean : beans) {
String name = names.get(bean); String name = names.get(bean);
if (name.startsWith(prefix)) { if (name.startsWith(prefix)) {

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,7 +23,7 @@ import org.springframework.aop.TargetSource;
import org.springframework.aop.support.AopUtils; import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.OrderComparator; import org.springframework.core.annotation.AnnotationAwareOrderComparator;
/** /**
* Generic auto proxy creator that builds AOP proxies for specific beans * Generic auto proxy creator that builds AOP proxies for specific beans
@ -138,10 +138,11 @@ public abstract class AbstractAdvisorAutoProxyCreator extends AbstractAutoProxyC
* @param advisors the source List of Advisors * @param advisors the source List of Advisors
* @return the sorted List of Advisors * @return the sorted List of Advisors
* @see org.springframework.core.Ordered * @see org.springframework.core.Ordered
* @see org.springframework.core.OrderComparator * @see org.springframework.core.annotation.Order
* @see org.springframework.core.annotation.AnnotationAwareOrderComparator
*/ */
protected List<Advisor> sortAdvisors(List<Advisor> advisors) { protected List<Advisor> sortAdvisors(List<Advisor> advisors) {
OrderComparator.sort(advisors); AnnotationAwareOrderComparator.sort(advisors);
return advisors; return advisors;
} }

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

@ -18,6 +18,8 @@ package org.springframework.context.support;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -35,6 +37,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor; import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor; import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
@ -90,7 +93,7 @@ class PostProcessorRegistrationDelegate {
processedBeans.add(ppName); processedBeans.add(ppName);
} }
} }
OrderComparator.sort(priorityOrderedPostProcessors); sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
registryPostProcessors.addAll(priorityOrderedPostProcessors); registryPostProcessors.addAll(priorityOrderedPostProcessors);
invokeBeanDefinitionRegistryPostProcessors(priorityOrderedPostProcessors, registry); invokeBeanDefinitionRegistryPostProcessors(priorityOrderedPostProcessors, registry);
@ -103,7 +106,7 @@ class PostProcessorRegistrationDelegate {
processedBeans.add(ppName); processedBeans.add(ppName);
} }
} }
OrderComparator.sort(orderedPostProcessors); sortPostProcessors(beanFactory, orderedPostProcessors);
registryPostProcessors.addAll(orderedPostProcessors); registryPostProcessors.addAll(orderedPostProcessors);
invokeBeanDefinitionRegistryPostProcessors(orderedPostProcessors, registry); invokeBeanDefinitionRegistryPostProcessors(orderedPostProcessors, registry);
@ -159,7 +162,7 @@ class PostProcessorRegistrationDelegate {
} }
// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered. // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
OrderComparator.sort(priorityOrderedPostProcessors); sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory); invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
// Next, invoke the BeanFactoryPostProcessors that implement Ordered. // Next, invoke the BeanFactoryPostProcessors that implement Ordered.
@ -167,7 +170,7 @@ class PostProcessorRegistrationDelegate {
for (String postProcessorName : orderedPostProcessorNames) { for (String postProcessorName : orderedPostProcessorNames) {
orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
} }
OrderComparator.sort(orderedPostProcessors); sortPostProcessors(beanFactory, orderedPostProcessors);
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory); invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
// Finally, invoke all other BeanFactoryPostProcessors. // Finally, invoke all other BeanFactoryPostProcessors.
@ -212,7 +215,7 @@ class PostProcessorRegistrationDelegate {
} }
// First, register the BeanPostProcessors that implement PriorityOrdered. // First, register the BeanPostProcessors that implement PriorityOrdered.
OrderComparator.sort(priorityOrderedPostProcessors); sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors); registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
// Next, register the BeanPostProcessors that implement Ordered. // Next, register the BeanPostProcessors that implement Ordered.
@ -224,7 +227,7 @@ class PostProcessorRegistrationDelegate {
internalPostProcessors.add(pp); internalPostProcessors.add(pp);
} }
} }
OrderComparator.sort(orderedPostProcessors); sortPostProcessors(beanFactory, orderedPostProcessors);
registerBeanPostProcessors(beanFactory, orderedPostProcessors); registerBeanPostProcessors(beanFactory, orderedPostProcessors);
// Now, register all regular BeanPostProcessors. // Now, register all regular BeanPostProcessors.
@ -239,12 +242,23 @@ class PostProcessorRegistrationDelegate {
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors); registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
// Finally, re-register all internal BeanPostProcessors. // Finally, re-register all internal BeanPostProcessors.
OrderComparator.sort(internalPostProcessors); sortPostProcessors(beanFactory, internalPostProcessors);
registerBeanPostProcessors(beanFactory, internalPostProcessors); registerBeanPostProcessors(beanFactory, internalPostProcessors);
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext)); beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
} }
private static void sortPostProcessors(ConfigurableListableBeanFactory beanFactory, List<?> postProcessors) {
Comparator<Object> comparatorToUse = null;
if (beanFactory instanceof DefaultListableBeanFactory) {
comparatorToUse = ((DefaultListableBeanFactory) beanFactory).getDependencyComparator();
}
if (comparatorToUse == null) {
comparatorToUse = OrderComparator.INSTANCE;
}
Collections.sort(postProcessors, comparatorToUse);
}
/** /**
* Invoke the given BeanDefinitionRegistryPostProcessor beans. * Invoke the given BeanDefinitionRegistryPostProcessor beans.
*/ */

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,7 +28,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.core.NamedThreadLocal; import org.springframework.core.NamedThreadLocal;
import org.springframework.core.OrderComparator; import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
@ -314,7 +314,7 @@ public abstract class TransactionSynchronizationManager {
else { else {
// Sort lazily here, not in registerSynchronization. // Sort lazily here, not in registerSynchronization.
List<TransactionSynchronization> sortedSynchs = new ArrayList<TransactionSynchronization>(synchs); List<TransactionSynchronization> sortedSynchs = new ArrayList<TransactionSynchronization>(synchs);
OrderComparator.sort(sortedSynchs); AnnotationAwareOrderComparator.sort(sortedSynchs);
return Collections.unmodifiableList(sortedSynchs); return Collections.unmodifiableList(sortedSynchs);
} }
} }

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -48,7 +48,7 @@ import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.BeanInitializationException; import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.core.OrderComparator; import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.core.style.StylerUtils; import org.springframework.core.style.StylerUtils;
@ -66,9 +66,8 @@ import org.springframework.web.servlet.ViewResolver;
* controllers. Dispatches to registered handlers for processing a portlet request. * controllers. Dispatches to registered handlers for processing a portlet request.
* *
* <p>This portlet is very flexible: It can be used with just about any workflow, * <p>This portlet is very flexible: It can be used with just about any workflow,
* with the installation of the appropriate adapter classes. It offers the * with the installation of the appropriate adapter classes. It offers the following
* following functionality that distinguishes it from other request-driven * functionality that distinguishes it from other request-driven Portlet MVC frameworks:
* portlet MVC frameworks:
* *
* <ul> * <ul>
* <li>It is based around a JavaBeans configuration mechanism. * <li>It is based around a JavaBeans configuration mechanism.
@ -77,8 +76,8 @@ import org.springframework.web.servlet.ViewResolver;
* as part of an application - to control the routing of requests to handler objects. * as part of an application - to control the routing of requests to handler objects.
* Default is a {@link org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping}. * Default is a {@link org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping}.
* HandlerMapping objects can be defined as beans in the portlet's application context, * HandlerMapping objects can be defined as beans in the portlet's application context,
* implementing the HandlerMapping interface, overriding the default HandlerMapping if present. * implementing the HandlerMapping interface, overriding the default HandlerMapping
* HandlerMappings can be given any bean name (they are tested by type). * if present. HandlerMappings can be given any bean name (they are tested by type).
* *
* <li>It can use any {@link HandlerAdapter}; this allows for using any handler interface. * <li>It can use any {@link HandlerAdapter}; this allows for using any handler interface.
* The default adapter is {@link org.springframework.web.portlet.mvc.SimpleControllerHandlerAdapter} * The default adapter is {@link org.springframework.web.portlet.mvc.SimpleControllerHandlerAdapter}
@ -102,27 +101,24 @@ import org.springframework.web.servlet.ViewResolver;
* (they are tested by type). * (they are tested by type).
* *
* <li>The dispatcher's strategy for resolving multipart requests is determined by a * <li>The dispatcher's strategy for resolving multipart requests is determined by a
* {@link org.springframework.web.portlet.multipart.PortletMultipartResolver} implementation. * {@link org.springframework.web.portlet.multipart.PortletMultipartResolver}
* An implementations for Jakarta Commons FileUpload is included: * implementation. An implementations for Apache Commons FileUpload is included:
* {@link org.springframework.web.portlet.multipart.CommonsPortletMultipartResolver}. * {@link org.springframework.web.portlet.multipart.CommonsPortletMultipartResolver}.
* The MultipartResolver bean name is "portletMultipartResolver"; default is none. * The MultipartResolver bean name is "portletMultipartResolver"; default is none.
* </ul> * </ul>
* *
* <p><b>NOTE: The {@code @RequestMapping} annotation will only be processed * <p><b>NOTE: The {@code @RequestMapping} annotation will only be processed if a
* if a corresponding {@code HandlerMapping} (for type level annotations) * corresponding {@code HandlerMapping} (for type-level annotations) and/or
* and/or {@code HandlerAdapter} (for method level annotations) * {@code HandlerAdapter} (for method-level annotations) is present in the dispatcher.</b>
* is present in the dispatcher.</b> This is the case by default. * This is the case by default. However, if you are defining custom {@code HandlerMappings}
* However, if you are defining custom {@code HandlerMappings} or * or {@code HandlerAdapters}, then you need to make sure that a corresponding custom
* {@code HandlerAdapters}, then you need to make sure that a * {@code DefaultAnnotationHandlerMapping} and/or {@code AnnotationMethodHandlerAdapter}
* corresponding custom {@code DefaultAnnotationHandlerMapping} * is defined as well - provided that you intend to use {@code @RequestMapping}.
* and/or {@code AnnotationMethodHandlerAdapter} is defined as well
* - provided that you intend to use {@code @RequestMapping}.
* *
* <p><b>A web application can define any number of DispatcherPortlets.</b> * <p><b>A web application can define any number of DispatcherPortlets.</b>
* Each portlet will operate in its own namespace, loading its own application * Each portlet will operate in its own namespace, loading its own application context
* context with mappings, handlers, etc. Only the root application context * with mappings, handlers, etc. Only the root application context as loaded by
* as loaded by {@link org.springframework.web.context.ContextLoaderListener}, * {@link org.springframework.web.context.ContextLoaderListener}, if any, will be shared.
* if any, will be shared.
* *
* <p>Thanks to Rainer Schmitz, Nick Lothian and Eric Dalquist for their suggestions! * <p>Thanks to Rainer Schmitz, Nick Lothian and Eric Dalquist for their suggestions!
* *
@ -401,7 +397,7 @@ public class DispatcherPortlet extends FrameworkPortlet {
if (!matchingBeans.isEmpty()) { if (!matchingBeans.isEmpty()) {
this.handlerMappings = new ArrayList<HandlerMapping>(matchingBeans.values()); this.handlerMappings = new ArrayList<HandlerMapping>(matchingBeans.values());
// We keep HandlerMappings in sorted order. // We keep HandlerMappings in sorted order.
OrderComparator.sort(this.handlerMappings); AnnotationAwareOrderComparator.sort(this.handlerMappings);
} }
} }
else { else {
@ -439,7 +435,7 @@ public class DispatcherPortlet extends FrameworkPortlet {
if (!matchingBeans.isEmpty()) { if (!matchingBeans.isEmpty()) {
this.handlerAdapters = new ArrayList<HandlerAdapter>(matchingBeans.values()); this.handlerAdapters = new ArrayList<HandlerAdapter>(matchingBeans.values());
// We keep HandlerAdapters in sorted order. // We keep HandlerAdapters in sorted order.
OrderComparator.sort(this.handlerAdapters); AnnotationAwareOrderComparator.sort(this.handlerAdapters);
} }
} }
else { else {
@ -477,7 +473,7 @@ public class DispatcherPortlet extends FrameworkPortlet {
if (!matchingBeans.isEmpty()) { if (!matchingBeans.isEmpty()) {
this.handlerExceptionResolvers = new ArrayList<HandlerExceptionResolver>(matchingBeans.values()); this.handlerExceptionResolvers = new ArrayList<HandlerExceptionResolver>(matchingBeans.values());
// We keep HandlerExceptionResolvers in sorted order. // We keep HandlerExceptionResolvers in sorted order.
OrderComparator.sort(this.handlerExceptionResolvers); AnnotationAwareOrderComparator.sort(this.handlerExceptionResolvers);
} }
} }
else { else {
@ -516,7 +512,7 @@ public class DispatcherPortlet extends FrameworkPortlet {
if (!matchingBeans.isEmpty()) { if (!matchingBeans.isEmpty()) {
this.viewResolvers = new ArrayList<ViewResolver>(matchingBeans.values()); this.viewResolvers = new ArrayList<ViewResolver>(matchingBeans.values());
// We keep ViewResolvers in sorted order. // We keep ViewResolvers in sorted order.
OrderComparator.sort(this.viewResolvers); AnnotationAwareOrderComparator.sort(this.viewResolvers);
} }
} }
else { else {

126
spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -42,7 +42,7 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.i18n.LocaleContext; import org.springframework.context.i18n.LocaleContext;
import org.springframework.core.OrderComparator; import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.http.server.ServletServerHttpRequest; import org.springframework.http.server.ServletServerHttpRequest;
@ -60,73 +60,89 @@ import org.springframework.web.util.NestedServletException;
import org.springframework.web.util.WebUtils; import org.springframework.web.util.WebUtils;
/** /**
* Central dispatcher for HTTP request handlers/controllers, e.g. for web UI controllers or HTTP-based remote service * Central dispatcher for HTTP request handlers/controllers, e.g. for web UI controllers
* exporters. Dispatches to registered handlers for processing a web request, providing convenient mapping and exception * or HTTP-based remote service exporters. Dispatches to registered handlers for processing
* handling facilities. * a web request, providing convenient mapping and exception handling facilities.
* *
* <p>This servlet is very flexible: It can be used with just about any workflow, with the installation of the * <p>This servlet is very flexible: It can be used with just about any workflow, with the
* appropriate adapter classes. It offers the following functionality that distinguishes it from other request-driven * installation of the appropriate adapter classes. It offers the following functionality
* web MVC frameworks: * that distinguishes it from other request-driven web MVC frameworks:
* *
* <ul> <li>It is based around a JavaBeans configuration mechanism. * <ul>
* <li>It is based around a JavaBeans configuration mechanism.
* *
* <li>It can use any {@link HandlerMapping} implementation - pre-built or provided as part of an application - to * <li>It can use any {@link HandlerMapping} implementation - pre-built or provided as part
* control the routing of requests to handler objects. Default is {@link org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping} * of an application - to control the routing of requests to handler objects. Default is
* and {@link org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping}. HandlerMapping objects * {@link org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping} and
* can be defined as beans in the servlet's application context, implementing the HandlerMapping interface, overriding * {@link org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping}.
* the default HandlerMapping if present. HandlerMappings can be given any bean name (they are tested by type). * HandlerMapping objects can be defined as beans in the servlet's application context,
* implementing the HandlerMapping interface, overriding the default HandlerMapping if
* present. HandlerMappings can be given any bean name (they are tested by type).
* *
* <li>It can use any {@link HandlerAdapter}; this allows for using any handler interface. Default adapters are {@link * <li>It can use any {@link HandlerAdapter}; this allows for using any handler interface.
* org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter}, {@link org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter}, * Default adapters are {@link org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter},
* for Spring's {@link org.springframework.web.HttpRequestHandler} and {@link org.springframework.web.servlet.mvc.Controller} * {@link org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter}, for Spring's
* interfaces, respectively. A default {@link org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter} * {@link org.springframework.web.HttpRequestHandler} and
* will be registered as well. HandlerAdapter objects can be added as beans in the application context, overriding the * {@link org.springframework.web.servlet.mvc.Controller} interfaces, respectively. A default
* default HandlerAdapters. Like HandlerMappings, HandlerAdapters can be given any bean name (they are tested by type). * {@link org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter}
* will be registered as well. HandlerAdapter objects can be added as beans in the
* application context, overriding the default HandlerAdapters. Like HandlerMappings,
* HandlerAdapters can be given any bean name (they are tested by type).
* *
* <li>The dispatcher's exception resolution strategy can be specified via a {@link HandlerExceptionResolver}, for * <li>The dispatcher's exception resolution strategy can be specified via a
* example mapping certain exceptions to error pages. Default are * {@link HandlerExceptionResolver}, for example mapping certain exceptions to error pages.
* Default are
* {@link org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver}, * {@link org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver},
* {@link org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver}, and * {@link org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver}, and
* {@link org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver}. These HandlerExceptionResolvers can be overridden * {@link org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver}.
* through the application context. HandlerExceptionResolver can be given any bean name (they are tested by type). * These HandlerExceptionResolvers can be overridden through the application context.
* HandlerExceptionResolver can be given any bean name (they are tested by type).
* *
* <li>Its view resolution strategy can be specified via a {@link ViewResolver} implementation, resolving symbolic view * <li>Its view resolution strategy can be specified via a {@link ViewResolver}
* names into View objects. Default is {@link org.springframework.web.servlet.view.InternalResourceViewResolver}. * implementation, resolving symbolic view names into View objects. Default is
* ViewResolver objects can be added as beans in the application context, overriding the default ViewResolver. * {@link org.springframework.web.servlet.view.InternalResourceViewResolver}.
* ViewResolvers can be given any bean name (they are tested by type). * ViewResolver objects can be added as beans in the application context, overriding the
* default ViewResolver. ViewResolvers can be given any bean name (they are tested by type).
* *
* <li>If a {@link View} or view name is not supplied by the user, then the configured {@link * <li>If a {@link View} or view name is not supplied by the user, then the configured
* RequestToViewNameTranslator} will translate the current request into a view name. The corresponding bean name is * {@link RequestToViewNameTranslator} will translate the current request into a view name.
* "viewNameTranslator"; the default is {@link org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator}. * The corresponding bean name is "viewNameTranslator"; the default is
* {@link org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator}.
* *
* <li>The dispatcher's strategy for resolving multipart requests is determined by a {@link * <li>The dispatcher's strategy for resolving multipart requests is determined by a
* org.springframework.web.multipart.MultipartResolver} implementation. Implementations for Jakarta Commons FileUpload * {@link org.springframework.web.multipart.MultipartResolver} implementation.
* and Jason Hunter's COS are included; the typical choise is {@link org.springframework.web.multipart.commons.CommonsMultipartResolver}. * Implementations for Apache Commons FileUpload and Servlet 3 are included; the typical
* choice is {@link org.springframework.web.multipart.commons.CommonsMultipartResolver}.
* The MultipartResolver bean name is "multipartResolver"; default is none. * The MultipartResolver bean name is "multipartResolver"; default is none.
* *
* <li>Its locale resolution strategy is determined by a {@link LocaleResolver}. Out-of-the-box implementations work via * <li>Its locale resolution strategy is determined by a {@link LocaleResolver}.
* HTTP accept header, cookie, or session. The LocaleResolver bean name is "localeResolver"; default is {@link * Out-of-the-box implementations work via HTTP accept header, cookie, or session.
* org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver}. * The LocaleResolver bean name is "localeResolver"; default is
* {@link org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver}.
* *
* <li>Its theme resolution strategy is determined by a {@link ThemeResolver}. Implementations for a fixed theme and for * <li>Its theme resolution strategy is determined by a {@link ThemeResolver}.
* cookie and session storage are included. The ThemeResolver bean name is "themeResolver"; default is {@link * Implementations for a fixed theme and for cookie and session storage are included.
* org.springframework.web.servlet.theme.FixedThemeResolver}. </ul> * The ThemeResolver bean name is "themeResolver"; default is
* {@link org.springframework.web.servlet.theme.FixedThemeResolver}.
* </ul>
* *
* <p><b>NOTE: The {@code @RequestMapping} annotation will only be processed if a corresponding * <p><b>NOTE: The {@code @RequestMapping} annotation will only be processed if a
* {@code HandlerMapping} (for type level annotations) and/or {@code HandlerAdapter} (for method level * corresponding {@code HandlerMapping} (for type-level annotations) and/or
* annotations) is present in the dispatcher.</b> This is the case by default. However, if you are defining custom * {@code HandlerAdapter} (for method-level annotations) is present in the dispatcher.</b>
* {@code HandlerMappings} or {@code HandlerAdapters}, then you need to make sure that a corresponding custom * This is the case by default. However, if you are defining custom {@code HandlerMappings}
* {@code DefaultAnnotationHandlerMapping} and/or {@code AnnotationMethodHandlerAdapter} is defined as well - * or {@code HandlerAdapters}, then you need to make sure that a corresponding custom
* provided that you intend to use {@code @RequestMapping}. * {@code DefaultAnnotationHandlerMapping} and/or {@code AnnotationMethodHandlerAdapter}
* is defined as well - provided that you intend to use {@code @RequestMapping}.
* *
* <p><b>A web application can define any number of DispatcherServlets.</b> Each servlet will operate in its own * <p><b>A web application can define any number of DispatcherServlets.</b>
* namespace, loading its own application context with mappings, handlers, etc. Only the root application context as * Each servlet will operate in its own namespace, loading its own application context
* loaded by {@link org.springframework.web.context.ContextLoaderListener}, if any, will be shared. * with mappings, handlers, etc. Only the root application context as loaded by
* {@link org.springframework.web.context.ContextLoaderListener}, if any, will be shared.
* *
* <p>As of Spring 3.1, {@code DispatcherServlet} may now be injected with a web * <p>As of Spring 3.1, {@code DispatcherServlet} may now be injected with a web
* application context, rather than creating its own internally. This is useful in Servlet * application context, rather than creating its own internally. This is useful in Servlet
* 3.0+ environments, which support programmatic registration of servlet instances. See * 3.0+ environments, which support programmatic registration of servlet instances.
* {@link #DispatcherServlet(WebApplicationContext)} Javadoc for details. * See the {@link #DispatcherServlet(WebApplicationContext)} javadoc for details.
* *
* @author Rod Johnson * @author Rod Johnson
* @author Juergen Hoeller * @author Juergen Hoeller
@ -553,7 +569,7 @@ public class DispatcherServlet extends FrameworkServlet {
if (!matchingBeans.isEmpty()) { if (!matchingBeans.isEmpty()) {
this.handlerMappings = new ArrayList<HandlerMapping>(matchingBeans.values()); this.handlerMappings = new ArrayList<HandlerMapping>(matchingBeans.values());
// We keep HandlerMappings in sorted order. // We keep HandlerMappings in sorted order.
OrderComparator.sort(this.handlerMappings); AnnotationAwareOrderComparator.sort(this.handlerMappings);
} }
} }
else { else {
@ -591,7 +607,7 @@ public class DispatcherServlet extends FrameworkServlet {
if (!matchingBeans.isEmpty()) { if (!matchingBeans.isEmpty()) {
this.handlerAdapters = new ArrayList<HandlerAdapter>(matchingBeans.values()); this.handlerAdapters = new ArrayList<HandlerAdapter>(matchingBeans.values());
// We keep HandlerAdapters in sorted order. // We keep HandlerAdapters in sorted order.
OrderComparator.sort(this.handlerAdapters); AnnotationAwareOrderComparator.sort(this.handlerAdapters);
} }
} }
else { else {
@ -629,7 +645,7 @@ public class DispatcherServlet extends FrameworkServlet {
if (!matchingBeans.isEmpty()) { if (!matchingBeans.isEmpty()) {
this.handlerExceptionResolvers = new ArrayList<HandlerExceptionResolver>(matchingBeans.values()); this.handlerExceptionResolvers = new ArrayList<HandlerExceptionResolver>(matchingBeans.values());
// We keep HandlerExceptionResolvers in sorted order. // We keep HandlerExceptionResolvers in sorted order.
OrderComparator.sort(this.handlerExceptionResolvers); AnnotationAwareOrderComparator.sort(this.handlerExceptionResolvers);
} }
} }
else { else {
@ -691,7 +707,7 @@ public class DispatcherServlet extends FrameworkServlet {
if (!matchingBeans.isEmpty()) { if (!matchingBeans.isEmpty()) {
this.viewResolvers = new ArrayList<ViewResolver>(matchingBeans.values()); this.viewResolvers = new ArrayList<ViewResolver>(matchingBeans.values());
// We keep ViewResolvers in sorted order. // We keep ViewResolvers in sorted order.
OrderComparator.sort(this.viewResolvers); AnnotationAwareOrderComparator.sort(this.viewResolvers);
} }
} }
else { else {

4
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java

@ -31,7 +31,7 @@ import javax.xml.transform.Source;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.core.OrderComparator; import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.http.converter.ByteArrayHttpMessageConverter; import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter;
@ -258,7 +258,7 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
} }
List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext()); List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
Collections.sort(adviceBeans, new OrderComparator()); AnnotationAwareOrderComparator.sort(adviceBeans);
for (ControllerAdviceBean adviceBean : adviceBeans) { for (ControllerAdviceBean adviceBean : adviceBeans) {
ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(adviceBean.getBeanType()); ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(adviceBean.getBeanType());

7
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,7 +18,6 @@ package org.springframework.web.servlet.mvc.method.annotation;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -36,8 +35,8 @@ import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.core.DefaultParameterNameDiscoverer; import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.OrderComparator;
import org.springframework.core.ParameterNameDiscoverer; import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.task.AsyncTaskExecutor; import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.core.task.SimpleAsyncTaskExecutor; import org.springframework.core.task.SimpleAsyncTaskExecutor;
@ -519,7 +518,7 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter
} }
List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext()); List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
Collections.sort(beans, new OrderComparator()); AnnotationAwareOrderComparator.sort(beans);
List<Object> responseBodyAdviceBeans = new ArrayList<Object>(); List<Object> responseBodyAdviceBeans = new ArrayList<Object>();

4
spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlProvider.java

@ -30,7 +30,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.OrderComparator; import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.util.AntPathMatcher; import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher; import org.springframework.util.PathMatcher;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
@ -141,7 +141,7 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
Map<String, SimpleUrlHandlerMapping> map = appContext.getBeansOfType(SimpleUrlHandlerMapping.class); Map<String, SimpleUrlHandlerMapping> map = appContext.getBeansOfType(SimpleUrlHandlerMapping.class);
List<SimpleUrlHandlerMapping> handlerMappings = new ArrayList<SimpleUrlHandlerMapping>(map.values()); List<SimpleUrlHandlerMapping> handlerMappings = new ArrayList<SimpleUrlHandlerMapping>(map.values());
Collections.sort(handlerMappings, new OrderComparator()); AnnotationAwareOrderComparator.sort(handlerMappings);
for (SimpleUrlHandlerMapping hm : handlerMappings) { for (SimpleUrlHandlerMapping hm : handlerMappings) {
for (String pattern : hm.getHandlerMap().keySet()) { for (String pattern : hm.getHandlerMap().keySet()) {

6
spring-webmvc/src/main/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolver.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -33,8 +33,8 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.OrderComparator;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -194,7 +194,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
logger.warn("Did not find any ViewResolvers to delegate to; please configure them using the " + logger.warn("Did not find any ViewResolvers to delegate to; please configure them using the " +
"'viewResolvers' property on the ContentNegotiatingViewResolver"); "'viewResolvers' property on the ContentNegotiatingViewResolver");
} }
OrderComparator.sort(this.viewResolvers); AnnotationAwareOrderComparator.sort(this.viewResolvers);
this.cnManagerFactoryBean.setServletContext(servletContext); this.cnManagerFactoryBean.setServletContext(servletContext);
} }

Loading…
Cancel
Save