Skip to content

Commit

Permalink
Support configuring bean names as well as bean types for extra-refres…
Browse files Browse the repository at this point in the history
…hable 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]>
  • Loading branch information
quaff authored Jan 24, 2025
1 parent c81458d commit b4dac30
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
4 changes: 2 additions & 2 deletions docs/modules/ROOT/partials/_configprops.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@
|spring.cloud.loadbalancer.zone | | Spring Cloud LoadBalancer zone.
|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.
|spring.cloud.refresh.enabled | `+++true+++` | Enables autoconfiguration for the refresh scope and associated features.
|spring.cloud.refresh.extra-refreshable | `+++true+++` | Additional class names for beans to post process into refresh scope.
|spring.cloud.refresh.never-refreshable | `+++true+++` | Comma separated list of class names for beans to never be refreshed or rebound.
|spring.cloud.refresh.extra-refreshable | `+++true+++` | Additional bean names or class names for beans to post process into refresh scope.
|spring.cloud.refresh.never-refreshable | `+++true+++` | Comma separated list of bean names or class names for beans to never be refreshed or rebound.
|spring.cloud.refresh.on-restart.enabled | `+++true+++` | Enable refreshing context on start.
|spring.cloud.service-registry.auto-registration.enabled | `+++true+++` | Whether service auto-registration is enabled. Defaults to true.
|spring.cloud.service-registry.auto-registration.fail-fast | `+++false+++` | Whether startup fails if there is no AutoServiceRegistration. Defaults to false.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2025 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.
Expand Down Expand Up @@ -63,6 +63,7 @@
* @author Dave Syer
* @author Venil Noronha
* @author Olga Maciaszek-Sharma
* @author Yanming Zhou
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(RefreshScope.class)
Expand Down Expand Up @@ -209,6 +210,9 @@ private boolean isApplicable(BeanDefinitionRegistry registry, String name, BeanD
// Already refresh scoped
return false;
}
if (this.refreshables.contains(name)) {
return true;
}
String type = definition.getBeanClassName();
if (!StringUtils.hasText(type) && registry instanceof BeanFactory) {
Class<?> cls = ((BeanFactory) registry).getType(name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2025 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.
Expand Down Expand Up @@ -47,6 +47,7 @@
* the <code>@ConfigurationProperties</code> bean.
*
* @author Dave Syer
* @author Yanming Zhou
* @see RefreshScope for a deeper and optionally more focused refresh of bean components.
*
*/
Expand Down Expand Up @@ -131,7 +132,7 @@ private boolean rebind(String name, ApplicationContext appContext) {
// 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())) {
if (getNeverRefreshable().contains(bean.getClass().getName()) || getNeverRefreshable().contains(name)) {
return false; // ignore
}
appContext.getAutowireCapableBeanFactory().destroyBean(bean);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
{
"name": "spring.cloud.refresh.extra-refreshable",
"type": "java.util.Set<java.lang.String>",
"description": "Additional class names for beans to post process into refresh scope.",
"description": "Additional bean names or 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.",
"description": "Comma separated list of bean names or class names for beans to never be refreshed or rebound.",
"defaultValue": true
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2025 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.
Expand Down Expand Up @@ -41,6 +41,7 @@
/**
* @author Dave Syer
* @author Olga Maciaszek-Sharma
* @author Yanming Zhou
*/
@ExtendWith(OutputCaptureExtension.class)
class RefreshAutoConfigurationTests {
Expand Down Expand Up @@ -79,7 +80,7 @@ void refreshables() {
}

@Test
public void extraRefreshables() {
public void extraRefreshableWithClassName() {
try (ConfigurableApplicationContext context = getApplicationContext(WebApplicationType.NONE, Config.class,
"sealedconfig.foo=bar",
"spring.cloud.refresh.extra-refreshable:" + SealedConfigProps.class.getName())) {
Expand All @@ -89,7 +90,17 @@ public void extraRefreshables() {
}

@Test
void neverRefreshable() {
public void extraRefreshableWithBeanName() {
String beanName = "sealedconfig-" + SealedConfigProps.class.getName();
try (ConfigurableApplicationContext context = getApplicationContext(WebApplicationType.NONE, Config.class,
"sealedconfig.foo=bar", "spring.cloud.refresh.extra-refreshable:" + beanName)) {
context.getBean(SealedConfigProps.class);
context.getBean(ContextRefresher.class).refresh();
}
}

@Test
void neverRefreshableWithClassName() {
try (ConfigurableApplicationContext context = getApplicationContext(WebApplicationType.NONE, Config.class,
"countingconfig.foo=bar",
"spring.cloud.refresh.never-refreshable:" + CountingConfigProps.class.getName())) {
Expand All @@ -99,6 +110,17 @@ void neverRefreshable() {
}
}

@Test
void neverRefreshableWithBeanName() {
String beanName = "countingconfig-" + CountingConfigProps.class.getName();
try (ConfigurableApplicationContext context = getApplicationContext(WebApplicationType.NONE, Config.class,
"countingconfig.foo=bar", "spring.cloud.refresh.never-refreshable:" + beanName)) {
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);
}
}

@Test
void refreshScopeLifecylePresentByDefault() {
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(RefreshAutoConfiguration.class))
Expand Down

0 comments on commit b4dac30

Please sign in to comment.