Commit 85a56a79 authored by Dave Syer's avatar Dave Syer

Tidy up implementation of DispatcherServletAutoConfiguration

also adds another test.
parent 659d7b6d
...@@ -113,7 +113,10 @@ public class DispatcherServletAutoConfiguration { ...@@ -113,7 +113,10 @@ public class DispatcherServletAutoConfiguration {
return checkServletRegistrations(beanFactory); return checkServletRegistrations(beanFactory);
} }
private ConditionOutcome checkServlets(ConfigurableListableBeanFactory beanFactory) { }
private static ConditionOutcome checkServlets(
ConfigurableListableBeanFactory beanFactory) {
List<String> servlets = Arrays.asList(beanFactory.getBeanNamesForType( List<String> servlets = Arrays.asList(beanFactory.getBeanNamesForType(
DispatcherServlet.class, false, false)); DispatcherServlet.class, false, false));
boolean containsDispatcherBean = beanFactory boolean containsDispatcherBean = beanFactory
...@@ -141,9 +144,7 @@ public class DispatcherServletAutoConfiguration { ...@@ -141,9 +144,7 @@ public class DispatcherServletAutoConfiguration {
} }
/** private static ConditionOutcome checkServletRegistrations(
*/
protected ConditionOutcome checkServletRegistrations(
ConfigurableListableBeanFactory beanFactory) { ConfigurableListableBeanFactory beanFactory) {
List<String> registrations = Arrays.asList(beanFactory.getBeanNamesForType( List<String> registrations = Arrays.asList(beanFactory.getBeanNamesForType(
...@@ -165,8 +166,7 @@ public class DispatcherServletAutoConfiguration { ...@@ -165,8 +166,7 @@ public class DispatcherServletAutoConfiguration {
+ DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME); + DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);
} }
if (containsDispatcherRegistrationBean) { if (containsDispatcherRegistrationBean) {
return ConditionOutcome return ConditionOutcome.noMatch("found non-ServletRegistrationBean named "
.noMatch("found non-ServletRegistrationBean named "
+ DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME); + DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);
} }
...@@ -175,7 +175,4 @@ public class DispatcherServletAutoConfiguration { ...@@ -175,7 +175,4 @@ public class DispatcherServletAutoConfiguration {
+ DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME); + DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);
} }
}
} }
...@@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.web; ...@@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.web;
import javax.servlet.MultipartConfigElement; import javax.servlet.MultipartConfigElement;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.UnsatisfiedDependencyException;
import org.springframework.boot.context.embedded.MultiPartConfigFactory; import org.springframework.boot.context.embedded.MultiPartConfigFactory;
import org.springframework.boot.context.embedded.ServletRegistrationBean; import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
...@@ -69,6 +70,23 @@ public class DispatcherServletAutoConfigurationTests { ...@@ -69,6 +70,23 @@ public class DispatcherServletAutoConfigurationTests {
assertEquals(0, this.context.getBeanNamesForType(DispatcherServlet.class).length); assertEquals(0, this.context.getBeanNamesForType(DispatcherServlet.class).length);
} }
// If you override either the dispatcherServlet or its registration you have to
// provide both...
@Test(expected = UnsatisfiedDependencyException.class)
public void registrationOverrideWithAutowiredServlet() throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
this.context.register(CustomAutowiredRegistration.class,
ServerPropertiesAutoConfiguration.class,
DispatcherServletAutoConfiguration.class);
this.context.setServletContext(new MockServletContext());
this.context.refresh();
ServletRegistrationBean registration = this.context
.getBean(ServletRegistrationBean.class);
assertEquals("[/foo]", registration.getUrlMappings().toString());
assertEquals("customDispatcher", registration.getServletName());
assertEquals(1, this.context.getBeanNamesForType(DispatcherServlet.class).length);
}
@Test @Test
public void servletPath() throws Exception { public void servletPath() throws Exception {
this.context = new AnnotationConfigWebApplicationContext(); this.context = new AnnotationConfigWebApplicationContext();
...@@ -121,4 +139,16 @@ public class DispatcherServletAutoConfigurationTests { ...@@ -121,4 +139,16 @@ public class DispatcherServletAutoConfigurationTests {
} }
} }
@Configuration
protected static class CustomAutowiredRegistration {
@Bean
public ServletRegistrationBean dispatcherServletRegistration(
DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(
dispatcherServlet, "/foo");
registration.setName("customDispatcher");
return registration;
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment