Extend DefaultDispatcherServletCondition to check for a registration

...bean with no explicit @Bean DispatcherServlet. We still have to check
by bean name (slightly unfortunate, but we need to avoid instantiating
too early) so there's now another magic
bean name for the registration bean ("dispatcherServletRegistration")
that the user has to replace if he wants the registration without
defining a servlet @Bean

Fixes gh-482
This commit is contained in:
Dave Syer
2014-03-13 09:33:29 +00:00
parent a71c9b5de7
commit 659d7b6df1
2 changed files with 81 additions and 1 deletions

View File

@@ -54,6 +54,21 @@ public class DispatcherServletAutoConfigurationTests {
assertEquals("[/]", registration.getUrlMappings().toString());
}
@Test
public void registrationOverride() throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
this.context.register(CustomDispatcherRegistration.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(0, this.context.getBeanNamesForType(DispatcherServlet.class).length);
}
@Test
public void servletPath() throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
@@ -95,4 +110,15 @@ public class DispatcherServletAutoConfigurationTests {
}
@Configuration
protected static class CustomDispatcherRegistration {
@Bean
public ServletRegistrationBean dispatcherServletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(
new DispatcherServlet(), "/foo");
registration.setName("customDispatcher");
return registration;
}
}
}