🌱Spring场景安装运行
# 依赖
针对于使用了Spring但没有使用SpringBoot的项目
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-spring</artifactId>
<version>2.15.0</version>
</dependency>
为稳定版本,目前jar包已上传中央仓库,可以直接依赖到
# 定义你的组件
你需要定义并实现一些组件,确保Spring会扫描到这些组件并注册进上下文
@LiteflowComponent("a")
public class ACmp extends NodeComponent {
@Override
public void process() {
//do your business
}
}
以此类推再分别定义b,c组件:
@LiteflowComponent("b")
public class BCmp extends NodeComponent {
@Override
public void process() {
//do your business
}
}
@LiteflowComponent("c")
public class CCmp extends NodeComponent {
@Override
public void process() {
//do your business
}
}
# Spring xml中的配置
<context:component-scan base-package="com.yomahub.flowtest.components" />
<bean id="springAware" class="com.yomahub.liteflow.spi.spring.SpringAware"/>
<bean class="com.yomahub.liteflow.spring.ComponentScanner"/>
<bean id="liteflowConfig" class="com.yomahub.liteflow.property.LiteflowConfig">
<property name="ruleSource" value="config/flow.xml"/>
</bean>
<bean id="flowExecutor" class="com.yomahub.liteflow.core.FlowExecutor" depends-on="springAware">
<property name="liteflowConfig" ref="liteflowConfig"/>
</bean>
<!-- 如果上述enableLog为false,下面这段也可以省略 -->
<bean class="com.yomahub.liteflow.monitor.MonitorBus">
<constructor-arg ref="liteflowConfig" />
</bean>
更多配置项请参考配置项章节。
# 规则文件的定义
同时,你得在resources的config/flow.xml
中如下配置:
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<chain name="chain1">
THEN(a, b, c)
</chain>
</flow>
Spring在启动时会自动装载规则文件。
# 执行
和SpringBoot的执行方式一样,没有任何区别,你可以在你的任何受Spring托管的类里注入FlowExecutor
进行执行:
@Component
public class YourClass{
@Resource
private FlowExecutor flowExecutor;
@Test
public void testConfig(){
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg", DefaultContext.class);
}
}
提示
这个DefaultContext
是默认的上下文,用户可以用最自己的任意Bean当做上下文传入,如果需要传入自己的上下文,则需要传用户Bean的Class属性,具体请看数据上下文这一章节。
帮助我们改善此文档 (opens new window)
上次更新: 2025/08/28, 22:41:24