Commit 8a964814 authored by Stephane Nicoll's avatar Stephane Nicoll

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
parent cd39e6a7
...@@ -138,6 +138,7 @@ import org.springframework.web.context.support.StandardServletEnvironment; ...@@ -138,6 +138,7 @@ import org.springframework.web.context.support.StandardServletEnvironment;
* @author Dave Syer * @author Dave Syer
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Christian Dupuis * @author Christian Dupuis
* @author Stephane Nicoll
* @see #run(Object, String[]) * @see #run(Object, String[])
* @see #run(Object[], String[]) * @see #run(Object[], String[])
* @see #SpringApplication(Object...) * @see #SpringApplication(Object...)
...@@ -226,21 +227,12 @@ public class SpringApplication { ...@@ -226,21 +227,12 @@ public class SpringApplication {
if (sources != null && sources.length > 0) { if (sources != null && sources.length > 0) {
this.sources.addAll(Arrays.asList(sources)); this.sources.addAll(Arrays.asList(sources));
} }
this.webEnvironment = deduceWebEnvironment(); this.webEnvironment = isSpringWebAvailable();
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class)); setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass(); this.mainApplicationClass = deduceMainApplicationClass();
} }
private boolean deduceWebEnvironment() {
for (String className : WEB_ENVIRONMENT_CLASSES) {
if (!ClassUtils.isPresent(className, null)) {
return false;
}
}
return true;
}
private Class<?> deduceMainApplicationClass() { private Class<?> deduceMainApplicationClass() {
try { try {
StackTraceElement[] stackTrace = new RuntimeException().getStackTrace(); StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
...@@ -872,7 +864,8 @@ public class SpringApplication { ...@@ -872,7 +864,8 @@ public class SpringApplication {
public void setApplicationContextClass( public void setApplicationContextClass(
Class<? extends ConfigurableApplicationContext> applicationContextClass) { Class<? extends ConfigurableApplicationContext> applicationContextClass) {
this.applicationContextClass = applicationContextClass; this.applicationContextClass = applicationContextClass;
if (!WebApplicationContext.class.isAssignableFrom(applicationContextClass)) { if (isSpringWebAvailable() && !WebApplicationContext.class.isAssignableFrom(
applicationContextClass)) {
this.webEnvironment = false; this.webEnvironment = false;
} }
} }
...@@ -935,6 +928,15 @@ public class SpringApplication { ...@@ -935,6 +928,15 @@ public class SpringApplication {
return asUnmodifiableOrderedSet(this.listeners); 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 * Static helper that can be used to run a {@link SpringApplication} from the
* specified source using default settings. * specified source using default settings.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment