Skip to content

Commit 8a96481

Browse files
committed
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 spring-projectsgh-3856
1 parent cd39e6a commit 8a96481

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

+13-11
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
* @author Dave Syer
139139
* @author Andy Wilkinson
140140
* @author Christian Dupuis
141+
* @author Stephane Nicoll
141142
* @see #run(Object, String[])
142143
* @see #run(Object[], String[])
143144
* @see #SpringApplication(Object...)
@@ -226,21 +227,12 @@ private void initialize(Object[] sources) {
226227
if (sources != null && sources.length > 0) {
227228
this.sources.addAll(Arrays.asList(sources));
228229
}
229-
this.webEnvironment = deduceWebEnvironment();
230+
this.webEnvironment = isSpringWebAvailable();
230231
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
231232
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
232233
this.mainApplicationClass = deduceMainApplicationClass();
233234
}
234235

235-
private boolean deduceWebEnvironment() {
236-
for (String className : WEB_ENVIRONMENT_CLASSES) {
237-
if (!ClassUtils.isPresent(className, null)) {
238-
return false;
239-
}
240-
}
241-
return true;
242-
}
243-
244236
private Class<?> deduceMainApplicationClass() {
245237
try {
246238
StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
@@ -872,7 +864,8 @@ public void setResourceLoader(ResourceLoader resourceLoader) {
872864
public void setApplicationContextClass(
873865
Class<? extends ConfigurableApplicationContext> applicationContextClass) {
874866
this.applicationContextClass = applicationContextClass;
875-
if (!WebApplicationContext.class.isAssignableFrom(applicationContextClass)) {
867+
if (isSpringWebAvailable() && !WebApplicationContext.class.isAssignableFrom(
868+
applicationContextClass)) {
876869
this.webEnvironment = false;
877870
}
878871
}
@@ -935,6 +928,15 @@ public Set<ApplicationListener<?>> getListeners() {
935928
return asUnmodifiableOrderedSet(this.listeners);
936929
}
937930

931+
private boolean isSpringWebAvailable() {
932+
for (String className : WEB_ENVIRONMENT_CLASSES) {
933+
if (!ClassUtils.isPresent(className, null)) {
934+
return false;
935+
}
936+
}
937+
return true;
938+
}
939+
938940
/**
939941
* Static helper that can be used to run a {@link SpringApplication} from the
940942
* specified source using default settings.

0 commit comments

Comments
 (0)