How to Integrate URule Rule Engine into Spring Boot: A Hands‑On Guide
This article walks through the background, core concepts, installation steps, library definitions, rule set creation, decision table usage, and a real‑world promotion scenario for integrating the URule rule engine with a Spring Boot backend, providing code snippets and visual configuration tips.
Background
During a project refactor, many conditional checks were needed. Instead of writing numerous if‑else statements, the author explored rule engines and chose URule because it can be configured directly in the browser without heavy installation.
Introduction
A rule engine is a component that separates complex business logic from code. URule runs on various operating systems and offers a pure‑browser editing mode. It has open‑source and Pro versions; the feature comparison table is omitted here.
Installation and Usage
URule Pro provides four usage modes, but the author uses the open‑source version integrated with Spring Boot. Create an empty database, configure the datasource in the edas‑rule‑server service, and start the service.
<code>spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/urule-data?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=mysql</code>Open the browser at http://localhost:8090/urule/frame to verify the UI.
Basic Concepts
Overall Architecture
URule consists of a designer (library files and rule files) and a rule execution engine.
Library Files
Four types: variable library, constant library, parameter library, and action library.
Variable Library
Maps Java POJOs to rule variables using the @Label annotation.
<code>package com.cicada;
import com.bstek.urule.model.Label;
import lombok.Data;
@Data
public class Stu {
@Label("姓名")
private String name;
@Label("年龄")
private int age;
@Label("班级")
private String classes;
}</code>The @Label annotation links POJO fields to rule variable titles.
Constant Library
Defines enums or constants such as gender.
Parameter Library
Temporary variables stored as a Map.
Names must be unique because they become Map keys.
Action Library
Maps Spring bean methods to actions using @Component and @ExposeAction.
<code>package com.bstek.urule.cicada;
import com.bstek.urule.action.ActionId;
import com.bstek.urule.model.ExposeAction;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component("action")
public class Action {
@ActionId("Hello")
public String hello() {
return "hello";
}
@ExposeAction(value="方法1")
public boolean evalTest(String username) {
if (username == null) {
return false;
} else if (username.equals("张三")) {
return true;
}
return false;
}
@ExposeAction(value="测试Int")
public int testInt(int a, int b) {
return a + b;
}
@ExposeAction(value="打印内容")
public void printContent(String username, Date birthday) {
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (birthday != null) {
System.out.println(username + "今年已经" + sd.format(birthday) + "岁了!");
} else {
System.out.println("Hello " + username);
}
}
@ExposeAction(value="打印Stu")
public void printUser(Stu m) {
System.out.println("Hello " + m.getName() + ", is age:" + m.getAge());
}
}
</code>Rule Sets
Rule sets combine conditions (if), actions (then), and else branches. URule supports wizard‑style (visual) and script‑style rule sets.
Wizard‑style Rule Set
Create via “Decision Set → Add Wizard‑style Decision Set”, import libraries, and configure rules visually.
If: condition
Then: actions such as variable assignments
Else: alternative actions
Script‑style Rule Set
Write rules in script form; functionality is the same as wizard style.
Decision Table
An alternative, tabular representation of rule sets that is easier to read.
Use Cases
Example scenario: user promotion based on registration count, order amount, and order continuation rate. Different rules apply for normal → member and member → elite member promotions.
The configuration uses variable and constant libraries, then a decision table to define the promotion logic.
Conclusion
Rule engines can decouple complex business logic from code, making maintenance easier, but they require a solid understanding of requirements to abstract them properly.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.