diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java index 5fd1cec2e4b..667aa843be0 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java @@ -258,7 +258,8 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext for (Entry listenerBean : getOrderedBeansOfType(EventListener.class)) { String name = listenerBean.getKey(); EventListener listener = listenerBean.getValue(); - if (!filterRegistrations.contains(listener)) { + if (ServletListenerRegistrationBean.isSupportedType(listener) + && !filterRegistrations.contains(listener)) { ServletListenerRegistrationBean registration = new ServletListenerRegistrationBean( listener); registration.setName(name); diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/RegistrationBean.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/RegistrationBean.java index ebc29f81f5f..e5b5e84d5ea 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/RegistrationBean.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/RegistrationBean.java @@ -25,12 +25,12 @@ import org.springframework.core.Conventions; import org.springframework.util.Assert; /** - * Base class for {@link ServletRegistrationBean servlet} and - * {@link FilterRegistrationBean filter} registration beans. + * Base class for Servlet 3.0+ based registration beans. * * @author Phillip Webb * @see ServletRegistrationBean * @see FilterRegistrationBean + * @see ServletListenerRegistrationBean */ public abstract class RegistrationBean implements ServletContextInitializer { diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/ServletListenerRegistrationBean.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/ServletListenerRegistrationBean.java index ab96e10ce10..066b7001e79 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/ServletListenerRegistrationBean.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/ServletListenerRegistrationBean.java @@ -16,31 +16,82 @@ package org.springframework.boot.context.embedded; +import java.util.Collections; import java.util.EventListener; +import java.util.HashSet; +import java.util.Set; import javax.servlet.ServletContext; +import javax.servlet.ServletContextAttributeListener; +import javax.servlet.ServletContextListener; import javax.servlet.ServletException; +import javax.servlet.ServletRequestAttributeListener; +import javax.servlet.ServletRequestListener; +import javax.servlet.http.HttpSessionAttributeListener; +import javax.servlet.http.HttpSessionListener; + +import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; /** + * A {@link ServletContextInitializer} to register {@link EventListener}s in a Servlet + * 3.0+ container. Similar to the {@link ServletContext#addListener(EventListener) + * registration} features provided by {@link ServletContext} but with a Spring Bean + * friendly design. + * + * This bean can be used to register the following types of listener: + *
    + *
  • {@link ServletContextAttributeListener}
  • + *
  • {@link ServletRequestListener}
  • + *
  • {@link ServletRequestAttributeListener}
  • + *
  • {@link HttpSessionAttributeListener}
  • + *
  • {@link HttpSessionListener}
  • + *
  • {@link ServletContextListener}
  • + *
* @author Dave Syer + * @author Phillip Webb + * @param the type of listener */ public class ServletListenerRegistrationBean extends RegistrationBean { + private static final Set> SUPPORTED_TYPES; + static { + Set> types = new HashSet>(); + types.add(ServletContextAttributeListener.class); + types.add(ServletRequestListener.class); + types.add(ServletRequestAttributeListener.class); + types.add(HttpSessionAttributeListener.class); + types.add(HttpSessionListener.class); + types.add(ServletContextListener.class); + SUPPORTED_TYPES = Collections.unmodifiableSet(types); + } + private T listener; /** + * Create a new {@link ServletListenerRegistrationBean} instance. + */ + public ServletListenerRegistrationBean() { + } + + /** + * Create a new {@link ServletListenerRegistrationBean} instance. * @param listener the listener to register */ public ServletListenerRegistrationBean(T listener) { - super(); + Assert.notNull(listener, "Listener must not be null"); + Assert.isTrue(isSupportedType(listener), "EventLisener is not a supported type"); this.listener = listener; } /** + * Set the listener that will be registered. * @param listener the listener to register */ public void setListener(T listener) { + Assert.notNull(listener, "Listener must not be null"); + Assert.isTrue(isSupportedType(listener), "EventLisener is not a supported type"); this.listener = listener; } @@ -53,4 +104,18 @@ public class ServletListenerRegistrationBean extends return this.listener; } + /** + * Returns {@code true} if the specified listener is one of the supported types. + * @param listener the listener to test + * @return if the listener is of a supported type + */ + public static boolean isSupportedType(EventListener listener) { + for (Class type : SUPPORTED_TYPES) { + if (ClassUtils.isAssignableValue(type, listener)) { + return true; + } + } + return false; + } + } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializerTests.java index 6c2ad9b69c9..7e5014430c0 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializerTests.java @@ -20,7 +20,6 @@ import java.util.HashMap; import java.util.Map; import org.junit.Test; -import org.springframework.boot.context.initializer.ConfigFileApplicationContextInitializer; import org.springframework.context.support.StaticApplicationContext; import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.SimpleCommandLinePropertySource; @@ -132,4 +131,9 @@ public class ConfigFileApplicationContextInitializerTests { assertThat(property, equalTo("fromspecificlocation")); } + @Test + public void defaultApplicationProperties() throws Exception { + + } + }