Skip to content

Commit feb2452

Browse files
committed
Simplify WebApplicationContext class guard
See gh-3856
1 parent cb4e709 commit feb2452

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

spring-boot-samples/spring-boot-sample-simple/src/main/java/sample/simple/SampleSimpleApplication.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.springframework.boot.CommandLineRunner;
2121
import org.springframework.boot.SpringApplication;
2222
import org.springframework.boot.autoconfigure.SpringBootApplication;
23+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2324

2425
import sample.simple.service.HelloWorldService;
2526

@@ -39,6 +40,9 @@ public void run(String... args) {
3940
}
4041

4142
public static void main(String[] args) throws Exception {
43+
SpringApplication application = new SpringApplication(
44+
SampleSimpleApplication.class);
45+
application.setApplicationContextClass(AnnotationConfigApplicationContext.class);
4246
SpringApplication.run(SampleSimpleApplication.class, args);
4347
}
4448
}

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,21 @@ private void initialize(Object[] sources) {
227227
if (sources != null && sources.length > 0) {
228228
this.sources.addAll(Arrays.asList(sources));
229229
}
230-
this.webEnvironment = isSpringWebAvailable();
230+
this.webEnvironment = deduceWebEnvironment();
231231
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
232232
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
233233
this.mainApplicationClass = deduceMainApplicationClass();
234234
}
235235

236+
private boolean deduceWebEnvironment() {
237+
for (String className : WEB_ENVIRONMENT_CLASSES) {
238+
if (!ClassUtils.isPresent(className, null)) {
239+
return false;
240+
}
241+
}
242+
return true;
243+
}
244+
236245
private Class<?> deduceMainApplicationClass() {
237246
try {
238247
StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
@@ -864,12 +873,20 @@ public void setResourceLoader(ResourceLoader resourceLoader) {
864873
public void setApplicationContextClass(
865874
Class<? extends ConfigurableApplicationContext> applicationContextClass) {
866875
this.applicationContextClass = applicationContextClass;
867-
if (!isSpringWebAvailable() || !WebApplicationContext.class.isAssignableFrom(
868-
applicationContextClass)) {
876+
if (!isWebApplicationContext(applicationContextClass)) {
869877
this.webEnvironment = false;
870878
}
871879
}
872880

881+
private boolean isWebApplicationContext(Class<?> applicationContextClass) {
882+
try {
883+
return WebApplicationContext.class.isAssignableFrom(applicationContextClass);
884+
}
885+
catch (NoClassDefFoundError ex) {
886+
return false;
887+
}
888+
}
889+
873890
/**
874891
* Sets the {@link ApplicationContextInitializer} that will be applied to the Spring
875892
* {@link ApplicationContext}.
@@ -928,15 +945,6 @@ public Set<ApplicationListener<?>> getListeners() {
928945
return asUnmodifiableOrderedSet(this.listeners);
929946
}
930947

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-
940948
/**
941949
* Static helper that can be used to run a {@link SpringApplication} from the
942950
* specified source using default settings.

0 commit comments

Comments
 (0)