Integrating LiteFlow with Spring Boot for Decoupled, Dynamic, Scalable Business Logic
This guide shows how to integrate the lightweight LiteFlow rule engine into a Spring Boot application, split complex business logic into reusable components, configure rule files, and use various component types—including switch, condition, and parameterized components—to achieve dynamic orchestration, hot‑reloading, and high scalability.
Integration of LiteFlow into a Spring Boot project
LiteFlow requires Spring Boot 2.0 or higher (supported range 2.x – 3.x). Add the starter dependency:
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-spring-boot-starter</artifactId>
<version>2.11.4.2</version>
</dependency>Configure the rule file location in application.yml or application.properties:
liteflow:
rule-source: config/flow.el.xmlOptional properties (e.g., enable, print-banner, thread‑pool sizes) can be added under the liteflow prefix as needed.
Defining components
Each business step is a component that extends NodeComponent and is annotated with @Component("componentId"):
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("acmp")
public class ACmp extends NodeComponent {
@Override
public void process() {
System.out.println("acmp executed");
}
}
@Component("bcmp")
public class BCmp extends NodeComponent {
@Override
public void process() {
System.out.println("bcmp executed");
}
}
@Component("ccmp")
public class CCmp extends NodeComponent {
@Override
public void process() {
System.out.println("ccmp executed");
}
}Rule file config/flow.el.xml wires the components:
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<chain name="chain1">
THEN(acmp, bcmp, ccmp);
</chain>
</flow>Executing a flow
Ensure component packages are scanned:
@SpringBootApplication
@ComponentScan({"com.xxx.cmp"})
public class LiteflowExampleApplication {
public static void main(String[] args) {
SpringApplication.run(LiteflowExampleApplication.class, args);
}
}Inject FlowExecutor in any Spring‑managed bean and run the chain:
@Resource
private FlowExecutor flowExecutor;
LiteflowResponse response = flowExecutor.execute2Resp("chain1");
System.out.println(response);The test output shows the three components executing in order, confirming the rule configuration.
Advanced component types
Switch component
Implement NodeSwitchComponent and override processSwitch to decide the next node:
import com.yomahub.liteflow.core.NodeSwitchComponent;
import org.springframework.stereotype.Component;
@Component("switchCmp")
public class SwitchCmp extends NodeSwitchComponent {
@Override
public String processSwitch() throws Exception {
System.out.println("switchCmp executed!");
return "switchCmpA"; // choose next component
}
}Rule file example:
<chain name="switch_chain">
SWITCH(switchCmp).to(switchCmpA, switchCmpB);
</chain>Condition (IF) component
Available from version 2.8.5. Extend NodeIfComponent and return a boolean:
import com.yomahub.liteflow.core.NodeIfComponent;
import org.springframework.stereotype.Component;
@Component("xcmp")
public class XCmp extends NodeIfComponent {
@Override
public boolean processIf() throws Exception {
// business condition
return false;
}
}Rule usage:
<chain name="if_chain">
IF(xcmp, acmp, bcmp);
</chain>Parameter passing
Flows can receive arbitrary objects via the overloaded execute2Resp method:
public LiteflowResponse execute2Resp(String chainId, Object param, Class<?>... contextBeanClazzArray)Example passing a custom bean:
flowExecutor.execute2Resp(
"if_param_chain",
BusStudent.builder().name("公众号:霸道的程序猿").build()
);Declarative components
Components can be defined without extending framework classes by using annotations. Example:
import com.yomahub.liteflow.annotation.*;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.enums.LiteFlowMethodEnum;
@LiteflowComponent("defineCmp")
@LiteflowCmpDefine
public class DefineCmp {
@LiteflowMethod(LiteFlowMethodEnum.PROCESS)
public void processAcmp(NodeComponent bindCmp) {
System.out.println("ACmp executed!");
}
@LiteflowMethod(LiteFlowMethodEnum.IS_ACCESS)
public boolean isAcmpAccess(NodeComponent bindCmp) { return true; }
@LiteflowMethod(LiteFlowMethodEnum.BEFORE_PROCESS)
public void beforeAcmp(NodeComponent bindCmp) { System.out.println("before A"); }
@LiteflowMethod(LiteFlowMethodEnum.AFTER_PROCESS)
public void afterAcmp(NodeComponent bindCmp) { System.out.println("after A"); }
@LiteflowMethod(LiteFlowMethodEnum.ON_SUCCESS)
public void onAcmpSuccess(NodeComponent bindCmp) { System.out.println("Acmp success"); }
@LiteflowMethod(LiteFlowMethodEnum.ON_ERROR)
public void onAcmpError(NodeComponent bindCmp, Exception e) { System.out.println("Acmp error"); }
@LiteflowMethod(LiteFlowMethodEnum.IS_END)
public boolean isAcmpEnd(NodeComponent bindCmp) { return false; }
@LiteflowMethod(LiteFlowMethodEnum.ROLLBACK)
public void rollbackA(NodeComponent bindCmp) throws Exception { System.out.println("ACmp rollback!"); }
}Additional features
LiteFlow supports EL‑based rule composition, dynamic rule construction via code, smooth hot‑updates, and a rich set of configuration options (e.g., thread‑pool sizes, retry counts, monitoring settings). Detailed documentation and demo projects are available at the official site (https://liteflow.cc) and the Gitee repository (https://gitee.com/dromara/liteFlow).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
The Dominant Programmer
Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
