Skip to content

Commit e593a8d

Browse files
Merge pull request #34332 from terminux
* pr/34332: Polish "Disable embedded web auto-config when not using embedded web server" Disable embedded web auto-config when not using embedded web server Closes gh-34332
2 parents fe39ee6 + 6dc0f90 commit e593a8d

File tree

5 files changed

+129
-6
lines changed

5 files changed

+129
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.condition;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import org.springframework.context.annotation.Conditional;
26+
27+
/**
28+
* {@link Conditional @Conditional} that only matches when the application is not a
29+
* traditional WAR deployment. For applications with embedded servers, this condition will
30+
* return true.
31+
*
32+
* @author Guirong Hu
33+
* @since 2.7.10
34+
*/
35+
@Target({ ElementType.TYPE, ElementType.METHOD })
36+
@Retention(RetentionPolicy.RUNTIME)
37+
@Documented
38+
@Conditional(OnWarDeploymentCondition.class)
39+
public @interface ConditionalOnNotWarDeployment {
40+
41+
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,23 @@
2929
* deployment.
3030
*
3131
* @author Madhura Bhave
32+
* @see ConditionalOnWarDeployment
33+
* @see ConditionalOnNotWarDeployment
3234
*/
3335
class OnWarDeploymentCondition extends SpringBootCondition {
3436

3537
@Override
3638
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
39+
boolean required = metadata.isAnnotated(ConditionalOnWarDeployment.class.getName());
3740
ResourceLoader resourceLoader = context.getResourceLoader();
3841
if (resourceLoader instanceof WebApplicationContext) {
3942
WebApplicationContext applicationContext = (WebApplicationContext) resourceLoader;
4043
ServletContext servletContext = applicationContext.getServletContext();
4144
if (servletContext != null) {
42-
return ConditionOutcome.match("Application is deployed as a WAR file.");
45+
return new ConditionOutcome(required, "Application is deployed as a WAR file.");
4346
}
4447
}
45-
return ConditionOutcome.noMatch(ConditionMessage.forCondition(ConditionalOnWarDeployment.class)
48+
return new ConditionOutcome(!required, ConditionMessage.forCondition(ConditionalOnWarDeployment.class)
4649
.because("the application is not deployed as a WAR file."));
4750
}
4851

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/EmbeddedWebServerFactoryCustomizerAutoConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
import org.springframework.boot.autoconfigure.AutoConfiguration;
2929
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3030
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
31+
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWarDeployment;
3132
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
3233
import org.springframework.boot.autoconfigure.web.ServerProperties;
3334
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -43,6 +44,7 @@
4344
* @since 2.0.0
4445
*/
4546
@AutoConfiguration
47+
@ConditionalOnNotWarDeployment
4648
@ConditionalOnWebApplication
4749
@EnableConfigurationProperties(ServerProperties.class)
4850
public class EmbeddedWebServerFactoryCustomizerAutoConfiguration {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.condition;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
22+
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
23+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
24+
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* Tests for {@link ConditionalOnNotWarDeployment @ConditionalOnNotWarDeployment}.
32+
*
33+
* @author Guirong Hu
34+
*/
35+
class ConditionalOnNotWarDeploymentTests {
36+
37+
@Test
38+
void nonWebApplicationShouldMatch() {
39+
ApplicationContextRunner contextRunner = new ApplicationContextRunner();
40+
contextRunner.withUserConfiguration(NotWarDeploymentConfiguration.class)
41+
.run((context) -> assertThat(context).hasBean("notForWar"));
42+
}
43+
44+
@Test
45+
void reactiveWebApplicationShouldMatch() {
46+
ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner();
47+
contextRunner.withUserConfiguration(NotWarDeploymentConfiguration.class)
48+
.run((context) -> assertThat(context).hasBean("notForWar"));
49+
}
50+
51+
@Test
52+
void embeddedServletWebApplicationShouldMatch() {
53+
WebApplicationContextRunner contextRunner = new WebApplicationContextRunner(
54+
AnnotationConfigServletWebApplicationContext::new);
55+
contextRunner.withUserConfiguration(NotWarDeploymentConfiguration.class)
56+
.run((context) -> assertThat(context).hasBean("notForWar"));
57+
}
58+
59+
@Test
60+
void warDeployedServletWebApplicationShouldNotMatch() {
61+
WebApplicationContextRunner contextRunner = new WebApplicationContextRunner();
62+
contextRunner.withUserConfiguration(NotWarDeploymentConfiguration.class)
63+
.run((context) -> assertThat(context).doesNotHaveBean("notForWar"));
64+
}
65+
66+
@Configuration(proxyBeanMethods = false)
67+
@ConditionalOnNotWarDeployment
68+
static class NotWarDeploymentConfiguration {
69+
70+
@Bean
71+
String notForWar() {
72+
return "notForWar";
73+
}
74+
75+
}
76+
77+
}

spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/developing-auto-configuration.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ Resources can be specified by using the usual Spring conventions, as shown in th
125125

126126
[[features.developing-auto-configuration.condition-annotations.web-application-conditions]]
127127
==== Web Application Conditions
128-
The `@ConditionalOnWebApplication` and `@ConditionalOnNotWebApplication` annotations let configuration be included depending on whether the application is a "`web application`".
128+
The `@ConditionalOnWebApplication` and `@ConditionalOnNotWebApplication` annotations let configuration be included depending on whether the application is a web application.
129129
A servlet-based web application is any application that uses a Spring `WebApplicationContext`, defines a `session` scope, or has a `ConfigurableWebEnvironment`.
130130
A reactive web application is any application that uses a `ReactiveWebApplicationContext`, or has a `ConfigurableReactiveWebEnvironment`.
131131

132-
The `@ConditionalOnWarDeployment` annotation lets configuration be included depending on whether the application is a traditional WAR application that is deployed to a container.
133-
This condition will not match for applications that are run with an embedded server.
132+
The `@ConditionalOnWarDeployment` and `@ConditionalOnNotWarDeployment` annotations let configuration be included depending on whether the application is a traditional WAR application that is deployed to a servlet container.
133+
This condition will not match for applications that are run with an embedded web server.
134134

135135

136136

0 commit comments

Comments
 (0)