feat: 解决 @ConfigurationProperties注解的类属性值更新 问题
This commit is contained in:
parent
3ce36ef0cb
commit
7a524ed09f
|
@ -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>
|
||||||
|
|
|
@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue