From 8a964814234d4fd38890c882842bc3df54c8c84d Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 1 Sep 2015 09:15:40 +0200 Subject: [PATCH] Properly guard customization of application context class SpringApplication wrongly expects spring-web to be on the classpath to figure out whether or not the web environment should be enabled for a custom context class. We now properly guard this check so that the web environment is not enabled (read: not checked) if `spring-web` is not available. Closes gh-3856 --- .../boot/SpringApplication.java | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) 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 49285a2824..c011df0a7a 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -138,6 +138,7 @@ import org.springframework.web.context.support.StandardServletEnvironment; * @author Dave Syer * @author Andy Wilkinson * @author Christian Dupuis + * @author Stephane Nicoll * @see #run(Object, String[]) * @see #run(Object[], String[]) * @see #SpringApplication(Object...) @@ -226,21 +227,12 @@ public class SpringApplication { if (sources != null && sources.length > 0) { this.sources.addAll(Arrays.asList(sources)); } - this.webEnvironment = deduceWebEnvironment(); + this.webEnvironment = isSpringWebAvailable(); 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(); @@ -872,7 +864,8 @@ public class SpringApplication { public void setApplicationContextClass( Class applicationContextClass) { this.applicationContextClass = applicationContextClass; - if (!WebApplicationContext.class.isAssignableFrom(applicationContextClass)) { + if (isSpringWebAvailable() && !WebApplicationContext.class.isAssignableFrom( + applicationContextClass)) { this.webEnvironment = false; } } @@ -935,6 +928,15 @@ 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.