Ensure DispatcherServlet is available in a deployed WAR

The DispatcherServletAutoConfiguration had a condition on it that
meant it wasn't used at all if there was no EmbeddedServletContainerFactory.
It's amazing that any WAR ever deployed.

Also added some info logging to servlet and filter registrations.
pull/382/head
Dave Syer 11 years ago
parent d984cc0826
commit 6631dd025c

@ -23,11 +23,9 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ConditionContext;
@ -51,7 +49,6 @@ import org.springframework.web.servlet.DispatcherServlet;
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass(DispatcherServlet.class)
@ConditionalOnBean(EmbeddedServletContainerFactory.class)
@AutoConfigureAfter(EmbeddedServletContainerAutoConfiguration.class)
public class DispatcherServletAutoConfiguration {

@ -234,12 +234,12 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
ServletContextInitializer initializer = initializerBean.getValue();
initializers.add(initializer);
if (initializer instanceof ServletRegistrationBean) {
servletRegistrations.add(((ServletRegistrationBean) initializer)
.getServlet());
ServletRegistrationBean servlet = (ServletRegistrationBean) initializer;
servletRegistrations.add(servlet.getServlet());
}
if (initializer instanceof FilterRegistrationBean) {
filterRegistrations.add(((FilterRegistrationBean) initializer)
.getFilter());
FilterRegistrationBean filter = (FilterRegistrationBean) initializer;
filterRegistrations.add(filter.getFilter());
}
if (initializer instanceof ServletListenerRegistrationBean) {
listenerRegistrations

@ -28,6 +28,8 @@ import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
/**
@ -48,6 +50,8 @@ import org.springframework.util.Assert;
*/
public class FilterRegistrationBean extends RegistrationBean {
private static Log logger = LogFactory.getLog(FilterRegistrationBean.class);
static final EnumSet<DispatcherType> ASYNC_DISPATCHER_TYPES = EnumSet.of(
DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.REQUEST,
DispatcherType.ASYNC);
@ -248,15 +252,21 @@ public class FilterRegistrationBean extends RegistrationBean {
servletNames.addAll(this.servletNames);
if (servletNames.isEmpty() && this.urlPatterns.isEmpty()) {
logger.info("Mapping filter: '" + registration.getName() + "' to: "
+ Arrays.asList(DEFAULT_URL_MAPPINGS));
registration.addMappingForUrlPatterns(dispatcherTypes, this.matchAfter,
DEFAULT_URL_MAPPINGS);
}
else {
if (servletNames.size() > 0) {
logger.info("Mapping filter: '" + registration.getName()
+ "' to servlets: " + servletNames);
registration.addMappingForServletNames(dispatcherTypes, this.matchAfter,
servletNames.toArray(new String[servletNames.size()]));
}
if (this.urlPatterns.size() > 0) {
logger.info("Mapping filter: '" + registration.getName() + "' to urls: "
+ this.urlPatterns);
registration.addMappingForUrlPatterns(dispatcherTypes, this.matchAfter,
this.urlPatterns.toArray(new String[this.urlPatterns.size()]));
}

@ -27,6 +27,8 @@ import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
/**
@ -45,6 +47,8 @@ import org.springframework.util.Assert;
*/
public class ServletRegistrationBean extends RegistrationBean {
private static Log logger = LogFactory.getLog(ServletRegistrationBean.class);
private static final String[] DEFAULT_MAPPINGS = { "/*" };
private Servlet servlet;
@ -152,6 +156,7 @@ public class ServletRegistrationBean extends RegistrationBean {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
Assert.notNull(this.servlet, "Servlet must not be null");
logger.info("Mapping servlet: '" + getServletName() + "' to " + this.urlMappings);
configure(servletContext.addServlet(getServletName(), this.servlet));
}

Loading…
Cancel
Save