mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Provide a condition for detecting war deployments
Closes gh-19421
This commit is contained in:
parent
9aae072872
commit
1342e4970a
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.condition;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
|
||||
/**
|
||||
* {@link Conditional @Conditional} that matches when the application is a traditional WAR
|
||||
* deployment. For applications with embedded servers, this condition will return false.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
* @since 2.3.0
|
||||
*/
|
||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Conditional(OnWarDeploymentCondition.class)
|
||||
public @interface ConditionalOnWarDeployment {
|
||||
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.condition;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
/**
|
||||
* {@link Condition} that checks if the application is running as a traditional war
|
||||
* deployment.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
class OnWarDeploymentCondition extends SpringBootCondition {
|
||||
|
||||
@Override
|
||||
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
ResourceLoader resourceLoader = context.getResourceLoader();
|
||||
if (resourceLoader instanceof WebApplicationContext) {
|
||||
WebApplicationContext applicationContext = (WebApplicationContext) resourceLoader;
|
||||
ServletContext servletContext = applicationContext.getServletContext();
|
||||
if (servletContext != null) {
|
||||
return ConditionOutcome.match("Application is deployed as a WAR file.");
|
||||
}
|
||||
}
|
||||
return ConditionOutcome.noMatch(ConditionMessage.forCondition(ConditionalOnWarDeployment.class)
|
||||
.because("the application is not deployed as a WAR file."));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.condition;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
|
||||
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
|
||||
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link ConditionalOnWarDeployment @ConditionalOnWarDeployment}.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
class ConditionalOnWarDeploymentTests {
|
||||
|
||||
@Test
|
||||
void nonWebApplicationShouldNotMatch() {
|
||||
ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.run((context) -> assertThat(context).doesNotHaveBean("forWar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void reactiveWebApplicationShouldNotMatch() {
|
||||
ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner();
|
||||
contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.run((context) -> assertThat(context).doesNotHaveBean("forWar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void embeddedServletWebApplicationShouldNotMatch() {
|
||||
WebApplicationContextRunner contextRunner = new WebApplicationContextRunner(
|
||||
AnnotationConfigServletWebApplicationContext::new);
|
||||
contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.run((context) -> assertThat(context).doesNotHaveBean("forWar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void warDeployedServletWebApplicationShouldMatch() {
|
||||
// sets a mock servletContext before context refresh which is what the
|
||||
// SpringBootServletInitializer does for WAR deployments.
|
||||
WebApplicationContextRunner contextRunner = new WebApplicationContextRunner();
|
||||
contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.run((context) -> assertThat(context).hasBean("forWar"));
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnWarDeployment
|
||||
static class TestConfiguration {
|
||||
|
||||
@Bean
|
||||
String forWar() {
|
||||
return "forWar";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -7606,6 +7606,9 @@ The `@ConditionalOnWebApplication` and `@ConditionalOnNotWebApplication` annotat
|
||||
A servlet-based web application is any application that uses a Spring `WebApplicationContext`, defines a `session` scope, or has a `ConfigurableWebEnvironment`.
|
||||
A reactive web application is any application that uses a `ReactiveWebApplicationContext`, or has a `ConfigurableReactiveWebEnvironment`.
|
||||
|
||||
The `@ConditionalOnWarDeployment` annotation lets configuration be included depending on whether the application is a traditional WAR application that is deployed to a container.
|
||||
This condition will not match for applications that are run with an embedded server.
|
||||
|
||||
|
||||
|
||||
[[boot-features-spel-conditions]]
|
||||
|
@ -10,6 +10,7 @@ dependencies {
|
||||
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-web")) {
|
||||
exclude group: "org.hibernate.validator"
|
||||
}
|
||||
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator"))
|
||||
|
||||
intTestImplementation(enforcedPlatform(project(path: ":spring-boot-project:spring-boot-parent")))
|
||||
intTestImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
|
||||
@ -20,8 +21,6 @@ dependencies {
|
||||
intTestImplementation("org.springframework:spring-web")
|
||||
|
||||
providedRuntime(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-tomcat"))
|
||||
|
||||
runtimeOnly(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator"))
|
||||
}
|
||||
|
||||
intTest {
|
||||
|
@ -66,6 +66,16 @@ class DeploymentIntegrationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("deployedApplications")
|
||||
void conditionalOnWarShouldBeTrue(DeployedApplication application) throws Exception {
|
||||
application.test((rest) -> {
|
||||
ResponseEntity<String> response = rest.getForEntity("/actuator/war", String.class);
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(response.getBody()).isEqualTo("{\"hello\":\"world\"}");
|
||||
});
|
||||
}
|
||||
|
||||
static List<DeployedApplication> deployedApplications() {
|
||||
return Arrays.asList(new DeployedApplication("open-liberty:19.0.0.9-webProfile8", "/config/dropins", 9080),
|
||||
new DeployedApplication("tomcat:9.0.29-jdk8-openjdk", "/usr/local/tomcat/webapps", 8080),
|
||||
|
@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample;
|
||||
package sample.app;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample;
|
||||
package sample.app;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.autoconfig;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWarDeployment;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@ConditionalOnWarDeployment
|
||||
@Configuration
|
||||
public class ExampleAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public TestEndpoint testEndpoint() {
|
||||
return new TestEndpoint();
|
||||
}
|
||||
|
||||
@Endpoint(id = "war")
|
||||
static class TestEndpoint {
|
||||
|
||||
@ReadOperation
|
||||
String hello() {
|
||||
return "{\"hello\":\"world\"}";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
sample.autoconfig.ExampleAutoConfiguration
|
@ -0,0 +1 @@
|
||||
management.endpoints.web.exposure.include: '*'
|
@ -11,6 +11,7 @@ configurations {
|
||||
|
||||
dependencies {
|
||||
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
|
||||
testImplementation(project(":spring-boot-project:spring-boot-actuator-autoconfigure"))
|
||||
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
|
||||
testImplementation("com.samskivert:jmustache")
|
||||
testImplementation("jakarta.servlet:jakarta.servlet-api")
|
||||
@ -25,6 +26,7 @@ dependencies {
|
||||
testRepository(project(path: ":spring-boot-project:spring-boot-dependencies", configuration: "mavenRepository"))
|
||||
testRepository(project(path: ":spring-boot-project:spring-boot-tools:spring-boot-maven-plugin", configuration: "mavenRepository"))
|
||||
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter", configuration: "mavenRepository"))
|
||||
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator", configuration: "mavenRepository"))
|
||||
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-jetty", configuration: "mavenRepository"))
|
||||
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-parent", configuration: "mavenRepository"))
|
||||
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-tomcat", configuration: "mavenRepository"))
|
||||
|
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.autoconfig;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWarDeployment;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@ConditionalOnWarDeployment
|
||||
@Configuration
|
||||
public class ExampleAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public TestEndpoint testEndpoint() {
|
||||
return new TestEndpoint();
|
||||
}
|
||||
|
||||
@Endpoint(id = "war")
|
||||
static class TestEndpoint {
|
||||
|
||||
@ReadOperation
|
||||
String hello() {
|
||||
return "{\"hello\":\"world\"}";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -146,6 +146,23 @@ class ApplicationBuilder {
|
||||
srcMainWebapp.mkdirs();
|
||||
FileCopyUtils.copy("webapp resource", new FileWriter(new File(srcMainWebapp, "webapp-resource.txt")));
|
||||
}
|
||||
copyAutoConfigurationFiles(appFolder);
|
||||
return;
|
||||
}
|
||||
|
||||
private void copyAutoConfigurationFiles(File appFolder) throws IOException {
|
||||
File autoConfigPackage = new File(appFolder, "src/main/java/com/autoconfig");
|
||||
autoConfigPackage.mkdirs();
|
||||
FileCopyUtils.copy(new File("src/test/java/com/autoconfig/ExampleAutoConfiguration.java"),
|
||||
new File(autoConfigPackage, "ExampleAutoConfiguration.java"));
|
||||
File srcMainResources = new File(appFolder, "src/main/resources");
|
||||
srcMainResources.mkdirs();
|
||||
File metaInf = new File(srcMainResources, "META-INF");
|
||||
metaInf.mkdirs();
|
||||
FileCopyUtils.copy(new File("src/test/resources/META-INF/spring.factories"),
|
||||
new File(metaInf, "spring.factories"));
|
||||
FileCopyUtils.copy(new File("src/test/resources/application.yml"),
|
||||
new File(srcMainResources, "application.yml"));
|
||||
}
|
||||
|
||||
private void packageApplication(File appFolder, File settingsXml) throws MavenInvocationException {
|
||||
|
@ -79,4 +79,10 @@ class EmbeddedServletContainerJarPackagingIntegrationTests {
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
@TestTemplate
|
||||
void conditionalOnWarDeploymentBeanIsNotAvailableForEmbeddedServer(RestTemplate rest) {
|
||||
ResponseEntity<String> entity = rest.getForEntity("/actuator/war", String.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -102,6 +102,12 @@ class EmbeddedServletContainerWarPackagingIntegrationTests {
|
||||
.noneMatch((resourcePath) -> resourcePath.startsWith("/org/springframework/boot/loader"));
|
||||
}
|
||||
|
||||
@TestTemplate
|
||||
void conditionalOnWarDeploymentBeanIsNotAvailableForEmbeddedServer(RestTemplate rest) {
|
||||
ResponseEntity<String> entity = rest.getForEntity("/actuator/war", String.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
private List<String> readLines(String input) {
|
||||
if (input == null) {
|
||||
return Collections.emptyList();
|
||||
|
@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.autoconfig.ExampleAutoConfiguration
|
@ -0,0 +1 @@
|
||||
management.endpoints.web.exposure.include: '*'
|
@ -18,7 +18,11 @@
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<artifactId>spring-boot-starter-json</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
@ -26,7 +30,7 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
|
Loading…
Reference in New Issue
Block a user