Allow default DispatcherServlet to be switched off more easily

All a user has to do now is declare a bean with name "dispatcherServlet".
pull/165/head
Dave Syer 11 years ago
parent b05ffd1164
commit 9c2b34f188

@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.web; package org.springframework.boot.autoconfigure.web;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@ -72,15 +73,26 @@ public class DispatcherServletAutoConfiguration {
AnnotatedTypeMetadata metadata) { AnnotatedTypeMetadata metadata) {
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
String[] beans = beanFactory.getBeanNamesForType(DispatcherServlet.class, List<String> servlets = Arrays.asList(beanFactory.getBeanNamesForType(
false, false); DispatcherServlet.class, false, false));
if (beans.length == 0) { boolean containsDispatcherBean = beanFactory
.containsBean(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
if (servlets.isEmpty()) {
if (containsDispatcherBean) {
return ConditionOutcome
.noMatch("found no DispatcherServlet but a non-DispatcherServlet named "
+ DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
}
return ConditionOutcome.match("no DispatcherServlet found"); return ConditionOutcome.match("no DispatcherServlet found");
} }
if (Arrays.asList(beans).contains(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)) { if (servlets.contains(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)) {
return ConditionOutcome.noMatch("found DispatcherServlet named " return ConditionOutcome.noMatch("found DispatcherServlet named "
+ DEFAULT_DISPATCHER_SERVLET_BEAN_NAME); + DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
} }
if (containsDispatcherBean) {
return ConditionOutcome.noMatch("found non-DispatcherServlet named "
+ DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
}
return ConditionOutcome return ConditionOutcome
.match("one or more DispatcherServlets found and none is named " .match("one or more DispatcherServlets found and none is named "
+ DEFAULT_DISPATCHER_SERVLET_BEAN_NAME); + DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);

@ -17,6 +17,8 @@
package org.springframework.boot.autoconfigure.web; package org.springframework.boot.autoconfigure.web;
import javax.servlet.Servlet; import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
@ -32,6 +34,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.FrameworkServlet;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
@ -75,6 +78,27 @@ public class EmbeddedServletContainerAutoConfigurationTests {
assertEquals(2, this.context.getBeanNamesForType(DispatcherServlet.class).length); assertEquals(2, this.context.getBeanNamesForType(DispatcherServlet.class).length);
} }
@Test
public void contextAlreadyHasNonDispatcherServlet() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
NonSpringServletConfiguration.class,
EmbeddedContainerConfiguration.class,
EmbeddedServletContainerAutoConfiguration.class,
DispatcherServletAutoConfiguration.class);
verifyContext(); // the non default servlet is still registered
assertEquals(0, this.context.getBeanNamesForType(DispatcherServlet.class).length);
}
@Test
public void contextAlreadyHasNonServlet() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
NonServletConfiguration.class, EmbeddedContainerConfiguration.class,
EmbeddedServletContainerAutoConfiguration.class,
DispatcherServletAutoConfiguration.class);
assertEquals(0, this.context.getBeanNamesForType(DispatcherServlet.class).length);
assertEquals(0, this.context.getBeanNamesForType(Servlet.class).length);
}
@Test @Test
public void contextAlreadyHasDispatcherServletAndRegistration() throws Exception { public void contextAlreadyHasDispatcherServletAndRegistration() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext( this.context = new AnnotationConfigEmbeddedWebApplicationContext(
@ -151,6 +175,31 @@ public class EmbeddedServletContainerAutoConfigurationTests {
} }
@Configuration
public static class NonSpringServletConfiguration {
@Bean
public Servlet dispatcherServlet() {
return new FrameworkServlet() {
@Override
protected void doService(HttpServletRequest request,
HttpServletResponse response) throws Exception {
}
};
}
}
@Configuration
public static class NonServletConfiguration {
@Bean
public String dispatcherServlet() {
return "foo";
}
}
@Configuration @Configuration
public static class DispatcherServletWithRegistrationConfiguration { public static class DispatcherServletWithRegistrationConfiguration {

Loading…
Cancel
Save