Ensures HikariDataSource is never re-bound.
Removes HikariDataSource from extra-refreshable and adds it to a new property never-refreshable. ConfigurationPropertiesRebinder now checks that property and does not rebind beans whose class is in never-refreshable. fixes gh-687
This commit is contained in:
@@ -184,9 +184,9 @@ Then, the next time something borrows a connection from the pool, it gets one wi
|
||||
Sometimes, it might even be mandatory to apply the `@RefreshScope` annotation on some beans that can be only initialized once.
|
||||
If a bean is "`immutable`", you have to either annotate the bean with `@RefreshScope` or specify the classname under the property key: `spring.cloud.refresh.extra-refreshable`.
|
||||
|
||||
IMPORTANT: If you create a `DataSource` bean yourself and the implementation is a `HikariDataSource`, return the
|
||||
most specific type, in this case `HikariDataSource`. Otherwise, you need to set
|
||||
`spring.cloud.refresh.extra-refreshable=javax.sql.DataSource`.
|
||||
WARNING: If you hava a `DataSource` bean that is a `HikariDataSource`, it can not be
|
||||
refreshed. It is the default value for `spring.cloud.refresh.never-refreshable`. Choose a
|
||||
different `DataSource` implementation if you need it to be refreshed.
|
||||
|
||||
Refresh scope beans are lazy proxies that initialize when they are used (that is, when a method is called), and the scope acts as a cache of initialized values.
|
||||
To force a bean to re-initialize on the next method call, you must invalidate its cache entry.
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.cloud.autoconfigure;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -140,8 +139,7 @@ public class RefreshAutoConfiguration {
|
||||
* Class names for beans to post process into refresh scope. Useful when you don't
|
||||
* control the bean definition (e.g. it came from auto-configuration).
|
||||
*/
|
||||
private Set<String> refreshables = new HashSet<>(
|
||||
Arrays.asList("com.zaxxer.hikari.HikariDataSource"));
|
||||
private Set<String> refreshables = new HashSet<>();
|
||||
|
||||
public Set<String> getRefreshable() {
|
||||
return this.refreshables;
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.springframework.jmx.export.annotation.ManagedAttribute;
|
||||
import org.springframework.jmx.export.annotation.ManagedOperation;
|
||||
import org.springframework.jmx.export.annotation.ManagedResource;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Listens for {@link EnvironmentChangeEvent} and rebinds beans that were bound to the
|
||||
@@ -96,6 +97,11 @@ public class ConfigurationPropertiesRebinder
|
||||
bean = ProxyUtils.getTargetObject(bean);
|
||||
}
|
||||
if (bean != null) {
|
||||
// TODO: determine a more general approach to fix this.
|
||||
// see https://github.com/spring-cloud/spring-cloud-commons/issues/571
|
||||
if (getNeverRefreshable().contains(bean.getClass().getName())) {
|
||||
return false; // ignore
|
||||
}
|
||||
this.applicationContext.getAutowireCapableBeanFactory()
|
||||
.destroyBean(bean);
|
||||
this.applicationContext.getAutowireCapableBeanFactory()
|
||||
@@ -115,6 +121,14 @@ public class ConfigurationPropertiesRebinder
|
||||
return false;
|
||||
}
|
||||
|
||||
@ManagedAttribute
|
||||
public Set<String> getNeverRefreshable() {
|
||||
String neverRefresh = this.applicationContext.getEnvironment().getProperty(
|
||||
"spring.cloud.refresh.never-refreshable",
|
||||
"com.zaxxer.hikari.HikariDataSource");
|
||||
return StringUtils.commaDelimitedListToSet(neverRefresh);
|
||||
}
|
||||
|
||||
@ManagedAttribute
|
||||
public Set<String> getBeanNames() {
|
||||
return new HashSet<>(this.beans.getBeanNames());
|
||||
|
||||
@@ -41,6 +41,12 @@
|
||||
"type": "java.util.Set<java.lang.String>",
|
||||
"description": "Additional class names for beans to post process into refresh scope.",
|
||||
"defaultValue": true
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.refresh.never-refreshable",
|
||||
"type": "java.lang.String",
|
||||
"description": "Comma separated list of class names for beans to never be refreshed or rebound.",
|
||||
"defaultValue": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.cloud.autoconfigure;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -30,6 +32,7 @@ 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;
|
||||
import static org.assertj.core.api.BDDAssertions.then;
|
||||
|
||||
/**
|
||||
@@ -68,8 +71,9 @@ public class RefreshAutoConfigurationTests {
|
||||
public void refreshables() {
|
||||
try (ConfigurableApplicationContext context = getApplicationContext(
|
||||
WebApplicationType.NONE, Config.class, "config.foo=bar",
|
||||
"spring.cloud.refresh.refreshable:" + ConfigProps.class.getName())) {
|
||||
context.getBean(ConfigProps.class);
|
||||
"spring.cloud.refresh.refreshable:"
|
||||
+ SealedConfigProps.class.getName())) {
|
||||
context.getBean(SealedConfigProps.class);
|
||||
context.getBean(ContextRefresher.class).refresh();
|
||||
}
|
||||
}
|
||||
@@ -77,23 +81,37 @@ public class RefreshAutoConfigurationTests {
|
||||
@Test
|
||||
public void extraRefreshables() {
|
||||
try (ConfigurableApplicationContext context = getApplicationContext(
|
||||
WebApplicationType.NONE, Config.class, "config.foo=bar",
|
||||
WebApplicationType.NONE, Config.class, "sealedconfig.foo=bar",
|
||||
"spring.cloud.refresh.extra-refreshable:"
|
||||
+ ConfigProps.class.getName())) {
|
||||
context.getBean(ConfigProps.class);
|
||||
+ SealedConfigProps.class.getName())) {
|
||||
context.getBean(SealedConfigProps.class);
|
||||
context.getBean(ContextRefresher.class).refresh();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void neverRefreshable() {
|
||||
try (ConfigurableApplicationContext context = getApplicationContext(
|
||||
WebApplicationType.NONE, Config.class, "countingconfig.foo=bar",
|
||||
"spring.cloud.refresh.never-refreshable:"
|
||||
+ CountingConfigProps.class.getName())) {
|
||||
CountingConfigProps configProps = context.getBean(CountingConfigProps.class);
|
||||
context.getBean(ContextRefresher.class).refresh();
|
||||
assertThat(configProps.count)
|
||||
.as("config props was rebound when it should not have been")
|
||||
.hasValue(1);
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
|
||||
@EnableConfigurationProperties(ConfigProps.class)
|
||||
@EnableConfigurationProperties({ SealedConfigProps.class, CountingConfigProps.class })
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
@ConfigurationProperties("config")
|
||||
static class ConfigProps {
|
||||
@ConfigurationProperties("sealedconfig")
|
||||
static class SealedConfigProps {
|
||||
|
||||
private String foo;
|
||||
|
||||
@@ -113,4 +131,22 @@ public class RefreshAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@ConfigurationProperties("countingconfig")
|
||||
static class CountingConfigProps {
|
||||
|
||||
private final AtomicInteger count = new AtomicInteger();
|
||||
|
||||
private String foo;
|
||||
|
||||
public String getFoo() {
|
||||
return this.foo;
|
||||
}
|
||||
|
||||
public void setFoo(String foo) {
|
||||
count.incrementAndGet();
|
||||
this.foo = foo;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user