24282 2 年之前
父節點
當前提交
607033eeba

+ 8 - 9
hx-flow/src/main/java/com/fjhx/flow/config/FlowInitializingBean.java

@@ -1,30 +1,29 @@
 package com.fjhx.flow.config;
 
-import cn.hutool.extra.spring.SpringUtil;
 import com.fjhx.flow.core.FlowBean;
 import com.fjhx.flow.core.FlowDelegate;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.config.BeanPostProcessor;
 import org.springframework.context.annotation.Configuration;
 
 /**
- * 流程初始化bean
+ * 利用spring后置处理器注册流程代理对象
  */
 @Slf4j
 @Configuration
-public class FlowInitializingBean implements InitializingBean {
+public class FlowInitializingBean implements BeanPostProcessor {
 
     @Override
-    public void afterPropertiesSet() {
+    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
 
-        String[] beanNamesForType = SpringUtil.getBeanNamesForType(FlowDelegate.class);
-
-        for (String beanName : beanNamesForType) {
-            FlowDelegate flowDelegate = SpringUtil.getBean(beanName, FlowDelegate.class);
+        if (bean instanceof FlowDelegate) {
+            FlowDelegate flowDelegate = (FlowDelegate) bean;
             FlowBean.addBean(flowDelegate.getFlowKey(), beanName);
             log.info("流程:{} 注册成功", flowDelegate.getFlowKey());
         }
 
+        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
     }
 
 }

+ 41 - 43
hx-flow/src/main/java/com/fjhx/flow/service/flow/impl/FlowProcessServiceImpl.java

@@ -68,9 +68,6 @@ public class FlowProcessServiceImpl implements FlowProcessService {
         // 获取流程委托对象
         FlowDelegate flowDelegate = FlowBean.getBean(dto.getFlowKey());
 
-        // 找到代码定义的流程bean
-        Class<? extends FlowDelegate> flowDelegateCls = flowDelegate.getClass();
-
         // 查找可用流程
         FlowDefinition flowDefinition = flowDefinitionService.getOne(q -> q
                 .eq(FlowDefinition::getFlowKey, dto.getFlowKey())
@@ -158,7 +155,7 @@ public class FlowProcessServiceImpl implements FlowProcessService {
             FlowThreadLocalUtil.setBusinessId(businessId);
 
             // 执行结束方法
-            invokeEndMethod(nextUserNode, flowDelegateCls, flowDelegate);
+            invokeEndMethod(nextUserNode, flowDelegate);
 
             // 结束流程明细
             FlowExampleDetail endExampleDetail = new FlowExampleDetail();
@@ -215,9 +212,6 @@ public class FlowProcessServiceImpl implements FlowProcessService {
         // 获取流程委托对象
         FlowDelegate flowDelegate = FlowBean.getBean(flowExample.getFlowKey());
 
-        // 找到代码定义的流程bean
-        Class<? extends FlowDelegate> flowDelegateCls = flowDelegate.getClass();
-
         // 流程节点列表
         List<FlowDefinitionNode> flowDefinitionNodeList = flowDefinitionNodeService.list(q -> q
                 .eq(FlowDefinitionNode::getFlowDefinitionId, flowExample.getDefinitionId()));
@@ -316,10 +310,10 @@ public class FlowProcessServiceImpl implements FlowProcessService {
         String handlingMethod = currentNode.getHandlingMethod();
         if (StrUtil.isNotBlank(handlingMethod)) {
             try {
-                invokeMethod(flowDelegateCls, flowDelegate, handlingMethod);
+                invokeMethod(flowDelegate, handlingMethod);
             } catch (Exception e) {
                 log.error("跳转节点方法异常", e);
-                throw new ServiceException("跳转节点方法异常");
+                throw new ServiceException("跳转节点方法异常" + e.getMessage());
             }
         }
 
@@ -338,7 +332,7 @@ public class FlowProcessServiceImpl implements FlowProcessService {
         if (FlowStatusEnum.HAVE_PASSED.getKey().equals(flowExample.getStatus())) {
 
             // 执行结束方法
-            invokeEndMethod(nextUserNode, flowDelegateCls, flowDelegate);
+            invokeEndMethod(nextUserNode, flowDelegate);
 
             // 结束流程明细
             FlowExampleDetail endExampleDetail = new FlowExampleDetail();
@@ -365,11 +359,11 @@ public class FlowProcessServiceImpl implements FlowProcessService {
     /**
      * 执行节点方法
      *
-     * @param flowDelegateCls 流程代理class
-     * @param flowDelegate    流程代理对象
-     * @param handlingMethod  流程执行方法名
+     * @param flowDelegate   流程代理对象
+     * @param handlingMethod 流程执行方法名
      */
-    private void invokeMethod(Class<? extends FlowDelegate> flowDelegateCls, FlowDelegate flowDelegate, String handlingMethod) throws Exception {
+    private void invokeMethod(FlowDelegate flowDelegate, String handlingMethod) throws Exception {
+        Class<? extends FlowDelegate> flowDelegateCls = flowDelegate.getClass();
         Method method = flowDelegateCls.getMethod(handlingMethod.trim());
         method.invoke(flowDelegate);
     }
@@ -377,29 +371,45 @@ public class FlowProcessServiceImpl implements FlowProcessService {
     /**
      * 执行结束节点方法
      *
-     * @param endNode         结束节点
-     * @param flowDelegateCls java委托对象class
-     * @param flowDelegate    java委托对象
+     * @param endNode      结束节点
+     * @param flowDelegate java委托对象
      */
-    private void invokeEndMethod(FlowDefinitionNode endNode, Class<? extends FlowDelegate> flowDelegateCls, FlowDelegate flowDelegate) {
+    private void invokeEndMethod(FlowDefinitionNode endNode, FlowDelegate flowDelegate) {
         try {
             // 执行结束方法
             String handlingMethod = endNode.getHandlingMethod();
             if (StrUtil.isNotBlank(handlingMethod)) {
                 // 执行指定方法
-                invokeMethod(flowDelegateCls, flowDelegate, handlingMethod);
+                invokeMethod(flowDelegate, handlingMethod);
             } else {
                 // 执行默认方法
                 flowDelegate.end();
             }
         } catch (Exception e) {
             log.error("结束节点方法异常", e);
-            throw new ServiceException("结束节点方法异常");
+            throw new ServiceException("结束节点方法异常:" + e.getMessage());
+        }
+    }
+
+    /**
+     * 查找上一个节点
+     *
+     * @param flowExampleId 流程实例id
+     * @return 用户节点
+     */
+    private FlowExampleDetail getLastOneUserNode(Long flowExampleId) {
+        FlowExampleDetail flowExampleDetail = flowExampleDetailService.getOne(q -> q
+                .eq(FlowExampleDetail::getFlowExampleId, flowExampleId)
+                .orderByDesc(BaseIdPo::getId)
+                .last("limit 1,1"));
+        if (flowExampleDetail == null) {
+            throw new ServiceException("没有找到回退节点");
         }
+        return flowExampleDetail;
     }
 
     /**
-     * 查找下一个用户执行节点
+     * 查找下一个节点
      *
      * @param currentNode            当前节点
      * @param flowDefinitionNodeList 流程节点列表
@@ -448,31 +458,19 @@ public class FlowProcessServiceImpl implements FlowProcessService {
     }
 
     /**
-     * 查找上一个节点
-     *
-     * @param flowExampleId 流程实例id
-     * @return 用户节点
-     */
-    private FlowExampleDetail getLastOneUserNode(Long flowExampleId) {
-        FlowExampleDetail flowExampleDetail = flowExampleDetailService.getOne(q -> q
-                .eq(FlowExampleDetail::getFlowExampleId, flowExampleId)
-                .orderByDesc(BaseIdPo::getId)
-                .last("limit 1,1"));
-
-        if (flowExampleDetail == null) {
-            throw new ServiceException("没有找到回退节点");
-        }
-
-        return flowExampleDetail;
-    }
-
-    /**
      * 校验el表达式
      */
     public boolean expressionResult(Map<String, Object> map, String expression) {
-        Expression exp = AviatorEvaluator.compile(expression);
-        Object execute = exp.execute(map);
-        return Boolean.parseBoolean(String.valueOf(execute));
+        boolean result;
+        try {
+            Expression exp = AviatorEvaluator.compile(expression);
+            Object execute = exp.execute(map);
+            result = Boolean.parseBoolean(String.valueOf(execute));
+        } catch (Exception e) {
+            log.error("el表达式校验错误", e);
+            throw new ServiceException("el表达式校验错误:" + e.getMessage());
+        }
+        return result;
     }
 
     /**