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) - [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) - [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 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) { public static ConfigKVFile loadConfigKVFile(ConfigFileService configFileService, String namespace, String group, String fileName) {
ConfigKVFile configKVFile; ConfigKVFile configKVFile;
// unknown extension is resolved as properties file // unknown extension is resolved as yaml file
if (ConfigFileFormat.isPropertyFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) { if (ConfigFileFormat.isYamlFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) {
configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName);
}
else if (ConfigFileFormat.isYamlFile(fileName)) {
configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName); configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName);
} }
else if (ConfigFileFormat.isPropertyFile(fileName)) {
configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName);
}
else { else {
LOGGER.warn("[SCT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", namespace, group, fileName); 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) { catch (Exception ignored) {
// ignore // ignore
} }
processClass(bean, beanName, clazz, isRefreshScope);
for (Field field : findAllField(clazz)) { for (Field field : findAllField(clazz)) {
processField(bean, beanName, field, isRefreshScope); processField(bean, beanName, field, isRefreshScope);
@ -73,6 +74,11 @@ public abstract class PolarisProcessor implements BeanPostProcessor, PriorityOrd
return bean; 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. * subclass should implement this method to process field.
* @param bean bean * @param bean bean

@ -135,6 +135,15 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanDefini
return bean; 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 @Override
protected void processField(Object bean, String beanName, Field field, boolean isRefreshScope) { protected void processField(Object bean, String beanName, Field field, boolean isRefreshScope) {
// register @Value on field // register @Value on field

@ -87,6 +87,7 @@ public class RefreshScopeSpringProcessorTest {
.withConfiguration(AutoConfigurations.of(TestConfig5.class)) .withConfiguration(AutoConfigurations.of(TestConfig5.class))
.withConfiguration(AutoConfigurations.of(TestBeanProperties1.class)) .withConfiguration(AutoConfigurations.of(TestBeanProperties1.class))
.withConfiguration(AutoConfigurations.of(TestBeanProperties2.class)) .withConfiguration(AutoConfigurations.of(TestBeanProperties2.class))
.withConfiguration(AutoConfigurations.of(TestBeanProperties3.class))
.withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class)) .withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class))
.withAllowBeanDefinitionOverriding(true) .withAllowBeanDefinitionOverriding(true)
.withPropertyValues("spring.application.name=" + "conditionalOnConfigReflectEnabledTest") .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.map.key")).isTrue();
assertThat(springValueRegistry.isRefreshScopeKey("test.properties2.notExist")).isFalse(); 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 // @RefreshScope and @Bean on method, @Value bean in class
assertThat(springValueRegistry.isRefreshScopeKey("test.bean5.name")).isTrue(); 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 { static class InnerProperties {
private String name; private String name;

Loading…
Cancel
Save