From 7b0652e8e7e58d25570d42241b479ab1cdffff3e Mon Sep 17 00:00:00 2001 From: odboy Date: Sat, 7 Dec 2024 05:52:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8A=A8=E6=80=81=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ValueAnnotationBeanPostProcessor.java | 63 +++++++++++++++++++ .../cn/odboy/config/netty/ConfigClient.java | 2 +- .../java/cn/odboy/rest/DemoController.java | 21 ++----- 3 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 kenaito-config-core/src/main/java/cn/odboy/config/context/ValueAnnotationBeanPostProcessor.java diff --git a/kenaito-config-core/src/main/java/cn/odboy/config/context/ValueAnnotationBeanPostProcessor.java b/kenaito-config-core/src/main/java/cn/odboy/config/context/ValueAnnotationBeanPostProcessor.java new file mode 100644 index 0000000..43c9acb --- /dev/null +++ b/kenaito-config-core/src/main/java/cn/odboy/config/context/ValueAnnotationBeanPostProcessor.java @@ -0,0 +1,63 @@ +package cn.odboy.config.context; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.stereotype.Component; + +@Component +public class ValueAnnotationBeanPostProcessor implements BeanPostProcessor { + private final Logger logger = LoggerFactory.getLogger(ValueAnnotationBeanPostProcessor.class); + private final Map nameFieldMap = new HashMap<>(); + private final Map nameBeanMap = new HashMap<>(); + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + Class clazz = bean.getClass(); + while (clazz != null) { + for (Field field : clazz.getDeclaredFields()) { + if (field.isAnnotationPresent(Value.class)) { + field.setAccessible(true); + // 对@Value的处理 + Value annotation = field.getAnnotation(Value.class); + String propertyName = annotation.value(); + String substring = + propertyName.substring(propertyName.indexOf("${") + 2, propertyName.lastIndexOf("}")); + // 处理带有默认值的配置 + propertyName = substring; + if (substring.contains(":")) { + String[] splits = substring.split(":"); + if (splits.length == 2) { + propertyName = splits[0]; + } + } + nameFieldMap.put(propertyName, field); + nameBeanMap.put(propertyName, bean); + } + } + clazz = clazz.getSuperclass(); + } + return bean; + } + + public void setValue(String propertyName, Object value) { + try { + Field field = nameFieldMap.getOrDefault(propertyName, null); + if (field != null) { + field.setAccessible(true); + field.set(nameBeanMap.get(propertyName), value); + } + } catch (IllegalAccessException e) { + logger.error("配置 {} 字段值 {} 失败", propertyName, value, e); + } + } + + public Map getNameFieldMap() { + return nameFieldMap; + } +} diff --git a/kenaito-config-core/src/main/java/cn/odboy/config/netty/ConfigClient.java b/kenaito-config-core/src/main/java/cn/odboy/config/netty/ConfigClient.java index 79d8ac2..172b55d 100644 --- a/kenaito-config-core/src/main/java/cn/odboy/config/netty/ConfigClient.java +++ b/kenaito-config-core/src/main/java/cn/odboy/config/netty/ConfigClient.java @@ -43,7 +43,7 @@ public class ConfigClient { private static volatile ConfigClient instance; /** 最大重试次数 */ - private static final int MAX_RETRY_COUNT = 5; + private static final int MAX_RETRY_COUNT = 2; /** 当前重试次数 */ private static int retryCount = 0; diff --git a/kenaito-config-demo/src/main/java/cn/odboy/rest/DemoController.java b/kenaito-config-demo/src/main/java/cn/odboy/rest/DemoController.java index bbdb7e8..717f912 100644 --- a/kenaito-config-demo/src/main/java/cn/odboy/rest/DemoController.java +++ b/kenaito-config-demo/src/main/java/cn/odboy/rest/DemoController.java @@ -1,11 +1,8 @@ package cn.odboy.rest; -import java.util.HashMap; -import java.util.Map; +import cn.odboy.config.context.ValueAnnotationBeanPostProcessor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.core.env.MapPropertySource; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -23,21 +20,15 @@ public class DemoController { @Value("${kenaito.config-center.demo:123}") private String demoStr; - @Autowired private ConfigurableEnvironment environment; + @Autowired private ValueAnnotationBeanPostProcessor valueAnnotationBeanPostProcessor; /** 配置变化了 */ @GetMapping("/test") public ResponseEntity test() { - String key = "kenaito.config-center.demo"; - String property = environment.getProperty(key); - System.err.println("property=" + property); - Map properties = new HashMap<>(); - properties.put(key, "Hello World!"); - MapPropertySource propertySource = new MapPropertySource("dynamicProperties", properties); - // 将新的属性源添加到 ConfigurableEnvironment 中 - environment.getPropertySources().addFirst(propertySource); - String property1 = environment.getProperty(key); - System.err.println("property1=" + property1); + System.err.println("demoStr=" + demoStr); + String propertyName = "kenaito.config-center.demo"; + valueAnnotationBeanPostProcessor.setValue(propertyName, "xxxxxxxxxx"); + System.err.println("demoStr=" + demoStr); return ResponseEntity.ok("success"); } }