fix: fix watch tsf config, fix bean refresh with RefreshScope and ConfigurationProperties. (#1512)

Co-authored-by: shedfreewu <49236872+shedfreewu@users.noreply.github.com>
pull/1535/head
Haotian Zhang 6 months ago committed by GitHub
parent 70a32f216d
commit 612188b338
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -53,3 +53,4 @@
- [feat:support default instance circuit breaker rule.](https://github.com/Tencent/spring-cloud-tencent/pull/1506)
- [docs:update JDK version configuration in GitHub Actions.](https://github.com/Tencent/spring-cloud-tencent/pull/1507)
- [fix: fix count circuit breaker in gateway & return 404 when context api does not match.](https://github.com/Tencent/spring-cloud-tencent/pull/1509)
- [fix:fix watch tsf config, fix bean refresh with RefreshScope and ConfigurationProperties.](https://github.com/Tencent/spring-cloud-tencent/pull/1512)

@ -118,13 +118,13 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
public static ConfigKVFile loadConfigKVFile(ConfigFileService configFileService, String namespace, String group, String fileName) {
ConfigKVFile configKVFile;
// unknown extension is resolved as properties file
if (ConfigFileFormat.isPropertyFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) {
configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName);
}
else if (ConfigFileFormat.isYamlFile(fileName)) {
// unknown extension is resolved as yaml file
if (ConfigFileFormat.isYamlFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) {
configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName);
}
else if (ConfigFileFormat.isPropertyFile(fileName)) {
configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName);
}
else {
LOGGER.warn("[SCT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", namespace, group, fileName);

@ -58,6 +58,7 @@ public abstract class PolarisProcessor implements BeanPostProcessor, PriorityOrd
catch (Exception ignored) {
// ignore
}
processClass(bean, beanName, clazz, isRefreshScope);
for (Field field : findAllField(clazz)) {
processField(bean, beanName, field, isRefreshScope);
@ -73,6 +74,11 @@ public abstract class PolarisProcessor implements BeanPostProcessor, PriorityOrd
return bean;
}
/**
* subclass should implement this method to process class.
*/
protected abstract void processClass(Object bean, String beanName, Class<?> clazz, boolean isRefreshScope);
/**
* subclass should implement this method to process field.
* @param bean bean

@ -135,6 +135,15 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanDefini
return bean;
}
@Override
protected void processClass(Object bean, String beanName, Class<?> clazz, boolean isRefreshScope) {
ConfigurationProperties configurationProperties = clazz.getAnnotation(ConfigurationProperties.class);
if (configurationProperties != null && isRefreshScope) {
// A bean with RefreshScope and ConfigurationProperties can't be refreshed by reflection, because it's proxied by Spring AOP. Related keys needs to be registered
parseConfigurationPropertiesKeys(configurationProperties, clazz);
}
}
@Override
protected void processField(Object bean, String beanName, Field field, boolean isRefreshScope) {
// register @Value on field

@ -87,6 +87,7 @@ public class RefreshScopeSpringProcessorTest {
.withConfiguration(AutoConfigurations.of(TestConfig5.class))
.withConfiguration(AutoConfigurations.of(TestBeanProperties1.class))
.withConfiguration(AutoConfigurations.of(TestBeanProperties2.class))
.withConfiguration(AutoConfigurations.of(TestBeanProperties3.class))
.withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class))
.withAllowBeanDefinitionOverriding(true)
.withPropertyValues("spring.application.name=" + "conditionalOnConfigReflectEnabledTest")
@ -124,6 +125,9 @@ public class RefreshScopeSpringProcessorTest {
assertThat(springValueRegistry.isRefreshScopeKey("test.properties2.map.key")).isTrue();
assertThat(springValueRegistry.isRefreshScopeKey("test.properties2.notExist")).isFalse();
// @RefreshScope and @ConfigurationProperties on @Component bean
assertThat(springValueRegistry.isRefreshScopeKey("test.properties3.name")).isTrue();
assertThat(springValueRegistry.isRefreshScopeKey("test.properties3.notExist")).isFalse();
// @RefreshScope and @Bean on method, @Value bean in class
assertThat(springValueRegistry.isRefreshScopeKey("test.bean5.name")).isTrue();
});
@ -319,6 +323,21 @@ public class RefreshScopeSpringProcessorTest {
}
}
@Component
@RefreshScope
@ConfigurationProperties(prefix = "test.properties3")
static class TestBeanProperties3 {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
static class InnerProperties {
private String name;

Loading…
Cancel
Save