mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-15 01:07:30 +08:00
Simplify WebApplicationContext class guard
See gh-3856
This commit is contained in:
parent
cb4e709ba2
commit
feb2452f06
@ -20,6 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
import sample.simple.service.HelloWorldService;
|
||||
|
||||
@ -39,6 +40,9 @@ public class SampleSimpleApplication implements CommandLineRunner {
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication application = new SpringApplication(
|
||||
SampleSimpleApplication.class);
|
||||
application.setApplicationContextClass(AnnotationConfigApplicationContext.class);
|
||||
SpringApplication.run(SampleSimpleApplication.class, args);
|
||||
}
|
||||
}
|
||||
|
@ -227,12 +227,21 @@ public class SpringApplication {
|
||||
if (sources != null && sources.length > 0) {
|
||||
this.sources.addAll(Arrays.asList(sources));
|
||||
}
|
||||
this.webEnvironment = isSpringWebAvailable();
|
||||
this.webEnvironment = deduceWebEnvironment();
|
||||
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();
|
||||
@ -864,12 +873,20 @@ public class SpringApplication {
|
||||
public void setApplicationContextClass(
|
||||
Class<? extends ConfigurableApplicationContext> applicationContextClass) {
|
||||
this.applicationContextClass = applicationContextClass;
|
||||
if (!isSpringWebAvailable() || !WebApplicationContext.class.isAssignableFrom(
|
||||
applicationContextClass)) {
|
||||
if (!isWebApplicationContext(applicationContextClass)) {
|
||||
this.webEnvironment = false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isWebApplicationContext(Class<?> applicationContextClass) {
|
||||
try {
|
||||
return WebApplicationContext.class.isAssignableFrom(applicationContextClass);
|
||||
}
|
||||
catch (NoClassDefFoundError ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link ApplicationContextInitializer} that will be applied to the Spring
|
||||
* {@link ApplicationContext}.
|
||||
@ -928,15 +945,6 @@ 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…
Reference in New Issue
Block a user