diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/FilterRegistrationBean.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/FilterRegistrationBean.java index 493f5e2ab8..af56ac60ad 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/FilterRegistrationBean.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/FilterRegistrationBean.java @@ -231,6 +231,10 @@ public class FilterRegistrationBean extends RegistrationBean { public void onStartup(ServletContext servletContext) throws ServletException { Assert.notNull(this.filter, "Filter must not be null"); String name = getOrDeduceName(this.filter); + if (!isEnabled()) { + logger.info("Filter " + name + " was not registered (disabled)"); + return; + } FilterRegistration.Dynamic added = servletContext.addFilter(name, this.filter); if (added == null) { logger.info("Filter " + name + " was not registered " 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 30ecf3f05f..99b58b4ae9 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 @@ -41,6 +41,8 @@ public abstract class RegistrationBean implements ServletContextInitializer, Ord private boolean asyncSupported = true; + private boolean enabled = true; + private Map initParameters = new LinkedHashMap(); /** @@ -66,6 +68,22 @@ public abstract class RegistrationBean implements ServletContextInitializer, Ord return this.asyncSupported; } + /** + * Flag to indicate that the registration is enabled. + * + * @param enabled the enabled to set + */ + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + /** + * @return the enabled flag (default true) + */ + public boolean isEnabled() { + return this.enabled; + } + /** * Set init-parameters for this registration. Calling this method will replace any * existing init-parameters. 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 bababae8b7..fca2b7b0b8 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 @@ -30,6 +30,8 @@ import javax.servlet.ServletRequestListener; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionListener; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; @@ -55,6 +57,8 @@ import org.springframework.util.ClassUtils; public class ServletListenerRegistrationBean extends RegistrationBean { + private static Log logger = LogFactory.getLog(ServletListenerRegistrationBean.class); + private static final Set> SUPPORTED_TYPES; static { Set> types = new HashSet>(); @@ -97,6 +101,10 @@ public class ServletListenerRegistrationBean extends @Override public void onStartup(ServletContext servletContext) throws ServletException { + if (!isEnabled()) { + logger.info("Listener " + this.listener + " was not registered (disabled)"); + return; + } servletContext.addListener(this.listener); } diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/ServletRegistrationBean.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/ServletRegistrationBean.java index 419787bac9..25c9a63172 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/ServletRegistrationBean.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/ServletRegistrationBean.java @@ -158,6 +158,10 @@ public class ServletRegistrationBean extends RegistrationBean { public void onStartup(ServletContext servletContext) throws ServletException { Assert.notNull(this.servlet, "Servlet must not be null"); String name = getServletName(); + if (!isEnabled()) { + logger.info("Filter " + name + " was not registered (disabled)"); + return; + } logger.info("Mapping servlet: '" + name + "' to " + this.urlMappings); Dynamic added = servletContext.addServlet(name, this.servlet); if (added == null) { diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/FilterRegistrationBeanTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/FilterRegistrationBeanTests.java index 8484171bb4..b92147c554 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/FilterRegistrationBeanTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/FilterRegistrationBeanTests.java @@ -36,6 +36,7 @@ import org.mockito.MockitoAnnotations; import static org.mockito.BDDMockito.given; import static org.mockito.Matchers.anyObject; import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; /** @@ -122,6 +123,15 @@ public class FilterRegistrationBeanTests { verify(this.servletContext).addFilter("mockFilter", this.filter); } + @Test + public void disable() throws Exception { + FilterRegistrationBean bean = new FilterRegistrationBean(); + bean.setFilter(this.filter); + bean.setEnabled(false); + bean.onStartup(this.servletContext); + verify(this.servletContext, times(0)).addFilter("mockFilter", this.filter); + } + @Test public void setFilterMustNotBeNull() throws Exception { FilterRegistrationBean bean = new FilterRegistrationBean(); diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/ServletListenerRegistrationBeanTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/ServletListenerRegistrationBeanTests.java index e87c4fd784..6433474714 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/ServletListenerRegistrationBeanTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/ServletListenerRegistrationBeanTests.java @@ -29,6 +29,8 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; /** @@ -60,6 +62,16 @@ public class ServletListenerRegistrationBeanTests { verify(this.servletContext).addListener(this.listener); } + @Test + public void disable() throws Exception { + ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean( + this.listener); + bean.setEnabled(false); + bean.onStartup(this.servletContext); + verify(this.servletContext, times(0)).addListener( + any(ServletContextListener.class)); + } + @Test public void cannotRegisterUnsupportedType() throws Exception { this.thrown.expect(IllegalArgumentException.class); diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/ServletRegistrationBeanTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/ServletRegistrationBeanTests.java index 631b5e4be8..c075501588 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/ServletRegistrationBeanTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/ServletRegistrationBeanTests.java @@ -39,6 +39,7 @@ import static org.mockito.BDDMockito.given; import static org.mockito.Matchers.anyObject; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; /** @@ -129,6 +130,15 @@ public class ServletRegistrationBeanTests { verify(this.servletContext).addServlet("mockServlet", this.servlet); } + @Test + public void disable() throws Exception { + ServletRegistrationBean bean = new ServletRegistrationBean(); + bean.setServlet(this.servlet); + bean.setEnabled(false); + bean.onStartup(this.servletContext); + verify(this.servletContext, times(0)).addServlet("mockServlet", this.servlet); + } + @Test public void setServletMustNotBeNull() throws Exception { ServletRegistrationBean bean = new ServletRegistrationBean();