Ensure only supported listeners are instantiated

Previously all EventListeners were eagerly instantiated
but that can cause problems because it happens quite early
in the lifecycle. Better to be explicit about the
supported types.
pull/138/head
Dave Syer 11 years ago
parent aefec4c16a
commit d1dcb015b6

@ -47,6 +47,7 @@ import org.springframework.core.io.ResourceLoader;
import org.springframework.format.Formatter; import org.springframework.format.Formatter;
import org.springframework.format.FormatterRegistry; import org.springframework.format.FormatterRegistry;
import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.filter.HiddenHttpMethodFilter; import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.View; import org.springframework.web.servlet.View;
@ -134,6 +135,12 @@ public class WebMvcAutoConfiguration {
return resolver; return resolver;
} }
@Bean
@ConditionalOnMissingBean(RequestContextListener.class)
public RequestContextListener requestContextListener() {
return new RequestContextListener();
}
@Bean @Bean
@ConditionalOnBean(View.class) @ConditionalOnBean(View.class)
public BeanNameViewResolver beanNameViewResolver() { public BeanNameViewResolver beanNameViewResolver() {

@ -276,15 +276,18 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
} }
} }
for (Entry<String, EventListener> listenerBean : getOrderedBeansOfType(EventListener.class)) { Set<Class<?>> listenerTypes = ServletListenerRegistrationBean.getSupportedTypes();
String name = listenerBean.getKey(); for (Class<?> type : listenerTypes) {
EventListener listener = listenerBean.getValue(); for (Entry<String, ?> listenerBean : getOrderedBeansOfType(type)) {
if (ServletListenerRegistrationBean.isSupportedType(listener) String name = listenerBean.getKey();
&& !filterRegistrations.contains(listener)) { EventListener listener = (EventListener) listenerBean.getValue();
ServletListenerRegistrationBean<EventListener> registration = new ServletListenerRegistrationBean<EventListener>( if (ServletListenerRegistrationBean.isSupportedType(listener)
listener); && !filterRegistrations.contains(listener)) {
registration.setName(name); ServletListenerRegistrationBean<EventListener> registration = new ServletListenerRegistrationBean<EventListener>(
initializers.add(registration); listener);
registration.setName(name);
initializers.add(registration);
}
} }
} }

@ -118,4 +118,11 @@ public class ServletListenerRegistrationBean<T extends EventListener> extends
return false; return false;
} }
/**
* @return the supportedTypes for this registration
*/
public static Set<Class<?>> getSupportedTypes() {
return SUPPORTED_TYPES;
}
} }

@ -22,6 +22,7 @@ import java.util.Properties;
import javax.servlet.Filter; import javax.servlet.Filter;
import javax.servlet.Servlet; import javax.servlet.Servlet;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletContextListener;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
@ -264,6 +265,18 @@ public class EmbeddedWebApplicationContextTests {
ordered.verify(initializer2).onStartup(servletContext); ordered.verify(initializer2).onStartup(servletContext);
} }
@Test
public void servletContextListenerBeans() throws Exception {
addEmbeddedServletContainerFactoryBean();
ServletContextListener initializer = mock(ServletContextListener.class);
this.context.registerBeanDefinition("initializerBean",
beanDefinition(initializer));
this.context.refresh();
ServletContext servletContext = getEmbeddedServletContainerFactory()
.getServletContext();
verify(servletContext).addListener(initializer);
}
@Test @Test
public void unorderedServletContextInitializerBeans() throws Exception { public void unorderedServletContextInitializerBeans() throws Exception {
addEmbeddedServletContainerFactoryBean(); addEmbeddedServletContainerFactoryBean();

Loading…
Cancel
Save