@@ -227,12 +227,21 @@ private void initialize(Object[] sources) {
227
227
if (sources != null && sources .length > 0 ) {
228
228
this .sources .addAll (Arrays .asList (sources ));
229
229
}
230
- this .webEnvironment = isSpringWebAvailable ();
230
+ this .webEnvironment = deduceWebEnvironment ();
231
231
setInitializers ((Collection ) getSpringFactoriesInstances (ApplicationContextInitializer .class ));
232
232
setListeners ((Collection ) getSpringFactoriesInstances (ApplicationListener .class ));
233
233
this .mainApplicationClass = deduceMainApplicationClass ();
234
234
}
235
235
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
+
236
245
private Class <?> deduceMainApplicationClass () {
237
246
try {
238
247
StackTraceElement [] stackTrace = new RuntimeException ().getStackTrace ();
@@ -864,12 +873,20 @@ public void setResourceLoader(ResourceLoader resourceLoader) {
864
873
public void setApplicationContextClass (
865
874
Class <? extends ConfigurableApplicationContext > applicationContextClass ) {
866
875
this .applicationContextClass = applicationContextClass ;
867
- if (!isSpringWebAvailable () || !WebApplicationContext .class .isAssignableFrom (
868
- applicationContextClass )) {
876
+ if (!isWebApplicationContext (applicationContextClass )) {
869
877
this .webEnvironment = false ;
870
878
}
871
879
}
872
880
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
+
873
890
/**
874
891
* Sets the {@link ApplicationContextInitializer} that will be applied to the Spring
875
892
* {@link ApplicationContext}.
@@ -928,15 +945,6 @@ public Set<ApplicationListener<?>> getListeners() {
928
945
return asUnmodifiableOrderedSet (this .listeners );
929
946
}
930
947
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
-
940
948
/**
941
949
* Static helper that can be used to run a {@link SpringApplication} from the
942
950
* specified source using default settings.
0 commit comments