Add ServletContextListener to embedded web app
* User can add @Bean of type EventListener (e.g. ServletContextListener) [Fixes #54112999] [bs-254]
This commit is contained in:
parent
3b9df63f73
commit
42bb793155
|
|
@ -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");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<ServletContextInitializer> initializers = new LinkedHashSet<ServletContextInitializer>();
|
||||
Set<Servlet> servletRegistrations = new LinkedHashSet<Servlet>();
|
||||
Set<Filter> filterRegistrations = new LinkedHashSet<Filter>();
|
||||
Set<EventListener> listenerRegistrations = new LinkedHashSet<EventListener>();
|
||||
|
||||
for (Entry<String, ServletContextInitializer> 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<Entry<String, Servlet>> servletBeans = getOrderedBeansOfType(Servlet.class);
|
||||
|
|
@ -248,6 +255,17 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
|
|||
}
|
||||
}
|
||||
|
||||
for (Entry<String, EventListener> listenerBean : getOrderedBeansOfType(EventListener.class)) {
|
||||
String name = listenerBean.getKey();
|
||||
EventListener listener = listenerBean.getValue();
|
||||
if (!filterRegistrations.contains(listener)) {
|
||||
ServletListenerRegistrationBean<EventListener> registration = new ServletListenerRegistrationBean<EventListener>(
|
||||
listener);
|
||||
registration.setName(name);
|
||||
initializers.add(registration);
|
||||
}
|
||||
}
|
||||
|
||||
return initializers;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<T extends EventListener> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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<ServletContextListener> bean = new ServletListenerRegistrationBean<ServletContextListener>(
|
||||
this.listener);
|
||||
bean.onStartup(this.servletContext);
|
||||
verify(this.servletContext).addListener(this.listener);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue