feature #I54VBS 从设计上改善NodeComponent,使之变成声明式的组件

This commit is contained in:
bryan31
2022-05-16 11:58:18 +08:00
parent 3e6547b1e8
commit 935506a388
339 changed files with 8359 additions and 16 deletions

View File

@@ -4,11 +4,13 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import com.yomahub.liteflow.spi.ContextAware;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
/**
* 基于代码形式的spring上下文工具类
@@ -64,6 +66,14 @@ public class SpringAware implements ApplicationContextAware, ContextAware {
return registerBean(c.getName(), c);
}
@Override
public <T> T registerBean(String beanName, Object bean) {
ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) applicationContext;
DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext.getAutowireCapableBeanFactory();
defaultListableBeanFactory.registerSingleton(beanName,bean);
return (T) configurableApplicationContext.getBean(beanName);
}
public <T> T registerOrGet(String beanName, Class<T> clazz) {
if (ObjectUtil.isNull(applicationContext)){
return null;

View File

@@ -12,6 +12,7 @@ import com.yomahub.liteflow.aop.ICmpAroundAspect;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.property.LiteflowConfig;
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;
@@ -55,17 +56,29 @@ public class ComponentScanner implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
Class clazz = bean.getClass();
//判断是不是声明式组件
//如果是就缓存到类属性的map中
if (LiteFlowProxyUtil.isMarkedCmp(bean.getClass())){
LOG.info("proxy component[{}] has been found", beanName);
NodeComponent nodeComponent = LiteFlowProxyUtil.proxy2NodeComponent(bean, beanName);
nodeComponentMap.put(beanName, nodeComponent);
return nodeComponent;
}
// 组件的扫描发现扫到之后缓存到类属性map中
if (NodeComponent.class.isAssignableFrom(clazz)) {
LOG.info("component[{}] has been found", beanName);
NodeComponent nodeComponent = (NodeComponent) bean;
nodeComponentMap.put(beanName, nodeComponent);
return nodeComponent;
}
// 组件Aop的实现类加载
if (ICmpAroundAspect.class.isAssignableFrom(clazz)) {
LOG.info("component aspect implement[{}] has been found", beanName);
cmpAroundAspect = (ICmpAroundAspect) bean;
return cmpAroundAspect;
}
return bean;