Improve ApplicationContextInitializer sorting
ContextLoader and FrameworkServlet now use AnnotationAwareOrderComparator to support @Order usage; previously supported only implementation of the Ordered interface. git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4381 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
parent
bd541aca94
commit
c7a7618400
|
|
@ -27,16 +27,16 @@ package org.springframework.context;
|
|||
* for declaring a "contextInitializerClasses" context-param and init-param, respectively.
|
||||
*
|
||||
* <p>{@code ApplicationContextInitializer} processors are encouraged to detect
|
||||
* whether Spring's {@link org.springframework.core.Ordered Ordered} or
|
||||
* {@link org.springframework.core.PriorityOrdered PriorityOrdered} interfaces are also
|
||||
* implemented and sort instances accordingly if so prior to invocation.
|
||||
* whether Spring's {@link org.springframework.core.Ordered Ordered} interface has been
|
||||
* implemented or if the @{@link org.springframework.core.annotation.Order Order}
|
||||
* annotation is present and to sort instances accordingly if so prior to invocation.
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @since 3.1
|
||||
* @see org.springframework.web.context.ContextLoader#customizeContext
|
||||
* @see org.springframework.web.context.ContextLoader#CONTEXT_INITIALIZER_CLASSES_PARAM
|
||||
* @see org.springframework.web.servlet.FrameworkServlet#setContextInitializerClasses
|
||||
* @see org.springframework.web.servlet.FrameworkServlet#initializeWebApplicationContext
|
||||
* @see org.springframework.web.servlet.FrameworkServlet#applyInitializers
|
||||
*/
|
||||
public interface ApplicationContextInitializer<C extends ConfigurableApplicationContext> {
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,8 @@ package org.springframework.web.servlet;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.security.Principal;
|
||||
import java.util.TreeSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
|
|
@ -184,8 +185,8 @@ public abstract class FrameworkServlet extends HttpServletBean {
|
|||
private String contextInitializerClasses;
|
||||
|
||||
/** Actual ApplicationContextInitializer instances to apply to the context */
|
||||
private TreeSet<ApplicationContextInitializer<ConfigurableApplicationContext>> contextInitializers =
|
||||
new TreeSet<ApplicationContextInitializer<ConfigurableApplicationContext>>(new AnnotationAwareOrderComparator());
|
||||
private ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>> contextInitializers =
|
||||
new ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>>();
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -652,6 +653,8 @@ public abstract class FrameworkServlet extends HttpServletBean {
|
|||
}
|
||||
}
|
||||
|
||||
Collections.sort(this.contextInitializers, new AnnotationAwareOrderComparator());
|
||||
|
||||
for (ApplicationContextInitializer<ConfigurableApplicationContext> initializer : this.contextInitializers) {
|
||||
initializer.initialize(wac);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ package org.springframework.web.context;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
|
@ -36,8 +37,8 @@ import org.springframework.context.ApplicationContextInitializer;
|
|||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.access.ContextSingletonBeanFactoryLocator;
|
||||
import org.springframework.core.GenericTypeResolver;
|
||||
import org.springframework.core.OrderComparator;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
|
@ -348,7 +349,7 @@ public class ContextLoader {
|
|||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
throw new ApplicationContextException(
|
||||
"Failed to load context configurer class [" + className + "]", ex);
|
||||
"Failed to load context initializer class [" + className + "]", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -364,8 +365,9 @@ public class ContextLoader {
|
|||
* {@linkplain #CONTEXT_INITIALIZER_CLASSES_PARAM context init parameters} and
|
||||
* {@linkplain ApplicationContextInitializer#initialize invokes each} with the
|
||||
* given web application context.
|
||||
* <p>Any {@link Ordered} {@code ApplicationContextInitializer} will be sorted
|
||||
* appropriately.
|
||||
* <p>Any {@code ApplicationContextInitializers} implementing
|
||||
* {@link org.springframework.core.Ordered Ordered} or marked with @{@link
|
||||
* org.springframework.core.annotation.Order Order} will be sorted appropriately.
|
||||
* @param servletContext the current servlet context
|
||||
* @param applicationContext the newly created application context
|
||||
* @see #createWebApplicationContext(ServletContext, ApplicationContext)
|
||||
|
|
@ -373,7 +375,7 @@ public class ContextLoader {
|
|||
* @see ApplicationContextInitializer#initialize(ConfigurableApplicationContext)
|
||||
*/
|
||||
protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext) {
|
||||
List<ApplicationContextInitializer<ConfigurableApplicationContext>> initializerInstances =
|
||||
ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>> initializerInstances =
|
||||
new ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>>();
|
||||
|
||||
for (Class<ApplicationContextInitializer<ConfigurableApplicationContext>> initializerClass :
|
||||
|
|
@ -388,7 +390,7 @@ public class ContextLoader {
|
|||
initializerInstances.add(BeanUtils.instantiateClass(initializerClass));
|
||||
}
|
||||
|
||||
OrderComparator.sort(initializerInstances);
|
||||
Collections.sort(initializerInstances, new AnnotationAwareOrderComparator());
|
||||
|
||||
for (ApplicationContextInitializer<ConfigurableApplicationContext> initializer : initializerInstances) {
|
||||
initializer.initialize(applicationContext);
|
||||
|
|
|
|||
Loading…
Reference in New Issue