From 597e1c3cdeb6dffe58aea74dfd9459061cbf9703 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 20 May 2015 13:43:38 +0100 Subject: [PATCH] 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 --- .../web/SpringBootServletInitializer.java | 14 +++++++- .../SpringBootServletInitializerTests.java | 36 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/context/web/SpringBootServletInitializer.java b/spring-boot/src/main/java/org/springframework/boot/context/web/SpringBootServletInitializer.java index e5ae60fd204..9bc3ae6da46 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/web/SpringBootServletInitializer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/web/SpringBootServletInitializer.java @@ -83,7 +83,7 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit protected WebApplicationContext createRootApplicationContext( ServletContext servletContext) { - SpringApplicationBuilder builder = new SpringApplicationBuilder(); + SpringApplicationBuilder builder = createSpringApplicationBuilder(); ApplicationContext parent = getExistingRootWebApplicationContext(servletContext); if (parent != null) { this.logger.info("Root context already created (using as parent)."); @@ -108,6 +108,18 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit 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}. * @param application the application to run diff --git a/spring-boot/src/test/java/org/springframework/boot/context/web/SpringBootServletInitializerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/web/SpringBootServletInitializerTests.java index 595b2aac5e5..2751efeb5b5 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/web/SpringBootServletInitializerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/web/SpringBootServletInitializerTests.java @@ -33,6 +33,7 @@ import org.springframework.mock.web.MockServletContext; import org.springframework.web.context.WebApplicationContext; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; /** @@ -72,6 +73,13 @@ public class SpringBootServletInitializerTests { 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> equalToSet(Object... items) { Set set = new LinkedHashSet(); 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 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(); + } + + } + }