Add a configurable of beans to postprocess into refresh scope

Allows you to apply @RefreshScope to beans that you do not
control or define. The list is configurable as
spring.cloud.refresh.refreshable and by default has only
HikariDataSource.

Fixes gh-318
This commit is contained in:
Dave Syer
2018-03-02 14:03:54 +00:00
parent d14e8ffa1a
commit dcc822626b
5 changed files with 164 additions and 20 deletions

View File

@@ -5,14 +5,17 @@ import org.junit.Test;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Dave Syer
*/
@@ -24,7 +27,7 @@ public class RefreshAutoConfigurationTests {
@Test
public void noWarnings() {
try (ConfigurableApplicationContext context = getApplicationContext(
WebApplicationType.NONE, Config.class)) {
WebApplicationType.NONE, Config.class)) {
assertThat(context.containsBean("refreshScope")).isTrue();
assertThat(output.toString()).doesNotContain("WARN");
}
@@ -33,19 +36,59 @@ public class RefreshAutoConfigurationTests {
@Test
public void disabled() {
try (ConfigurableApplicationContext context = getApplicationContext(
WebApplicationType.SERVLET, Config.class, "spring.cloud.refresh.enabled:false")) {
WebApplicationType.SERVLET, Config.class,
"spring.cloud.refresh.enabled:false")) {
assertThat(context.containsBean("refreshScope")).isFalse();
}
}
@Test
public void refreshables() {
try (ConfigurableApplicationContext context = getApplicationContext(
WebApplicationType.NONE, Config.class, "config.foo=bar",
"spring.cloud.refresh.refreshable:" + ConfigProps.class.getName())) {
context.getBean(ContextRefresher.class).refresh();
}
}
@Test
public void extraRefreshables() {
try (ConfigurableApplicationContext context = getApplicationContext(
WebApplicationType.NONE, Config.class, "config.foo=bar",
"spring.cloud.refresh.extra-refreshable:"
+ ConfigProps.class.getName())) {
context.getBean(ContextRefresher.class).refresh();
}
}
private static ConfigurableApplicationContext getApplicationContext(
WebApplicationType type, Class<?> configuration, String... properties) {
return new SpringApplicationBuilder(configuration).web(type).properties(properties).properties("server.port=0").run();
WebApplicationType type, Class<?> configuration, String... properties) {
return new SpringApplicationBuilder(configuration).web(type)
.properties(properties).properties("server.port=0").run();
}
@Configuration
@EnableAutoConfiguration
@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
@EnableConfigurationProperties(ConfigProps.class)
static class Config {
}
@ConfigurationProperties("config")
static class ConfigProps {
private String foo;
private boolean sealed;
public String getFoo() {
return this.foo;
}
public void setFoo(String foo) {
if (this.sealed) {
throw new IllegalStateException("Cannot set sealed property");
}
this.foo = foo;
this.sealed = true;
}
}
}