From 74afa3aaac72a209fd5f6162e77d6d2869b24b6d Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Wed, 3 May 2023 22:12:18 +0800 Subject: [PATCH] =?UTF-8?q?bugfix=20#I6XPN4=20@RefreshScope=20=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3=20=E5=BC=95=E8=B5=B7=20beanName=20=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E6=94=B9=E5=8F=98=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../liteflow/spring/ComponentScanner.java | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/liteflow-spring/src/main/java/com/yomahub/liteflow/spring/ComponentScanner.java b/liteflow-spring/src/main/java/com/yomahub/liteflow/spring/ComponentScanner.java index bf1978ad8..1e753b632 100644 --- a/liteflow-spring/src/main/java/com/yomahub/liteflow/spring/ComponentScanner.java +++ b/liteflow-spring/src/main/java/com/yomahub/liteflow/spring/ComponentScanner.java @@ -8,6 +8,7 @@ */ package com.yomahub.liteflow.spring; +import cn.hutool.core.annotation.AnnotationUtil; import cn.hutool.core.collection.CollStreamUtil; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjectUtil; @@ -28,6 +29,7 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; +import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.Arrays; import java.util.HashMap; @@ -42,6 +44,16 @@ import java.util.stream.Collectors; */ public class ComponentScanner implements BeanPostProcessor { + /** + * @RefreshScope 注解 bean 的前缀 + */ + private static final String REFRESH_SCOPE_ANN_BEAN_PREFIX = "scopedTarget."; + + /** + * @RefreshScope 注解完整类路径 + */ + private static final String REFRESH_SCOPE_ANN_CLASS_PATH = "org.springframework.cloud.context.config.annotation.RefreshScope"; + private static final Logger LOG = LoggerFactory.getLogger(ComponentScanner.class); public static Map nodeComponentMap = new HashMap<>(); @@ -79,7 +91,7 @@ public class ComponentScanner implements BeanPostProcessor { List nodeComponents = LiteFlowProxyUtil.proxy2NodeComponent(bean, beanName); nodeComponents.forEach(nodeComponent -> { String nodeId = nodeComponent.getNodeId(); - nodeId = StrUtil.isEmpty(nodeId) ? beanName : nodeId; + nodeId = StrUtil.isEmpty(nodeId) ? getRealBeanName(clazz, beanName) : nodeId; nodeComponentMap.put(nodeId, nodeComponent); }); // 只有注解支持单bean多Node,所以一个直接返回 @@ -93,7 +105,7 @@ public class ComponentScanner implements BeanPostProcessor { if (NodeComponent.class.isAssignableFrom(clazz)) { LOG.info("component[{}] has been found", beanName); NodeComponent nodeComponent = (NodeComponent) bean; - nodeComponentMap.put(beanName, nodeComponent); + nodeComponentMap.put(getRealBeanName(clazz, beanName), nodeComponent); return nodeComponent; } @@ -145,4 +157,24 @@ public class ComponentScanner implements BeanPostProcessor { nodeComponentMap.clear(); } + /** + * 获取真实的 beanName 1. @RefreshScope 注解标注的bean 名称前会多加一个 scopedTarget. + * + * @param clazz clazz + * @param beanName beanName + */ + private String getRealBeanName(Class clazz, String beanName) { + if (beanName.startsWith(REFRESH_SCOPE_ANN_BEAN_PREFIX)) { + Annotation[] annotations = AnnotationUtil.getAnnotations(clazz, true); + for (Annotation annotation : annotations) { + String name = annotation.annotationType().getName(); + if (REFRESH_SCOPE_ANN_CLASS_PATH.equals(name)) { + return beanName.replace(REFRESH_SCOPE_ANN_BEAN_PREFIX, ""); + } + + } + } + return beanName; + } + }