From feb2452f06050e08aa6aba7754a7fa5d5cd00269 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 3 Sep 2015 10:33:53 -0700 Subject: [PATCH 1/3] Simplify WebApplicationContext class guard See gh-3856 --- .../simple/SampleSimpleApplication.java | 4 +++ .../boot/SpringApplication.java | 32 ++++++++++++------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/spring-boot-samples/spring-boot-sample-simple/src/main/java/sample/simple/SampleSimpleApplication.java b/spring-boot-samples/spring-boot-sample-simple/src/main/java/sample/simple/SampleSimpleApplication.java index 80604eba8d..7222ab5dd8 100644 --- a/spring-boot-samples/spring-boot-sample-simple/src/main/java/sample/simple/SampleSimpleApplication.java +++ b/spring-boot-samples/spring-boot-sample-simple/src/main/java/sample/simple/SampleSimpleApplication.java @@ -20,6 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import sample.simple.service.HelloWorldService; @@ -39,6 +40,9 @@ public class SampleSimpleApplication implements CommandLineRunner { } public static void main(String[] args) throws Exception { + SpringApplication application = new SpringApplication( + SampleSimpleApplication.class); + application.setApplicationContextClass(AnnotationConfigApplicationContext.class); SpringApplication.run(SampleSimpleApplication.class, args); } } diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index 9b5198a200..41e42ccb4b 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -227,12 +227,21 @@ public class SpringApplication { if (sources != null && sources.length > 0) { this.sources.addAll(Arrays.asList(sources)); } - this.webEnvironment = isSpringWebAvailable(); + this.webEnvironment = deduceWebEnvironment(); setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class)); setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); this.mainApplicationClass = deduceMainApplicationClass(); } + private boolean deduceWebEnvironment() { + for (String className : WEB_ENVIRONMENT_CLASSES) { + if (!ClassUtils.isPresent(className, null)) { + return false; + } + } + return true; + } + private Class deduceMainApplicationClass() { try { StackTraceElement[] stackTrace = new RuntimeException().getStackTrace(); @@ -864,12 +873,20 @@ public class SpringApplication { public void setApplicationContextClass( Class applicationContextClass) { this.applicationContextClass = applicationContextClass; - if (!isSpringWebAvailable() || !WebApplicationContext.class.isAssignableFrom( - applicationContextClass)) { + if (!isWebApplicationContext(applicationContextClass)) { this.webEnvironment = false; } } + private boolean isWebApplicationContext(Class applicationContextClass) { + try { + return WebApplicationContext.class.isAssignableFrom(applicationContextClass); + } + catch (NoClassDefFoundError ex) { + return false; + } + } + /** * Sets the {@link ApplicationContextInitializer} that will be applied to the Spring * {@link ApplicationContext}. @@ -928,15 +945,6 @@ public class SpringApplication { return asUnmodifiableOrderedSet(this.listeners); } - private boolean isSpringWebAvailable() { - for (String className : WEB_ENVIRONMENT_CLASSES) { - if (!ClassUtils.isPresent(className, null)) { - return false; - } - } - return true; - } - /** * Static helper that can be used to run a {@link SpringApplication} from the * specified source using default settings. From b4e0a772690130850698826b013a0b4bd95c3060 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 3 Sep 2015 10:39:49 -0700 Subject: [PATCH 2/3] Polish POM whitespace --- spring-boot-deployment-tests/pom.xml | 3 --- 1 file changed, 3 deletions(-) diff --git a/spring-boot-deployment-tests/pom.xml b/spring-boot-deployment-tests/pom.xml index ffbba6c40d..af27f3be1e 100644 --- a/spring-boot-deployment-tests/pom.xml +++ b/spring-boot-deployment-tests/pom.xml @@ -19,7 +19,6 @@ ${basedir}/.. 1.7 - 300000 ${user.home}/.cargo/installs @@ -28,7 +27,6 @@ spring-boot-deployment-test-tomcat spring-boot-deployment-test-wildfly - @@ -74,5 +72,4 @@ - From adf2c44bdda569ee3801ebae1569c625743027a2 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 3 Sep 2015 10:58:27 -0700 Subject: [PATCH 3/3] Set registerShutdownHook to false for tests Update SpringApplicationContextLoader so that setRegisterShutdownHook is false for tests. Fixes gh-3763 --- .../boot/test/SpringApplicationContextLoader.java | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java b/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java index ff49efee56..3d7560a315 100644 --- a/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java +++ b/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java @@ -82,6 +82,7 @@ public class SpringApplicationContextLoader extends AbstractContextLoader { throws Exception { assertValidAnnotations(config.getTestClass()); SpringApplication application = getSpringApplication(); + application.setRegisterShutdownHook(false); application.setMainApplicationClass(config.getTestClass()); application.setSources(getSources(config)); ConfigurableEnvironment environment = new StandardEnvironment();