@@ -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