Skip to content

Commit 456d1af

Browse files
committed
Polish
1 parent cdc6cfc commit 456d1af

File tree

3 files changed

+65
-57
lines changed

3 files changed

+65
-57
lines changed

spring-boot-project/spring-boot-starters/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
34
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
45
<modelVersion>4.0.0</modelVersion>
56
<parent>
@@ -145,6 +146,9 @@
145146
<ignoredResourcePatterns>
146147
<ignoredResourcePattern>changelog.txt</ignoredResourcePattern>
147148
</ignoredResourcePatterns>
149+
<ignoredClassPatterns>
150+
<ignoredClassPattern>module-info</ignoredClassPattern>
151+
</ignoredClassPatterns>
148152
</configuration>
149153
</execution>
150154
</executions>

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

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -164,27 +164,16 @@ public class SpringApplication {
164164
* The class name of application context that will be used by default for web
165165
* environments.
166166
*/
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."
168168
+ "web.servlet.context.AnnotationConfigServletWebServerApplicationContext";
169169

170-
private static final String[] WEB_ENVIRONMENT_CLASSES = { "javax.servlet.Servlet",
171-
"org.springframework.web.context.ConfigurableWebApplicationContext" };
172-
173170
/**
174171
* The class name of application context that will be used by default for reactive web
175172
* environments.
176173
*/
177174
public static final String DEFAULT_REACTIVE_WEB_CONTEXT_CLASS = "org.springframework."
178175
+ "boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext";
179176

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-
188177
/**
189178
* Default banner location.
190179
*/
@@ -266,27 +255,13 @@ public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySourc
266255
this.resourceLoader = resourceLoader;
267256
Assert.notNull(primarySources, "PrimarySources must not be null");
268257
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
269-
this.webApplicationType = deduceWebApplicationType();
258+
this.webApplicationType = WebApplicationType.deduceFromClasspath();
270259
setInitializers((Collection) getSpringFactoriesInstances(
271260
ApplicationContextInitializer.class));
272261
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
273262
this.mainApplicationClass = deduceMainApplicationClass();
274263
}
275264

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-
290265
private Class<?> deduceMainApplicationClass() {
291266
try {
292267
StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
@@ -596,7 +571,7 @@ protected ConfigurableApplicationContext createApplicationContext() {
596571
try {
597572
switch (this.webApplicationType) {
598573
case SERVLET:
599-
contextClass = Class.forName(DEFAULT_WEB_CONTEXT_CLASS);
574+
contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);
600575
break;
601576
case REACTIVE:
602577
contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
@@ -1173,38 +1148,16 @@ public void setResourceLoader(ResourceLoader resourceLoader) {
11731148

11741149
/**
11751150
* 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.
11781154
* @param applicationContextClass the context class to set
11791155
*/
11801156
public void setApplicationContextClass(
11811157
Class<? extends ConfigurableApplicationContext> applicationContextClass) {
11821158
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);
12081161
}
12091162

12101163
/**

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/WebApplicationType.java

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot;
1818

19+
import org.springframework.util.ClassUtils;
20+
1921
/**
2022
* An enumeration of possible types of web application.
2123
*
@@ -41,6 +43,55 @@ public enum WebApplicationType {
4143
* The application should run as a reactive web application and should start an
4244
* embedded reactive web server.
4345
*/
44-
REACTIVE
46+
REACTIVE;
47+
48+
private static final String[] SERVLET_INDICATOR_CLASSES = { "javax.servlet.Servlet",
49+
"org.springframework.web.context.ConfigurableWebApplicationContext" };
50+
51+
private static final String WEBMVC_INDICATOR_CLASS = "org.springframework."
52+
+ "web.servlet.DispatcherServlet";
53+
54+
private static final String WEBFLUX_INDICATOR_CLASS = "org."
55+
+ "springframework.web.reactive.DispatcherHandler";
56+
57+
private static final String JERSEY_INDICATOR_CLASS = "org.glassfish.jersey.server.ResourceConfig";
58+
59+
private static final String SERVLET_APPLICATION_CONTEXT_CLASS = "org.springframework.web.context.WebApplicationContext";
60+
61+
private static final String REACTIVE_APPLICATION_CONTEXT_CLASS = "org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext";
62+
63+
static WebApplicationType deduceFromClasspath() {
64+
if (ClassUtils.isPresent(WEBFLUX_INDICATOR_CLASS, null)
65+
&& !ClassUtils.isPresent(WEBMVC_INDICATOR_CLASS, null)
66+
&& !ClassUtils.isPresent(JERSEY_INDICATOR_CLASS, null)) {
67+
return WebApplicationType.REACTIVE;
68+
}
69+
for (String className : SERVLET_INDICATOR_CLASSES) {
70+
if (!ClassUtils.isPresent(className, null)) {
71+
return WebApplicationType.NONE;
72+
}
73+
}
74+
return WebApplicationType.SERVLET;
75+
}
76+
77+
static WebApplicationType deduceFromApplicationContext(
78+
Class<?> applicationContextClass) {
79+
if (isAssignable(SERVLET_APPLICATION_CONTEXT_CLASS, applicationContextClass)) {
80+
return WebApplicationType.SERVLET;
81+
}
82+
if (isAssignable(REACTIVE_APPLICATION_CONTEXT_CLASS, applicationContextClass)) {
83+
return WebApplicationType.REACTIVE;
84+
}
85+
return WebApplicationType.NONE;
86+
}
87+
88+
private static boolean isAssignable(String target, Class<?> type) {
89+
try {
90+
return ClassUtils.resolveClassName(target, null).isAssignableFrom(type);
91+
}
92+
catch (Throwable ex) {
93+
return false;
94+
}
95+
}
4596

4697
}

0 commit comments

Comments
 (0)