Integrating URule Rule Engine with Spring Boot: Installation, Configuration, and Practical Examples
This article introduces the URule rule engine, explains its core concepts, shows how to install and configure it in a Spring Boot project, demonstrates library definitions, rule set creation (wizard and script modes), decision table usage, and provides real‑world scenario code examples for backend developers.
Background – While refactoring a project the author needed a flexible way to handle many conditional checks and explored rule engines, ultimately choosing URule for its browser‑based visual configuration and cross‑platform support.
Introduction – A rule engine separates complex business logic from code, taking input data, applying predefined rules, and producing results. Popular engines include Drools, Aviator, EasyRules, but URule stands out with pure browser editing and no installation requirements.
Installation & Usage – URule can run on Windows, Linux, Unix. The open‑source version is integrated into a Spring Boot project by adding the edas-rule-server module, configuring the datasource in application.properties , and launching the service at http://localhost:8090/urule/frame .
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=mysqlBasic Concepts
URule consists of two parts: the Designer (library files and rule files) and the Rule Execution Engine.
Library Files
Variable Library – maps Java POJOs (e.g., Stu ) to rule variables.
Constant Library – stores enums or constant values.
Parameter Library – temporary variables stored as a Map.
Action Library – exposes Spring beans or methods to rules using @ExposeAction .
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;
} 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;
return "张三".equals(username);
}
@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());
}
}Rule Sets
Two creation modes are available:
Wizard‑style rule set – visual point‑and‑click configuration.
Script‑style rule set – written as scripts, requiring coding knowledge.
Example of a wizard rule set includes three parts: If (condition), Then (action), and Else (fallback).
package com.cicada;
import cn.hutool.core.bean.BeanUtil;
import com.Result;
import com.bstek.urule.Utils;
import com.bstek.urule.runtime.KnowledgePackage;
import com.bstek.urule.runtime.KnowledgeSession;
import com.bstek.urule.runtime.KnowledgeSessionFactory;
import com.bstek.urule.runtime.service.KnowledgeService;
import com.cicada.req.StuReq;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
@RestController
@RequestMapping("/rule")
public class RuleDataController {
@PostMapping("/stu")
public Result rule(@RequestBody StuReq stuReq) throws IOException {
KnowledgeService knowledgeService = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
KnowledgePackage knowledgePackage = knowledgeService.getKnowledge("xxx/xxx");
KnowledgeSession knowledgeSession = KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage);
Stu stu = BeanUtil.copyProperties(stuReq, Stu.class);
knowledgeSession.insert(stu);
knowledgeSession.fireRules();
return Result.success(stu.getTeacher());
}
}Decision Table – An alternative representation of rule sets that is more tabular and easier for non‑technical stakeholders to understand. The table contains condition columns and an output column.
Application Scenario – The author describes a promotion system where users advance from ordinary to member to elite member based on registration counts, order amounts, and order continuation rates, implemented with a decision table and supporting variable/constant libraries.
Conclusion – Rule engines like URule can decouple complex business decisions from code, making maintenance easier, but require solid understanding of the domain to model rules effectively.
References
Programming documentation: https://gitee.com/cicadasmile/butte-java-note
Application repository: https://gitee.com/cicadasmile/butte-flyer-parent
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.