diff --git a/spring-boot-samples/spring-boot-sample-tomcat/src/main/java/org/springframework/boot/sample/tomcat/SampleTomcatApplication.java b/spring-boot-samples/spring-boot-sample-tomcat/src/main/java/org/springframework/boot/sample/tomcat/SampleTomcatApplication.java index 848c786d415..9f365f1dfa5 100644 --- a/spring-boot-samples/spring-boot-sample-tomcat/src/main/java/org/springframework/boot/sample/tomcat/SampleTomcatApplication.java +++ b/spring-boot-samples/spring-boot-sample-tomcat/src/main/java/org/springframework/boot/sample/tomcat/SampleTomcatApplication.java @@ -16,8 +16,14 @@ package org.springframework.boot.sample.tomcat; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @@ -26,8 +32,25 @@ import org.springframework.context.annotation.Configuration; @ComponentScan public class SampleTomcatApplication { + private static Log logger = LogFactory.getLog(SampleTomcatApplication.class); + public static void main(String[] args) throws Exception { SpringApplication.run(SampleTomcatApplication.class, args); } + @Bean + protected ServletContextListener listener() { + return new ServletContextListener() { + @Override + public void contextInitialized(ServletContextEvent sce) { + logger.info("ServletContext initialized"); + } + + @Override + public void contextDestroyed(ServletContextEvent sce) { + logger.info("ServletContext destroyed"); + } + }; + } + } 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 ad20cc577cf..5fd1cec2e4b 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 @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; +import java.util.EventListener; import java.util.LinkedHashSet; import java.util.List; import java.util.Map.Entry; @@ -207,6 +208,7 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext Set initializers = new LinkedHashSet(); Set servletRegistrations = new LinkedHashSet(); Set filterRegistrations = new LinkedHashSet(); + Set listenerRegistrations = new LinkedHashSet(); for (Entry initializerBean : getOrderedBeansOfType(ServletContextInitializer.class)) { ServletContextInitializer initializer = initializerBean.getValue(); @@ -219,6 +221,11 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext filterRegistrations.add(((FilterRegistrationBean) initializer) .getFilter()); } + if (initializer instanceof ServletListenerRegistrationBean) { + listenerRegistrations + .add(((ServletListenerRegistrationBean) initializer) + .getListener()); + } } List> servletBeans = getOrderedBeansOfType(Servlet.class); @@ -248,6 +255,17 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext } } + for (Entry listenerBean : getOrderedBeansOfType(EventListener.class)) { + String name = listenerBean.getKey(); + EventListener listener = listenerBean.getValue(); + if (!filterRegistrations.contains(listener)) { + ServletListenerRegistrationBean registration = new ServletListenerRegistrationBean( + listener); + registration.setName(name); + initializers.add(registration); + } + } + return initializers; } 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 new file mode 100644 index 00000000000..ab96e10ce10 --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/ServletListenerRegistrationBean.java @@ -0,0 +1,56 @@ +/* + * Copyright 2012-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.context.embedded; + +import java.util.EventListener; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; + +/** + * @author Dave Syer + */ +public class ServletListenerRegistrationBean extends + RegistrationBean { + + private T listener; + + /** + * @param listener the listener to register + */ + public ServletListenerRegistrationBean(T listener) { + super(); + this.listener = listener; + } + + /** + * @param listener the listener to register + */ + public void setListener(T listener) { + this.listener = listener; + } + + @Override + public void onStartup(ServletContext servletContext) throws ServletException { + servletContext.addListener(this.listener); + } + + public T getListener() { + return this.listener; + } + +} 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 new file mode 100644 index 00000000000..2899f34a0b8 --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/ServletListenerRegistrationBeanTests.java @@ -0,0 +1,60 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.context.embedded; + +import javax.servlet.ServletContext; +import javax.servlet.ServletContextListener; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +import static org.mockito.Mockito.verify; + +/** + * Tests for {@link ServletListenerRegistrationBean}. + * + * @author Dave Syer + */ +public class ServletListenerRegistrationBeanTests { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private ServletContextListener listener = Mockito.mock(ServletContextListener.class); + + @Mock + private ServletContext servletContext; + + @Before + public void setupMocks() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void startupWithDefaults() throws Exception { + ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean( + this.listener); + bean.onStartup(this.servletContext); + verify(this.servletContext).addListener(this.listener); + } + +}