From dd9d2014be1210ba34dac08943776c5a0a3958ce Mon Sep 17 00:00:00 2001 From: "everywhere.z" Date: Mon, 22 Jan 2024 16:12:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E7=9A=84=E5=A3=B0=E6=98=8E=E5=BC=8F?= =?UTF-8?q?=E6=94=B9=E9=80=A0=E6=94=AF=E6=8C=81=E4=BD=8E=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E7=9A=84spring?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../liteflow/spring/DeclBeanDefinition.java | 46 ++++++++++++++----- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/liteflow-spring/src/main/java/com/yomahub/liteflow/spring/DeclBeanDefinition.java b/liteflow-spring/src/main/java/com/yomahub/liteflow/spring/DeclBeanDefinition.java index 2a30ebb2a..7d4d396b6 100644 --- a/liteflow-spring/src/main/java/com/yomahub/liteflow/spring/DeclBeanDefinition.java +++ b/liteflow-spring/src/main/java/com/yomahub/liteflow/spring/DeclBeanDefinition.java @@ -4,6 +4,8 @@ import cn.hutool.core.annotation.AnnotationUtil; import cn.hutool.core.util.ReflectUtil; import com.yomahub.liteflow.annotation.LiteflowMethod; import com.yomahub.liteflow.core.proxy.DeclWarpBean; +import com.yomahub.liteflow.log.LFLog; +import com.yomahub.liteflow.log.LFLoggerManager; import com.yomahub.liteflow.spi.holder.DeclComponentParserHolder; import org.springframework.beans.BeansException; import org.springframework.beans.MutablePropertyValues; @@ -12,6 +14,7 @@ import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.*; +import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -23,38 +26,42 @@ import java.util.Map; * @since 2.11.4 */ public class DeclBeanDefinition implements BeanDefinitionRegistryPostProcessor { + + private final LFLog LOG = LFLoggerManager.getLogger(this.getClass()); @Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) registry; - Map beanDefinitionHolderMap = (Map)ReflectUtil.getFieldValue(defaultListableBeanFactory, "mergedBeanDefinitions"); - beanDefinitionHolderMap.entrySet().stream().filter(entry -> { - Class rawClass = entry.getValue().getResolvableType().getRawClass(); + String[] beanDefinitionNames = defaultListableBeanFactory.getBeanDefinitionNames(); + + Arrays.stream(beanDefinitionNames).filter(beanName -> { + BeanDefinition beanDefinition = defaultListableBeanFactory.getMergedBeanDefinition(beanName); + Class rawClass = getRawClassFromBeanDefinition(beanDefinition); if (rawClass == null){ return false; }else{ return Arrays.stream(rawClass.getMethods()).anyMatch(method -> AnnotationUtil.getAnnotation(method, LiteflowMethod.class) != null); } - }).forEach(entry -> { - Class rawClass = entry.getValue().getResolvableType().getRawClass(); + }).forEach(beanName -> { + BeanDefinition beanDefinition = defaultListableBeanFactory.getMergedBeanDefinition(beanName); + Class rawClass = getRawClassFromBeanDefinition(beanDefinition); List declWarpBeanList = DeclComponentParserHolder.loadDeclComponentParser().parseDeclBean(rawClass); declWarpBeanList.forEach(declWarpBean -> { - GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); - beanDefinition.setBeanClass(DeclWarpBean.class); - beanDefinition.setScope(ConfigurableBeanFactory.SCOPE_SINGLETON); + GenericBeanDefinition newBeanDefinition = new GenericBeanDefinition(); + newBeanDefinition.setBeanClass(DeclWarpBean.class); + newBeanDefinition.setScope(ConfigurableBeanFactory.SCOPE_SINGLETON); MutablePropertyValues mutablePropertyValues = new MutablePropertyValues(); mutablePropertyValues.add("nodeId", declWarpBean.getNodeId()); mutablePropertyValues.add("nodeName", declWarpBean.getNodeName()); mutablePropertyValues.add("nodeType", declWarpBean.getNodeType()); mutablePropertyValues.add("rawClazz", declWarpBean.getRawClazz()); mutablePropertyValues.add("methodWrapBeanList", declWarpBean.getMethodWrapBeanList()); - mutablePropertyValues.add("rawBean", entry.getValue()); - beanDefinition.setPropertyValues(mutablePropertyValues); + mutablePropertyValues.add("rawBean", beanDefinition); + newBeanDefinition.setPropertyValues(mutablePropertyValues); defaultListableBeanFactory.setAllowBeanDefinitionOverriding(true); - defaultListableBeanFactory.registerBeanDefinition(declWarpBean.getNodeId(), beanDefinition); + defaultListableBeanFactory.registerBeanDefinition(declWarpBean.getNodeId(), newBeanDefinition); }); - }); } @@ -62,4 +69,19 @@ public class DeclBeanDefinition implements BeanDefinitionRegistryPostProcessor { public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { } + + private Class getRawClassFromBeanDefinition(BeanDefinition beanDefinition){ + try{ + Method method = ReflectUtil.getMethodByName(DeclBeanDefinition.class, "getResolvableType"); + if (method != null){ + Object resolvableType = ReflectUtil.invoke(beanDefinition, method); + return ReflectUtil.invoke(resolvableType, "getRawClass"); + }else{ + return ReflectUtil.invoke(beanDefinition, "getTargetType"); + } + }catch (Exception e){ + LOG.error("An error occurred while obtaining the rowClass.",e); + return null; + } + } }