mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-05-14 04:02:09 +08:00
enhancement #I8IDE5 对声明式代理底层代码进行增强
This commit is contained in:
@@ -6,6 +6,7 @@ import com.yomahub.liteflow.spi.ContextCmpInit;
|
||||
import com.yomahub.liteflow.spring.ComponentScanner;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Spring环境容器上下文组件初始化实现
|
||||
@@ -17,11 +18,7 @@ public class SpringContextCmpInit implements ContextCmpInit {
|
||||
|
||||
@Override
|
||||
public void initCmp() {
|
||||
for (Map.Entry<String, NodeComponent> componentEntry : ComponentScanner.nodeComponentMap.entrySet()) {
|
||||
if (!FlowBus.containNode(componentEntry.getKey())) {
|
||||
FlowBus.addManagedNode(componentEntry.getKey(), componentEntry.getValue());
|
||||
}
|
||||
}
|
||||
ComponentScanner.nodeComponentSet.forEach(FlowBus::addManagedNode);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -13,6 +13,7 @@ import cn.hutool.core.collection.CollStreamUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.annotation.LiteflowCmpDefine;
|
||||
import com.yomahub.liteflow.annotation.util.AnnoUtil;
|
||||
import com.yomahub.liteflow.aop.ICmpAroundAspect;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
@@ -22,19 +23,24 @@ import com.yomahub.liteflow.script.annotation.ScriptBean;
|
||||
import com.yomahub.liteflow.script.annotation.ScriptMethod;
|
||||
import com.yomahub.liteflow.script.proxy.ScriptBeanProxy;
|
||||
import com.yomahub.liteflow.script.proxy.ScriptMethodProxy;
|
||||
import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
|
||||
import com.yomahub.liteflow.spi.spring.SpringAware;
|
||||
import com.yomahub.liteflow.util.LOGOPrinter;
|
||||
import com.yomahub.liteflow.util.LiteFlowProxyUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -42,7 +48,7 @@ import java.util.stream.Collectors;
|
||||
*
|
||||
* @author Bryan.Zhang
|
||||
*/
|
||||
public class ComponentScanner implements BeanPostProcessor {
|
||||
public class ComponentScanner implements InstantiationAwareBeanPostProcessor {
|
||||
|
||||
/**
|
||||
* @RefreshScope 注解 bean 的前缀
|
||||
@@ -56,7 +62,7 @@ public class ComponentScanner implements BeanPostProcessor {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ComponentScanner.class);
|
||||
|
||||
public static Map<String, NodeComponent> nodeComponentMap = new HashMap<>();
|
||||
public static Set<String> nodeComponentSet = new HashSet<>();
|
||||
|
||||
private LiteflowConfig liteflowConfig;
|
||||
|
||||
@@ -75,13 +81,13 @@ public class ComponentScanner implements BeanPostProcessor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
return bean;
|
||||
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
|
||||
return InstantiationAwareBeanPostProcessor.super.postProcessBeforeInstantiation(beanClass, beanName);
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
Class clazz = LiteFlowProxyUtil.getUserClass(bean.getClass());
|
||||
|
||||
// 判断是不是声明式组件
|
||||
@@ -92,20 +98,26 @@ public class ComponentScanner implements BeanPostProcessor {
|
||||
nodeComponents.forEach(nodeComponent -> {
|
||||
String nodeId = nodeComponent.getNodeId();
|
||||
nodeId = StrUtil.isEmpty(nodeId) ? getRealBeanName(clazz, beanName) : nodeId;
|
||||
nodeComponentMap.put(nodeId, nodeComponent);
|
||||
nodeComponentSet.add(nodeId);
|
||||
});
|
||||
// 只有注解支持单bean多Node,所以一个直接返回
|
||||
if (nodeComponents.size() == 1) {
|
||||
return nodeComponents.get(0);
|
||||
|
||||
LiteflowCmpDefine liteflowCmpDefine = AnnotationUtil.getAnnotation(clazz, LiteflowCmpDefine.class);
|
||||
//liteflowCmpDefine为null说明是方法级别声明
|
||||
if (liteflowCmpDefine == null){
|
||||
nodeComponents.forEach(nodeComponent -> {
|
||||
String nodeId = nodeComponent.getNodeId();
|
||||
nodeId = StrUtil.isEmpty(nodeId) ? getRealBeanName(clazz, beanName) : nodeId;
|
||||
ContextAwareHolder.loadContextAware().registerBean(nodeId, nodeComponent);
|
||||
});
|
||||
}
|
||||
return bean;
|
||||
return nodeComponents.get(0);
|
||||
}
|
||||
|
||||
// 组件的扫描发现,扫到之后缓存到类属性map中
|
||||
if (NodeComponent.class.isAssignableFrom(clazz)) {
|
||||
LOG.info("component[{}] has been found", beanName);
|
||||
NodeComponent nodeComponent = (NodeComponent) bean;
|
||||
nodeComponentMap.put(getRealBeanName(clazz, beanName), nodeComponent);
|
||||
nodeComponentSet.add(getRealBeanName(clazz, beanName));
|
||||
return nodeComponent;
|
||||
}
|
||||
|
||||
@@ -150,11 +162,16 @@ public class ComponentScanner implements BeanPostProcessor {
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于清除 spring 上下文扫描到的组件实体
|
||||
*/
|
||||
public static void cleanCache() {
|
||||
nodeComponentMap.clear();
|
||||
nodeComponentSet.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.yomahub.liteflow.spring;
|
||||
|
||||
import cn.hutool.core.annotation.AnnotationUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import com.yomahub.liteflow.annotation.LiteflowCmpDefine;
|
||||
import com.yomahub.liteflow.annotation.LiteflowMethod;
|
||||
import com.yomahub.liteflow.spring.vo.MethodDeclWarpVo;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class MethodDeclBeanDefinition implements BeanDefinitionRegistryPostProcessor {
|
||||
@Override
|
||||
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
|
||||
DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory)registry;
|
||||
Map<String, BeanDefinition> beanDefinitionHolderMap = (Map<String, BeanDefinition>)ReflectUtil.getFieldValue(defaultListableBeanFactory, "mergedBeanDefinitions");
|
||||
|
||||
beanDefinitionHolderMap.entrySet().stream().filter(entry -> {
|
||||
Class<?> rawClass = entry.getValue().getResolvableType().getRawClass();
|
||||
LiteflowCmpDefine liteflowCmpDefine = AnnotationUtil.getAnnotation(rawClass, LiteflowCmpDefine.class);
|
||||
//必须是方法级别声明式,才进行动态BeanDefinition的注册
|
||||
if (liteflowCmpDefine == null){
|
||||
return Arrays.stream(rawClass.getMethods()).anyMatch(method -> AnnotationUtil.getAnnotation(method, LiteflowMethod.class) != null);
|
||||
}
|
||||
return false;
|
||||
}).map(new Function<Map.Entry<String, BeanDefinition>, MethodDeclWarpVo>() {
|
||||
@Override
|
||||
public MethodDeclWarpVo apply(Map.Entry<String, BeanDefinition> entry) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
System.out.println(defaultListableBeanFactory);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
System.out.println("bb");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.yomahub.liteflow.spring.vo;
|
||||
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
|
||||
public class MethodDeclWarpVo {
|
||||
|
||||
private String nodeId;
|
||||
|
||||
private NodeComponent nodeComponent;
|
||||
|
||||
public MethodDeclWarpVo(String nodeId, NodeComponent nodeComponent) {
|
||||
this.nodeId = nodeId;
|
||||
this.nodeComponent = nodeComponent;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user