Forces web-application-type=none in ContextRefresher.
This allows apps with spring.main.web-application-type={REACTIVE|SERVLET} to function properly without error.
fixes gh-678
This commit is contained in:
1
pom.xml
1
pom.xml
@@ -157,6 +157,7 @@
|
||||
<module>spring-cloud-test-support</module>
|
||||
<module>spring-cloud-context</module>
|
||||
<module>spring-cloud-context-integration-tests</module>
|
||||
<module>spring-cloud-context-webflux-integration-tests</module>
|
||||
<module>spring-cloud-commons</module>
|
||||
<module>spring-cloud-loadbalancer</module>
|
||||
<module>spring-cloud-starter</module>
|
||||
|
||||
57
spring-cloud-context-webflux-integration-tests/pom.xml
Normal file
57
spring-cloud-context-webflux-integration-tests/pom.xml
Normal file
@@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-commons-parent</artifactId>
|
||||
<version>2.1.5.BUILD-SNAPSHOT</version>
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
<artifactId>spring-cloud-context-webflux-integration-tests</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>spring-cloud-context-webflux-integration-tests</name>
|
||||
<description>Spring Cloud Context Webflux Integration Tests</description>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-context</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-test-support</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<configuration>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,268 @@
|
||||
/*
|
||||
* Copyright 2013-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.cloud.context.integration;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.cloud.context.integration.RefreshScopeIntegrationTests.TestConfiguration;
|
||||
import org.springframework.cloud.context.scope.GenericScope;
|
||||
import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jmx.export.annotation.ManagedAttribute;
|
||||
import org.springframework.jmx.export.annotation.ManagedResource;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.BDDAssertions.then;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = TestConfiguration.class)
|
||||
@SuppressWarnings("Duplicates")
|
||||
public class RefreshScopeIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private Service service;
|
||||
|
||||
@Autowired
|
||||
private TestProperties properties;
|
||||
|
||||
@Autowired
|
||||
private org.springframework.cloud.context.scope.refresh.RefreshScope scope;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
then(ExampleService.getInitCount()).isEqualTo(1);
|
||||
ExampleService.reset();
|
||||
}
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
ExampleService.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void testSimpleProperties() throws Exception {
|
||||
then(this.service.getMessage()).isEqualTo("Hello scope!");
|
||||
then(this.service instanceof Advised).isTrue();
|
||||
// Change the dynamic property source...
|
||||
this.properties.setMessage("Foo");
|
||||
// ...but don't refresh, so the bean stays the same:
|
||||
then(this.service.getMessage()).isEqualTo("Hello scope!");
|
||||
then(ExampleService.getInitCount()).isEqualTo(0);
|
||||
then(ExampleService.getDestroyCount()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void testRefresh() throws Exception {
|
||||
then(this.service.getMessage()).isEqualTo("Hello scope!");
|
||||
String id1 = this.service.toString();
|
||||
// Change the dynamic property source...
|
||||
this.properties.setMessage("Foo");
|
||||
// ...and then refresh, so the bean is re-initialized:
|
||||
this.scope.refreshAll();
|
||||
String id2 = this.service.toString();
|
||||
then(this.service.getMessage()).isEqualTo("Foo");
|
||||
then(ExampleService.getInitCount()).isEqualTo(1);
|
||||
then(ExampleService.getDestroyCount()).isEqualTo(1);
|
||||
then(id2).isNotSameAs(id1);
|
||||
then(ExampleService.event).isNotNull();
|
||||
then(ExampleService.event.getName())
|
||||
.isEqualTo(RefreshScopeRefreshedEvent.DEFAULT_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void testRefreshBean() throws Exception {
|
||||
then(this.service.getMessage()).isEqualTo("Hello scope!");
|
||||
String id1 = this.service.toString();
|
||||
// Change the dynamic property source...
|
||||
this.properties.setMessage("Foo");
|
||||
// ...and then refresh, so the bean is re-initialized:
|
||||
this.scope.refresh("service");
|
||||
String id2 = this.service.toString();
|
||||
then(this.service.getMessage()).isEqualTo("Foo");
|
||||
then(this.service.getMessage()).isEqualTo("Foo");
|
||||
then(ExampleService.getInitCount()).isEqualTo(1);
|
||||
then(ExampleService.getDestroyCount()).isEqualTo(1);
|
||||
then(id2).isNotSameAs(id1);
|
||||
then(ExampleService.event).isNotNull();
|
||||
then(ExampleService.event.getName())
|
||||
.isEqualTo(GenericScope.SCOPED_TARGET_PREFIX + "service");
|
||||
}
|
||||
|
||||
// see gh-349
|
||||
@Test(expected = ServiceException.class)
|
||||
@DirtiesContext
|
||||
public void testCheckedException() throws Exception {
|
||||
this.service.throwsException();
|
||||
}
|
||||
|
||||
public interface Service {
|
||||
|
||||
String getMessage();
|
||||
|
||||
String throwsException() throws ServiceException;
|
||||
|
||||
}
|
||||
|
||||
public static class ExampleService implements Service, InitializingBean,
|
||||
DisposableBean, ApplicationListener<RefreshScopeRefreshedEvent> {
|
||||
|
||||
private static Log logger = LogFactory.getLog(ExampleService.class);
|
||||
|
||||
private volatile static int initCount = 0;
|
||||
|
||||
private volatile static int destroyCount = 0;
|
||||
|
||||
private volatile static RefreshScopeRefreshedEvent event;
|
||||
|
||||
private String message = null;
|
||||
|
||||
private volatile long delay = 0;
|
||||
|
||||
public static void reset() {
|
||||
initCount = 0;
|
||||
destroyCount = 0;
|
||||
event = null;
|
||||
}
|
||||
|
||||
public static int getInitCount() {
|
||||
return initCount;
|
||||
}
|
||||
|
||||
public static int getDestroyCount() {
|
||||
return destroyCount;
|
||||
}
|
||||
|
||||
public void setDelay(long delay) {
|
||||
this.delay = delay;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
logger.debug("Initializing message: " + this.message);
|
||||
initCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
logger.debug("Destroying message: " + this.message);
|
||||
destroyCount++;
|
||||
this.message = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
logger.debug("Getting message: " + this.message);
|
||||
try {
|
||||
Thread.sleep(this.delay);
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
logger.info("Returning message: " + this.message);
|
||||
return this.message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
logger.debug("Setting message: " + message);
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String throwsException() throws ServiceException {
|
||||
throw new ServiceException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(RefreshScopeRefreshedEvent e) {
|
||||
event = e;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public static class ServiceException extends Exception {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(TestProperties.class)
|
||||
@EnableAutoConfiguration
|
||||
protected static class TestConfiguration {
|
||||
|
||||
@Autowired
|
||||
private TestProperties properties;
|
||||
|
||||
@Bean
|
||||
@RefreshScope
|
||||
public ExampleService service() {
|
||||
ExampleService service = new ExampleService();
|
||||
service.setMessage(this.properties.getMessage());
|
||||
service.setDelay(this.properties.getDelay());
|
||||
return service;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ConfigurationProperties
|
||||
@ManagedResource
|
||||
protected static class TestProperties {
|
||||
|
||||
private String message;
|
||||
|
||||
private int delay;
|
||||
|
||||
@ManagedAttribute
|
||||
public String getMessage() {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@ManagedAttribute
|
||||
public int getDelay() {
|
||||
return this.delay;
|
||||
}
|
||||
|
||||
public void setDelay(int delay) {
|
||||
this.delay = delay;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright 2013-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.cloud.context.integration.webflux;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = RefreshEndpointIntegrationTests.ClientApp.class, properties = {
|
||||
"management.endpoints.web.exposure.include=*" }, webEnvironment = RANDOM_PORT)
|
||||
public class RefreshEndpointIntegrationTests {
|
||||
|
||||
private static final String BASE_PATH = new WebEndpointProperties().getBasePath();
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Test
|
||||
public void webAccess() throws Exception {
|
||||
TestRestTemplate template = new TestRestTemplate();
|
||||
ResponseEntity<String> entity = template.postForEntity(
|
||||
"http://localhost:" + this.port + BASE_PATH + "/refresh", null,
|
||||
String.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
protected static class ClientApp {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
spring.main.web-application-type=REACTIVE
|
||||
@@ -0,0 +1 @@
|
||||
info.name:child
|
||||
@@ -182,6 +182,8 @@ public class ContextRefresher {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("spring.jmx.enabled", false);
|
||||
map.put("spring.main.sources", "");
|
||||
// gh-678 without this apps with this property set to REACTIVE or SERVLET fail
|
||||
map.put("spring.main.web-application-type", "NONE");
|
||||
capturedPropertySources
|
||||
.addFirst(new MapPropertySource(REFRESH_ARGS_PROPERTY_SOURCE, map));
|
||||
return environment;
|
||||
|
||||
Reference in New Issue
Block a user