feat: 解决 @ConfigurationProperties注解的类属性值更新 问题

This commit is contained in:
骑着蜗牛追导弹 2024-12-07 14:46:54 +08:00
parent 3ce36ef0cb
commit 7a524ed09f
5 changed files with 19 additions and 4 deletions

View File

@ -19,5 +19,10 @@
<artifactId>kenaito-config-common</artifactId> <artifactId>kenaito-config-common</artifactId>
<version>1.0</version> <version>1.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<version>4.2.0</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -3,6 +3,8 @@ package cn.odboy.config.context;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import java.util.Map; import java.util.Map;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.cloud.context.refresh.ConfigDataContextRefresher;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.MutablePropertySources;
@ -20,6 +22,7 @@ import org.springframework.stereotype.Component;
public class ClientPropertyHelper { public class ClientPropertyHelper {
private final ConfigurableEnvironment environment; private final ConfigurableEnvironment environment;
private final ValueAnnotationProcessor valueAnnotationProcessor; private final ValueAnnotationProcessor valueAnnotationProcessor;
private final ConfigDataContextRefresher configDataContextRefresher;
/** /**
* 动态更新配置值 * 动态更新配置值
@ -40,6 +43,10 @@ public class ClientPropertyHelper {
} }
// 单独更新@Value对应的值 // 单独更新@Value对应的值
valueAnnotationProcessor.setValue(propertyName, value); valueAnnotationProcessor.setValue(propertyName, value);
// 刷新上下文(解决 @ConfigurationProperties注解的类属性值更新 问题)
// Spring Cloud只会对被@RefreshScope和@ConfigurationProperties标注的bean进行刷新
// 这个方法主要做了两件事刷新配置源也就是PropertySource然后刷新了@ConfigurationProperties注解的类
configDataContextRefresher.refresh();
} }
} }
} }

View File

@ -11,7 +11,7 @@ import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
* 加载并处理@Value对应的引用, @ConfigurationProperties注解下的值会自动刷新所以不用管 * 加载并处理@Value对应的引用
* *
* @author odboy * @author odboy
* @date 2024-12-07 * @date 2024-12-07

View File

@ -21,4 +21,5 @@ public class ConfigCenterProperties {
private String dataId; private String dataId;
private String env; private String env;
private String cacheDir; private String cacheDir;
private String test;
} }

View File

@ -20,16 +20,18 @@ import org.springframework.web.bind.annotation.RestController;
public class DemoController { public class DemoController {
@Value("${kenaito.config-center.test}") @Value("${kenaito.config-center.test}")
private String testStr; private String testStr;
private final ConfigCenterProperties configCenterProperties;
private final ClientPropertyHelper clientPropertyHelper; private final ClientPropertyHelper clientPropertyHelper;
/** 配置变化了 */ /** 配置变化了 */
@GetMapping("/test") @GetMapping("/test")
public ResponseEntity<Object> test() { public ResponseEntity<Object> test() {
System.err.println("testStr=" + testStr);
String propertyName = "kenaito.config-center.test"; String propertyName = "kenaito.config-center.test";
System.err.println("@Value注解的值1=" + testStr);
System.err.println("@ConfigurationProperties注解的值1=" + configCenterProperties.getTest());
clientPropertyHelper.updateValue(propertyName, "Hello World"); clientPropertyHelper.updateValue(propertyName, "Hello World");
System.err.println("testStr=" + testStr); System.err.println("@Value注解的值2=" + testStr);
System.err.println("@ConfigurationProperties注解的值2=" + configCenterProperties.getTest());
return ResponseEntity.ok("success"); return ResponseEntity.ok("success");
} }
} }