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
pull/3886/head
Stephane Nicoll 9 years ago
parent cd39e6a742
commit 8a96481423

@ -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<? extends ConfigurableApplicationContext> 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.

Loading…
Cancel
Save