Allow default DispatcherServlet to be switched off more easily

All a user has to do now is declare a bean with name "dispatcherServlet".
This commit is contained in:
Dave Syer 2013-12-12 16:45:07 +00:00
parent b05ffd1164
commit 9c2b34f188
2 changed files with 65 additions and 4 deletions

View File

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

View File

@ -17,6 +17,8 @@
package org.springframework.boot.autoconfigure.web;
import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Test;
import org.springframework.beans.BeansException;
@ -32,6 +34,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.FrameworkServlet;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
@ -75,6 +78,27 @@ public class EmbeddedServletContainerAutoConfigurationTests {
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
public void contextAlreadyHasDispatcherServletAndRegistration() throws Exception {
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
public static class DispatcherServletWithRegistrationConfiguration {