From 8a4b50e289a803c021426650fdb4b6f8fca4207a Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 24 Apr 2013 10:14:22 +0100 Subject: [PATCH] [bs-14] Ensure all servlet context initializers are called Misuse of TreeSet in EmbeddedWebApplicationContext corrected (use List and Comparator instead of TreeSet). [Fixes #48055339] --- .../EmbeddedWebApplicationContext.java | 24 +++++++++---------- .../EmbeddedWebApplicationContextTests.java | 18 ++++++++++++-- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/EmbeddedWebApplicationContext.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/EmbeddedWebApplicationContext.java index 20dfac5fb8..e87daa4dc2 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/EmbeddedWebApplicationContext.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/EmbeddedWebApplicationContext.java @@ -18,13 +18,12 @@ package org.springframework.bootstrap.context.embedded; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.Comparator; import java.util.LinkedHashSet; import java.util.List; import java.util.Map.Entry; import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; import javax.servlet.Filter; import javax.servlet.Servlet; @@ -194,7 +193,7 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext if (initializers.isEmpty()) { - SortedSet> servletBeans = getOrderedBeansOfType(Servlet.class); + List> servletBeans = getOrderedBeansOfType(Servlet.class); for (Entry servletBean : servletBeans) { String url = (servletBeans.size() == 1 ? "/" : "/" + servletBean.getKey().toLowerCase() + "/*"); @@ -258,16 +257,17 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext } } - private SortedSet> getOrderedBeansOfType(Class type) { - SortedSet> beans = new TreeSet>( - new Comparator>() { - @Override - public int compare(Entry o1, Entry o2) { - return AnnotationAwareOrderComparator.INSTANCE.compare( - o1.getValue(), o2.getValue()); - } - }); + private List> getOrderedBeansOfType(Class type) { + List> beans = new ArrayList>(); + Comparator> comparator = new Comparator>() { + @Override + public int compare(Entry o1, Entry o2) { + return AnnotationAwareOrderComparator.INSTANCE.compare(o1.getValue(), + o2.getValue()); + } + }; beans.addAll(getBeanFactory().getBeansOfType(type, true, true).entrySet()); + Collections.sort(beans, comparator); return beans; } diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/embedded/EmbeddedWebApplicationContextTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/embedded/EmbeddedWebApplicationContextTests.java index f559f2947f..a79a335eed 100644 --- a/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/embedded/EmbeddedWebApplicationContextTests.java +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/embedded/EmbeddedWebApplicationContextTests.java @@ -33,8 +33,6 @@ import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.ConstructorArgumentValues; import org.springframework.beans.factory.support.RootBeanDefinition; -import org.springframework.bootstrap.context.embedded.EmbeddedWebApplicationContext; -import org.springframework.bootstrap.context.embedded.FilterRegistrationBean; import org.springframework.context.ApplicationContextException; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; @@ -244,6 +242,22 @@ public class EmbeddedWebApplicationContextTests { ordered.verify(initializer2).onStartup(servletContext); } + @Test + public void unorderedServletContextInitializerBeans() throws Exception { + addEmbeddedServletContainerFactoryBean(); + ServletContextInitializer initializer1 = mock(ServletContextInitializer.class); + ServletContextInitializer initializer2 = mock(ServletContextInitializer.class); + this.context.registerBeanDefinition("initializerBean2", + beanDefinition(initializer2)); + this.context.registerBeanDefinition("initializerBean1", + beanDefinition(initializer1)); + this.context.refresh(); + ServletContext servletContext = getEmbeddedServletContainerFactory() + .getServletContext(); + verify(initializer1).onStartup(servletContext); + verify(initializer2).onStartup(servletContext); + } + @Test public void servletContextInitializerBeansSkipsServletsAndFilters() throws Exception { addEmbeddedServletContainerFactoryBean();