diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/core/NodeComponent.java b/liteflow-core/src/main/java/com/yomahub/liteflow/core/NodeComponent.java index 79865b752..1f2f06074 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/core/NodeComponent.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/core/NodeComponent.java @@ -34,13 +34,17 @@ public abstract class NodeComponent { private static final Logger LOG = LoggerFactory.getLogger(NodeComponent.class); - private TransmittableThreadLocal slotIndexTL = new TransmittableThreadLocal(); + private TransmittableThreadLocal slotIndexTL = new TransmittableThreadLocal<>(); @Autowired(required = false) private MonitorBus monitorBus; private String nodeId; + //这是自己的实例,取代this + //为何要设置这个,用this不行么,因为如果有aop去切的话,this在spring的aop里是切不到的。self对象有可能是代理过的对象 + private NodeComponent self; + //是否结束整个流程,这个只对串行流程有效,并行流程无效 private TransmittableThreadLocal isEndTL = new TransmittableThreadLocal<>(); @@ -56,7 +60,7 @@ public abstract class NodeComponent { ComponentScaner.cmpAroundAspect.beforeProcess(this.getNodeId(), slot); } - process(); + self.process(); // process后置处理 if (ObjectUtil.isNotNull(ComponentScaner.cmpAroundAspect)) { @@ -154,4 +158,12 @@ public abstract class NodeComponent { public void setNodeId(String nodeId) { this.nodeId = nodeId; } + + public NodeComponent getSelf() { + return self; + } + + public void setSelf(NodeComponent self) { + this.self = self; + } } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/FlowBus.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/FlowBus.java index 4ce599809..e98efc9bf 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/FlowBus.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/FlowBus.java @@ -14,6 +14,7 @@ import cn.hutool.core.map.MapUtil; import com.yomahub.liteflow.entity.flow.Chain; import com.yomahub.liteflow.entity.flow.Node; +import com.yomahub.liteflow.util.SpringAware; /** * 流程元数据类 diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/spring/ComponentScaner.java b/liteflow-core/src/main/java/com/yomahub/liteflow/spring/ComponentScaner.java index 2eafea230..136b3aea4 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/spring/ComponentScaner.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/spring/ComponentScaner.java @@ -14,6 +14,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; import org.springframework.core.Ordered; import org.springframework.core.PriorityOrdered; @@ -25,7 +26,7 @@ import com.yomahub.liteflow.util.LOGOPrinter; * 组件扫描类,只要是NodeComponent的实现类,都可以被这个扫描器扫到 * @author Bryan.Zhang */ -public class ComponentScaner implements BeanPostProcessor, PriorityOrdered { +public class ComponentScaner implements InstantiationAwareBeanPostProcessor { private static final Logger LOG = LoggerFactory.getLogger(ComponentScaner.class); @@ -39,19 +40,20 @@ public class ComponentScaner implements BeanPostProcessor, PriorityOrdered { } @Override - public int getOrder() { - return Ordered.LOWEST_PRECEDENCE; + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { + return bean; } @SuppressWarnings("rawtypes") @Override - public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { Class clazz = bean.getClass(); // 组件的扫描发现,扫到之后缓存到类属性map中 if (NodeComponent.class.isAssignableFrom(clazz)) { LOG.info("component[{}] has been found", beanName); NodeComponent nodeComponent = (NodeComponent) bean; nodeComponent.setNodeId(beanName); + nodeComponent.setSelf(nodeComponent); nodeComponentMap.put(beanName, nodeComponent); } @@ -63,9 +65,4 @@ public class ComponentScaner implements BeanPostProcessor, PriorityOrdered { return bean; } - - @Override - public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { - return bean; - } } diff --git a/liteflow-spring-boot-starter/src/main/java/com/yomahub/liteflow/springboot/LiteflowComponentScannerAutoConfiguration.java b/liteflow-spring-boot-starter/src/main/java/com/yomahub/liteflow/springboot/LiteflowComponentScannerAutoConfiguration.java index efeb21516..a415dbf8c 100644 --- a/liteflow-spring-boot-starter/src/main/java/com/yomahub/liteflow/springboot/LiteflowComponentScannerAutoConfiguration.java +++ b/liteflow-spring-boot-starter/src/main/java/com/yomahub/liteflow/springboot/LiteflowComponentScannerAutoConfiguration.java @@ -3,12 +3,14 @@ package com.yomahub.liteflow.springboot; import com.yomahub.liteflow.spring.ComponentScaner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.EnableAspectJAutoProxy; /** * 组件扫描器自动装配类 * @author Bryan.Zhang */ @Configuration +@EnableAspectJAutoProxy(exposeProxy = true) public class LiteflowComponentScannerAutoConfiguration { @Bean diff --git a/liteflow-spring-boot-starter/src/main/resources/META-INF/liteflow-default.properties b/liteflow-spring-boot-starter/src/main/resources/META-INF/liteflow-default.properties index 60ac4ce71..832357d8e 100644 --- a/liteflow-spring-boot-starter/src/main/resources/META-INF/liteflow-default.properties +++ b/liteflow-spring-boot-starter/src/main/resources/META-INF/liteflow-default.properties @@ -1,6 +1,6 @@ liteflow.rule-source=config/flow.xml liteflow.slot-size=1024 -liteflow.when-max-wait-second=15 +liteflow.when-max-wait-seconds=15 liteflow.when-max-workers=4 liteflow.when-queue-limit=512 liteflow.monitor.enable-log=false diff --git a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFCustomAOPTest.java b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFCustomAOPTest.java index b3e9e9033..b6b67f2cc 100644 --- a/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFCustomAOPTest.java +++ b/liteflow-test/src/test/java/com/yomahub/liteflow/test/aop/LFCustomAOPTest.java @@ -1,9 +1,12 @@ package com.yomahub.liteflow.test.aop; import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.core.NodeComponent; import com.yomahub.liteflow.entity.data.LiteflowResponse; import com.yomahub.liteflow.entity.data.Slot; import com.yomahub.liteflow.test.aop.aspect.CustomAspect; +import com.yomahub.liteflow.test.aop.cmp1.ACmp; +import com.yomahub.liteflow.util.SpringAware; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -43,7 +46,7 @@ public class LFCustomAOPTest { //测试自定义AOP,并行场景 @Test - public void testGlobalAopP() throws Exception{ + public void testCustomAopP() throws Exception{ LiteflowResponse response= flowExecutor.execute("chain2", "it's a request"); Assert.assertTrue(response.isSuccess()); Assert.assertEquals("before_after", response.getData().getData("a"));