@@ -164,27 +164,16 @@ public class SpringApplication {
164
164
* The class name of application context that will be used by default for web
165
165
* environments.
166
166
*/
167
- public static final String DEFAULT_WEB_CONTEXT_CLASS = "org.springframework.boot."
167
+ public static final String DEFAULT_SERVLET_WEB_CONTEXT_CLASS = "org.springframework.boot."
168
168
+ "web.servlet.context.AnnotationConfigServletWebServerApplicationContext" ;
169
169
170
- private static final String [] WEB_ENVIRONMENT_CLASSES = { "javax.servlet.Servlet" ,
171
- "org.springframework.web.context.ConfigurableWebApplicationContext" };
172
-
173
170
/**
174
171
* The class name of application context that will be used by default for reactive web
175
172
* environments.
176
173
*/
177
174
public static final String DEFAULT_REACTIVE_WEB_CONTEXT_CLASS = "org.springframework."
178
175
+ "boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext" ;
179
176
180
- private static final String REACTIVE_WEB_ENVIRONMENT_CLASS = "org.springframework."
181
- + "web.reactive.DispatcherHandler" ;
182
-
183
- private static final String MVC_WEB_ENVIRONMENT_CLASS = "org.springframework."
184
- + "web.servlet.DispatcherServlet" ;
185
-
186
- private static final String JERSEY_WEB_ENVIRONMENT_CLASS = "org.glassfish.jersey.server.ResourceConfig" ;
187
-
188
177
/**
189
178
* Default banner location.
190
179
*/
@@ -266,27 +255,13 @@ public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySourc
266
255
this .resourceLoader = resourceLoader ;
267
256
Assert .notNull (primarySources , "PrimarySources must not be null" );
268
257
this .primarySources = new LinkedHashSet <>(Arrays .asList (primarySources ));
269
- this .webApplicationType = deduceWebApplicationType ();
258
+ this .webApplicationType = WebApplicationType . deduceFromClasspath ();
270
259
setInitializers ((Collection ) getSpringFactoriesInstances (
271
260
ApplicationContextInitializer .class ));
272
261
setListeners ((Collection ) getSpringFactoriesInstances (ApplicationListener .class ));
273
262
this .mainApplicationClass = deduceMainApplicationClass ();
274
263
}
275
264
276
- private WebApplicationType deduceWebApplicationType () {
277
- if (ClassUtils .isPresent (REACTIVE_WEB_ENVIRONMENT_CLASS , null )
278
- && !ClassUtils .isPresent (MVC_WEB_ENVIRONMENT_CLASS , null )
279
- && !ClassUtils .isPresent (JERSEY_WEB_ENVIRONMENT_CLASS , null )) {
280
- return WebApplicationType .REACTIVE ;
281
- }
282
- for (String className : WEB_ENVIRONMENT_CLASSES ) {
283
- if (!ClassUtils .isPresent (className , null )) {
284
- return WebApplicationType .NONE ;
285
- }
286
- }
287
- return WebApplicationType .SERVLET ;
288
- }
289
-
290
265
private Class <?> deduceMainApplicationClass () {
291
266
try {
292
267
StackTraceElement [] stackTrace = new RuntimeException ().getStackTrace ();
@@ -596,7 +571,7 @@ protected ConfigurableApplicationContext createApplicationContext() {
596
571
try {
597
572
switch (this .webApplicationType ) {
598
573
case SERVLET :
599
- contextClass = Class .forName (DEFAULT_WEB_CONTEXT_CLASS );
574
+ contextClass = Class .forName (DEFAULT_SERVLET_WEB_CONTEXT_CLASS );
600
575
break ;
601
576
case REACTIVE :
602
577
contextClass = Class .forName (DEFAULT_REACTIVE_WEB_CONTEXT_CLASS );
@@ -1173,38 +1148,16 @@ public void setResourceLoader(ResourceLoader resourceLoader) {
1173
1148
1174
1149
/**
1175
1150
* Sets the type of Spring {@link ApplicationContext} that will be created. If not
1176
- * specified defaults to {@link #DEFAULT_WEB_CONTEXT_CLASS} for web based applications
1177
- * or {@link AnnotationConfigApplicationContext} for non web based applications.
1151
+ * specified defaults to {@link #DEFAULT_SERVLET_WEB_CONTEXT_CLASS} for web based
1152
+ * applications or {@link AnnotationConfigApplicationContext} for non web based
1153
+ * applications.
1178
1154
* @param applicationContextClass the context class to set
1179
1155
*/
1180
1156
public void setApplicationContextClass (
1181
1157
Class <? extends ConfigurableApplicationContext > applicationContextClass ) {
1182
1158
this .applicationContextClass = applicationContextClass ;
1183
- this .webApplicationType = deduceWebApplicationType (applicationContextClass );
1184
- }
1185
-
1186
- private WebApplicationType deduceWebApplicationType (
1187
- Class <?> applicationContextClass ) {
1188
- if (safeIsAssignableFrom ("org.springframework.web.context.WebApplicationContext" ,
1189
- applicationContextClass )) {
1190
- return WebApplicationType .SERVLET ;
1191
- }
1192
- if (safeIsAssignableFrom (
1193
- "org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext" ,
1194
- applicationContextClass )) {
1195
- return WebApplicationType .REACTIVE ;
1196
- }
1197
- return WebApplicationType .NONE ;
1198
- }
1199
-
1200
- private boolean safeIsAssignableFrom (String target , Class <?> type ) {
1201
- try {
1202
- Class <?> targetClass = ClassUtils .forName (target , getClassLoader ());
1203
- return targetClass .isAssignableFrom (type );
1204
- }
1205
- catch (Throwable ex ) {
1206
- return false ;
1207
- }
1159
+ this .webApplicationType = WebApplicationType
1160
+ .deduceFromApplicationContext (applicationContextClass );
1208
1161
}
1209
1162
1210
1163
/**
0 commit comments