24282 2 年之前
父节点
当前提交
37c6add890

+ 30 - 0
hx-flow/src/main/java/com/fjhx/flow/config/FlowInitializingBean.java

@@ -0,0 +1,30 @@
+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.context.annotation.Configuration;
+
+/**
+ * 流程初始化bean
+ */
+@Slf4j
+@Configuration
+public class FlowInitializingBean implements InitializingBean {
+
+    @Override
+    public void afterPropertiesSet() {
+
+        String[] beanNamesForType = SpringUtil.getBeanNamesForType(FlowDelegate.class);
+
+        for (String beanName : beanNamesForType) {
+            FlowDelegate flowDelegate = SpringUtil.getBean(beanName, FlowDelegate.class);
+            FlowBean.addBean(flowDelegate.getFlowKey(), beanName);
+            log.info("流程:{} 注册成功", flowDelegate.getFlowKey());
+        }
+
+    }
+
+}

+ 16 - 8
hx-flow/src/main/java/com/fjhx/flow/core/FlowBean.java

@@ -1,5 +1,6 @@
 package com.fjhx.flow.core;
 
+import cn.hutool.extra.spring.SpringUtil;
 import com.ruoyi.common.exception.ServiceException;
 
 import java.util.HashMap;
@@ -10,22 +11,29 @@ public class FlowBean {
     /**
      * 流程定义map
      */
-    protected static final Map<String, Class<? extends FlowDelegate>> map = new HashMap<>();
+    protected static final Map<String, String> map = new HashMap<>();
 
     /**
      * 添加流程bean
      *
-     * @param flowKey   流程标识
-     * @param beanClass 流程实现类
+     * @param flowKey  流程标识
+     * @param beanName springBean名称
      */
-    public static void addBean(String flowKey, Class<? extends FlowDelegate> beanClass) {
-        map.put(flowKey, beanClass);
+    public static void addBean(String flowKey, String beanName) {
+        map.put(flowKey, beanName);
     }
 
-    public static Class<? extends FlowDelegate> getBean(String flowKey) {
-        Class<? extends FlowDelegate> bean = map.get(flowKey);
+    /**
+     * 获取流程bean
+     *
+     * @param flowKey 流程bean
+     * @return springBean
+     */
+    public static FlowDelegate getBean(String flowKey) {
+        String beanBane = map.get(flowKey);
+        FlowDelegate bean = SpringUtil.getBean(beanBane);
         if (bean == null) {
-            throw new ServiceException("没有注册流程bean");
+            throw new ServiceException("没有把流程 " + flowKey + " 注册到spring中");
         }
         return bean;
     }

+ 2 - 0
hx-flow/src/main/java/com/fjhx/flow/core/FlowDelegate.java

@@ -23,6 +23,8 @@ public abstract class FlowDelegate {
         return map;
     }
 
+    public abstract String getFlowKey();
+
     /**
      * 开始流程实现类
      *

+ 8 - 23
hx-flow/src/main/java/com/fjhx/flow/service/flow/impl/FlowProcessServiceImpl.java

@@ -2,7 +2,6 @@ package com.fjhx.flow.service.flow.impl;
 
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
-import cn.hutool.extra.spring.SpringUtil;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 import com.fjhx.flow.core.FlowBean;
@@ -66,11 +65,11 @@ public class FlowProcessServiceImpl implements FlowProcessService {
     @Override
     public FlowResult initiate(InitiateDto dto) {
 
-        // 找到代码定义的流程bean
-        Class<? extends FlowDelegate> flowDelegateCls = FlowBean.getBean(dto.getFlowKey());
-
         // 获取流程委托对象
-        FlowDelegate flowDelegate = getFlowDelegate(flowDelegateCls);
+        FlowDelegate flowDelegate = FlowBean.getBean(dto.getFlowKey());
+
+        // 找到代码定义的流程bean
+        Class<? extends FlowDelegate> flowDelegateCls = flowDelegate.getClass();
 
         // 查找可用流程
         FlowDefinition flowDefinition = flowDefinitionService.getOne(q -> q
@@ -213,11 +212,11 @@ public class FlowProcessServiceImpl implements FlowProcessService {
             throw new ServiceException("流程已被处理");
         }
 
-        // 找到代码定义的流程bean
-        Class<? extends FlowDelegate> flowDelegateCls = FlowBean.getBean(flowExample.getFlowKey());
-
         // 获取流程委托对象
-        FlowDelegate flowDelegate = getFlowDelegate(flowDelegateCls);
+        FlowDelegate flowDelegate = FlowBean.getBean(flowExample.getFlowKey());
+
+        // 找到代码定义的流程bean
+        Class<? extends FlowDelegate> flowDelegateCls = flowDelegate.getClass();
 
         // 流程节点列表
         List<FlowDefinitionNode> flowDefinitionNodeList = flowDefinitionNodeService.list(q -> q
@@ -400,20 +399,6 @@ public class FlowProcessServiceImpl implements FlowProcessService {
     }
 
     /**
-     * 获取流程委托对象
-     *
-     * @param flowDelegateCls 流程代理class
-     * @return 流程委托对象
-     */
-    private FlowDelegate getFlowDelegate(Class<? extends FlowDelegate> flowDelegateCls) {
-        FlowDelegate flowDelegate = SpringUtil.getBean(flowDelegateCls);
-        if (flowDelegate == null) {
-            throw new ServiceException("没有把流程bean加入到spring中");
-        }
-        return flowDelegate;
-    }
-
-    /**
      * 查找下一个用户执行节点
      *
      * @param currentNode            当前节点

+ 1 - 9
ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java

@@ -1,12 +1,11 @@
 package com.ruoyi.web.controller.common;
 
 import com.baomidou.dynamic.datasource.annotation.DS;
-import com.fjhx.flow.core.FlowBean;
 import com.google.code.kaptcha.Producer;
 import com.ruoyi.common.config.RuoYiConfig;
+import com.ruoyi.common.constant.BaseSourceConstant;
 import com.ruoyi.common.constant.CacheConstants;
 import com.ruoyi.common.constant.Constants;
-import com.ruoyi.common.constant.BaseSourceConstant;
 import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.core.redis.RedisCache;
 import com.ruoyi.common.utils.sign.Base64;
@@ -18,7 +17,6 @@ import org.springframework.util.FastByteArrayOutputStream;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RestController;
 
-import javax.annotation.PostConstruct;
 import javax.annotation.Resource;
 import javax.imageio.ImageIO;
 import java.awt.image.BufferedImage;
@@ -46,12 +44,6 @@ public class CaptchaController {
     @Autowired
     private ISysConfigService configService;
 
-    @PostConstruct
-    public void initFlow() {
-        log.info("流程bean注册");
-        FlowBean.addBean("test_flow", TestFlow.class);
-    }
-
     /**
      * 生成验证码
      */

+ 5 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/TestFlow.java

@@ -16,6 +16,11 @@ public class TestFlow extends FlowDelegate {
     @Autowired
     private ISysConfigService sysConfigService;
 
+    @Override
+    public String getFlowKey() {
+        return "test_flow";
+    }
+
     @DS("iot")
     @Override
     public Long start(Long flowId, JSONObject data) {