Allow custom builder to be used in SpringBootServletInitializer

Previously, to use a custom SpringApplicationBuilder subclass in
SpringBootServletInitializer, it was necessary to either override
createRootApplicationContext or configure and duplicate the logic
that configures the builder with initializers, the context class, etc.

This commit introduces a new method, createSpringApplicationBuilder,
that can be overridden to use a custom builder without having to
duplicate any configuration logic.

Closes gh-2694
pull/3022/head
Andy Wilkinson 10 years ago
parent 4ca2f78f8f
commit 597e1c3cde

@ -83,7 +83,7 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit
protected WebApplicationContext createRootApplicationContext( protected WebApplicationContext createRootApplicationContext(
ServletContext servletContext) { ServletContext servletContext) {
SpringApplicationBuilder builder = new SpringApplicationBuilder(); SpringApplicationBuilder builder = createSpringApplicationBuilder();
ApplicationContext parent = getExistingRootWebApplicationContext(servletContext); ApplicationContext parent = getExistingRootWebApplicationContext(servletContext);
if (parent != null) { if (parent != null) {
this.logger.info("Root context already created (using as parent)."); this.logger.info("Root context already created (using as parent).");
@ -108,6 +108,18 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit
return run(application); return run(application);
} }
/**
* Returns the {@code SpringApplicationBuilder} that is used to configure and create
* the {@link SpringApplication}. The default implementation returns a new
* {@code SpringApplicationBuilder} in its default state.
*
* @return the {@code SpringApplicationBuilder}.
* @since 1.3.0
*/
protected SpringApplicationBuilder createSpringApplicationBuilder() {
return new SpringApplicationBuilder();
}
/** /**
* Called to run a fully configured {@link SpringApplication}. * Called to run a fully configured {@link SpringApplication}.
* @param application the application to run * @param application the application to run

@ -33,6 +33,7 @@ import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
/** /**
@ -72,6 +73,13 @@ public class SpringBootServletInitializerTests {
equalToSet(Config.class, ErrorPageFilter.class)); equalToSet(Config.class, ErrorPageFilter.class));
} }
@Test
public void applicationBuilderCanBeCustomized() throws Exception {
CustomSpringBootServletInitializer servletInitializer = new CustomSpringBootServletInitializer();
servletInitializer.createRootApplicationContext(this.servletContext);
assertThat(servletInitializer.applicationBuilder.built, is(true));
}
private Matcher<? super Set<Object>> equalToSet(Object... items) { private Matcher<? super Set<Object>> equalToSet(Object... items) {
Set<Object> set = new LinkedHashSet<Object>(); Set<Object> set = new LinkedHashSet<Object>();
Collections.addAll(set, items); Collections.addAll(set, items);
@ -88,6 +96,22 @@ public class SpringBootServletInitializerTests {
} }
private class CustomSpringBootServletInitializer extends
MockSpringBootServletInitializer {
private final CustomSpringApplicationBuilder applicationBuilder = new CustomSpringApplicationBuilder();
@Override
protected SpringApplicationBuilder createSpringApplicationBuilder() {
return this.applicationBuilder;
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Config.class);
}
}
@Configuration @Configuration
public class WithConfigurationAnnotation extends MockSpringBootServletInitializer { public class WithConfigurationAnnotation extends MockSpringBootServletInitializer {
} }
@ -106,4 +130,16 @@ public class SpringBootServletInitializerTests {
} }
private static class CustomSpringApplicationBuilder extends SpringApplicationBuilder {
private boolean built;
@Override
public SpringApplication build() {
this.built = true;
return super.build();
}
}
} }

Loading…
Cancel
Save