Skip to content

Commit ff98ba0

Browse files
committed
Allow early OnWebApplication filtering
Update `OnWebApplicationCondition` to be an `AutoConfigurationImportFilter` and filter out classes early. Closes gh-13328
1 parent 75bde00 commit ff98ba0

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnWebApplicationCondition.java

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Map;
2020

21+
import org.springframework.boot.autoconfigure.AutoConfigurationMetadata;
2122
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
2223
import org.springframework.boot.web.reactive.context.ConfigurableReactiveWebEnvironment;
2324
import org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext;
@@ -36,14 +37,60 @@
3637
* {@link WebApplicationContext}.
3738
*
3839
* @author Dave Syer
40+
* @author Phillip Webb
3941
* @see ConditionalOnWebApplication
4042
* @see ConditionalOnNotWebApplication
4143
*/
4244
@Order(Ordered.HIGHEST_PRECEDENCE + 20)
43-
class OnWebApplicationCondition extends SpringBootCondition {
45+
class OnWebApplicationCondition extends FilteringSpringBootCondition {
4446

45-
private static final String WEB_CONTEXT_CLASS = "org.springframework.web.context."
46-
+ "support.GenericWebApplicationContext";
47+
private static final String SERVLET_WEB_APPLICATION_CLASS = "org.springframework.web.context.support.GenericWebApplicationContext";
48+
49+
private static final String REACTIVE_WEB_APPLICATION_CLASS = "org.springframework.web.reactive.HandlerResult";
50+
51+
@Override
52+
protected ConditionOutcome[] getOutcomes(String[] autoConfigurationClasses,
53+
AutoConfigurationMetadata autoConfigurationMetadata) {
54+
ConditionOutcome[] outcomes = new ConditionOutcome[autoConfigurationClasses.length];
55+
for (int i = 0; i < outcomes.length; i++) {
56+
String autoConfigurationClass = autoConfigurationClasses[i];
57+
if (autoConfigurationClass != null) {
58+
outcomes[i] = getOutcome(autoConfigurationMetadata
59+
.get(autoConfigurationClass, "ConditionalOnWebApplication"));
60+
}
61+
}
62+
return outcomes;
63+
}
64+
65+
private ConditionOutcome getOutcome(String type) {
66+
if (type == null) {
67+
return null;
68+
}
69+
ConditionMessage.Builder message = ConditionMessage
70+
.forCondition(ConditionalOnWebApplication.class);
71+
if (ConditionalOnWebApplication.Type.SERVLET.name().equals(type)) {
72+
if (!ClassNameFilter.isPresent(SERVLET_WEB_APPLICATION_CLASS,
73+
getBeanClassLoader())) {
74+
return ConditionOutcome.noMatch(
75+
message.didNotFind("servlet web application classes").atAll());
76+
}
77+
}
78+
if (ConditionalOnWebApplication.Type.REACTIVE.name().equals(type)) {
79+
if (!ClassNameFilter.isPresent(REACTIVE_WEB_APPLICATION_CLASS,
80+
getBeanClassLoader())) {
81+
return ConditionOutcome.noMatch(
82+
message.didNotFind("reactive web application classes").atAll());
83+
}
84+
}
85+
if (!ClassNameFilter.isPresent(SERVLET_WEB_APPLICATION_CLASS,
86+
getBeanClassLoader())
87+
&& !ClassUtils.isPresent(REACTIVE_WEB_APPLICATION_CLASS,
88+
getBeanClassLoader())) {
89+
return ConditionOutcome.noMatch(message
90+
.didNotFind("reactive or servlet web application classes").atAll());
91+
}
92+
return null;
93+
}
4794

4895
@Override
4996
public ConditionOutcome getMatchOutcome(ConditionContext context,
@@ -93,9 +140,10 @@ private ConditionOutcome isAnyWebApplication(ConditionContext context,
93140

94141
private ConditionOutcome isServletWebApplication(ConditionContext context) {
95142
ConditionMessage.Builder message = ConditionMessage.forCondition("");
96-
if (!ClassUtils.isPresent(WEB_CONTEXT_CLASS, context.getClassLoader())) {
97-
return ConditionOutcome
98-
.noMatch(message.didNotFind("web application classes").atAll());
143+
if (!ClassNameFilter.isPresent(SERVLET_WEB_APPLICATION_CLASS,
144+
context.getClassLoader())) {
145+
return ConditionOutcome.noMatch(
146+
message.didNotFind("servlet web application classes").atAll());
99147
}
100148
if (context.getBeanFactory() != null) {
101149
String[] scopes = context.getBeanFactory().getRegisteredScopeNames();
@@ -115,6 +163,11 @@ private ConditionOutcome isServletWebApplication(ConditionContext context) {
115163

116164
private ConditionOutcome isReactiveWebApplication(ConditionContext context) {
117165
ConditionMessage.Builder message = ConditionMessage.forCondition("");
166+
if (!ClassNameFilter.isPresent(REACTIVE_WEB_APPLICATION_CLASS,
167+
context.getClassLoader())) {
168+
return ConditionOutcome.noMatch(
169+
message.didNotFind("reactive web application classes").atAll());
170+
}
118171
if (context.getEnvironment() instanceof ConfigurableReactiveWebEnvironment) {
119172
return ConditionOutcome
120173
.match(message.foundExactly("ConfigurableReactiveWebEnvironment"));

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoCo
1818
# Auto Configuration Import Filters
1919
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
2020
org.springframework.boot.autoconfigure.condition.OnBeanCondition,\
21-
org.springframework.boot.autoconfigure.condition.OnClassCondition
21+
org.springframework.boot.autoconfigure.condition.OnClassCondition,\
22+
org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition
2223

2324
# Auto Configure
2425
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

0 commit comments

Comments
 (0)