Skip to content

Commit bff2c95

Browse files
quaffryanjbaxter
authored andcommitted
Support configuring bean names as well as bean types for extra-refreshable and never-refreshable (#1457)
Thanks to spring-projects/spring-boot#22403, applications could define a bean in addition to an auto-configured bean of the same type, then we should support configuring bean names for fine-grained control. Signed-off-by: Yanming Zhou <[email protected]>
1 parent 9450a2d commit bff2c95

File tree

5 files changed

+37
-10
lines changed

5 files changed

+37
-10
lines changed

docs/modules/ROOT/partials/_configprops.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@
7676
|spring.cloud.loadbalancer.zone | | Spring Cloud LoadBalancer zone.
7777
|spring.cloud.refresh.additional-property-sources-to-retain | | Additional property sources to retain during a refresh. Typically only system property sources are retained. This property allows property sources, such as property sources created by EnvironmentPostProcessors to be retained as well.
7878
|spring.cloud.refresh.enabled | `+++true+++` | Enables autoconfiguration for the refresh scope and associated features.
79-
|spring.cloud.refresh.extra-refreshable | `+++true+++` | Additional class names for beans to post process into refresh scope.
80-
|spring.cloud.refresh.never-refreshable | `+++true+++` | Comma separated list of class names for beans to never be refreshed or rebound.
79+
|spring.cloud.refresh.extra-refreshable | `+++true+++` | Additional bean names or class names for beans to post process into refresh scope.
80+
|spring.cloud.refresh.never-refreshable | `+++true+++` | Comma separated list of bean names or class names for beans to never be refreshed or rebound.
8181
|spring.cloud.refresh.on-restart.enabled | `+++true+++` | Enable refreshing context on start.
8282
|spring.cloud.service-registry.auto-registration.enabled | `+++true+++` | Whether service auto-registration is enabled. Defaults to true.
8383
|spring.cloud.service-registry.auto-registration.fail-fast | `+++false+++` | Whether startup fails if there is no AutoServiceRegistration. Defaults to false.

spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/RefreshAutoConfiguration.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -63,6 +63,7 @@
6363
* @author Dave Syer
6464
* @author Venil Noronha
6565
* @author Olga Maciaszek-Sharma
66+
* @author Yanming Zhou
6667
*/
6768
@Configuration(proxyBeanMethods = false)
6869
@ConditionalOnClass(RefreshScope.class)
@@ -209,6 +210,9 @@ private boolean isApplicable(BeanDefinitionRegistry registry, String name, BeanD
209210
// Already refresh scoped
210211
return false;
211212
}
213+
if (this.refreshables.contains(name)) {
214+
return true;
215+
}
212216
String type = definition.getBeanClassName();
213217
if (!StringUtils.hasText(type) && registry instanceof BeanFactory) {
214218
Class<?> cls = ((BeanFactory) registry).getType(name);

spring-cloud-context/src/main/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -47,6 +47,7 @@
4747
* the <code>@ConfigurationProperties</code> bean.
4848
*
4949
* @author Dave Syer
50+
* @author Yanming Zhou
5051
* @see RefreshScope for a deeper and optionally more focused refresh of bean components.
5152
*
5253
*/
@@ -131,7 +132,7 @@ private boolean rebind(String name, ApplicationContext appContext) {
131132
// TODO: determine a more general approach to fix this.
132133
// see
133134
// https://github.com/spring-cloud/spring-cloud-commons/issues/571
134-
if (getNeverRefreshable().contains(bean.getClass().getName())) {
135+
if (getNeverRefreshable().contains(bean.getClass().getName()) || getNeverRefreshable().contains(name)) {
135136
return false; // ignore
136137
}
137138
appContext.getAutowireCapableBeanFactory().destroyBean(bean);

spring-cloud-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@
3939
{
4040
"name": "spring.cloud.refresh.extra-refreshable",
4141
"type": "java.util.Set<java.lang.String>",
42-
"description": "Additional class names for beans to post process into refresh scope.",
42+
"description": "Additional bean names or class names for beans to post process into refresh scope.",
4343
"defaultValue": true
4444
},
4545
{
4646
"name": "spring.cloud.refresh.never-refreshable",
4747
"type": "java.lang.String",
48-
"description": "Comma separated list of class names for beans to never be refreshed or rebound.",
48+
"description": "Comma separated list of bean names or class names for beans to never be refreshed or rebound.",
4949
"defaultValue": true
5050
},
5151
{

spring-cloud-context/src/test/java/org/springframework/cloud/autoconfigure/RefreshAutoConfigurationTests.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -41,6 +41,7 @@
4141
/**
4242
* @author Dave Syer
4343
* @author Olga Maciaszek-Sharma
44+
* @author Yanming Zhou
4445
*/
4546
@ExtendWith(OutputCaptureExtension.class)
4647
class RefreshAutoConfigurationTests {
@@ -79,7 +80,7 @@ void refreshables() {
7980
}
8081

8182
@Test
82-
public void extraRefreshables() {
83+
public void extraRefreshableWithClassName() {
8384
try (ConfigurableApplicationContext context = getApplicationContext(WebApplicationType.NONE, Config.class,
8485
"sealedconfig.foo=bar",
8586
"spring.cloud.refresh.extra-refreshable:" + SealedConfigProps.class.getName())) {
@@ -89,7 +90,17 @@ public void extraRefreshables() {
8990
}
9091

9192
@Test
92-
void neverRefreshable() {
93+
public void extraRefreshableWithBeanName() {
94+
String beanName = "sealedconfig-" + SealedConfigProps.class.getName();
95+
try (ConfigurableApplicationContext context = getApplicationContext(WebApplicationType.NONE, Config.class,
96+
"sealedconfig.foo=bar", "spring.cloud.refresh.extra-refreshable:" + beanName)) {
97+
context.getBean(SealedConfigProps.class);
98+
context.getBean(ContextRefresher.class).refresh();
99+
}
100+
}
101+
102+
@Test
103+
void neverRefreshableWithClassName() {
93104
try (ConfigurableApplicationContext context = getApplicationContext(WebApplicationType.NONE, Config.class,
94105
"countingconfig.foo=bar",
95106
"spring.cloud.refresh.never-refreshable:" + CountingConfigProps.class.getName())) {
@@ -99,6 +110,17 @@ void neverRefreshable() {
99110
}
100111
}
101112

113+
@Test
114+
void neverRefreshableWithBeanName() {
115+
String beanName = "countingconfig-" + CountingConfigProps.class.getName();
116+
try (ConfigurableApplicationContext context = getApplicationContext(WebApplicationType.NONE, Config.class,
117+
"countingconfig.foo=bar", "spring.cloud.refresh.never-refreshable:" + beanName)) {
118+
CountingConfigProps configProps = context.getBean(CountingConfigProps.class);
119+
context.getBean(ContextRefresher.class).refresh();
120+
assertThat(configProps.count).as("config props was rebound when it should not have been").hasValue(1);
121+
}
122+
}
123+
102124
@Test
103125
void refreshScopeLifecylePresentByDefault() {
104126
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(RefreshAutoConfiguration.class))

0 commit comments

Comments
 (0)