Using URule Rule Engine in Spring Boot: Installation, Configuration, and Sample Code
This article introduces the URule rule engine, explains its background and features, walks through installation and usage in a Spring Boot project, details core concepts such as variable, constant, parameter, and action libraries, and provides complete code examples for rule sets and decision tables.
The author encountered many conditional checks during a project refactor and decided to explore rule engines, ultimately selecting URule for its browser‑based visual configuration and ease of integration.
URule is a component that separates complex business logic from code, accepting input data, applying predefined rules, and producing results. It runs on various operating systems and offers both open‑source and Pro versions.
Installation & Usage : After adding URule to the project, configure the datasource in 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 and start the service. Access the UI at http://localhost:8090/urule/frame to verify the startup.
Basic Concepts :
Library files: variable, constant, parameter, and action libraries.
Variable library maps Java POJOs (e.g., package com.cicada; import com.bstek.urule.model.Label; import lombok.Data; /** * @author 往事如风 * @version 1.0 * @date 2023/3/3 15:38 */ @Data public class Stu { @Label("姓名") private String name; @Label("年龄") private int age; @Label("班级") private String classes; } ).
Constant library stores enums or fixed values.
Parameter library works like a Map for temporary variables.
Action library links Spring beans (e.g., 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; /** * @author 往事如风 * @version 1.0 * @date 2023/3/10 13:59 */ @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()); } } ).
Rule Sets : URule supports wizard‑style (visual) and script‑style rule sets. A wizard‑style set consists of three parts—condition (if), action (then), and else—configured through the UI. Example code to execute a rule set via Spring Boot:
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; /** * @author 往事如风 * @version 1.0 * @date 2023/3/10 16:47 */ @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, offering a clear tabular view of conditions and outcomes. The author demonstrates a decision table for user promotion scenarios (ordinary → member → elite member) based on registration counts, order amounts, and continuity rates.
Use Cases : The article outlines a real‑world scenario where user roles are upgraded monthly according to specific business rules, showing how URule can encapsulate such logic.
Conclusion : While not mandatory, a rule engine like URule can decouple complex decision logic from business code, making maintenance easier and allowing non‑developers to understand rule configurations.
Reference Sources : Links to the author's GitHub repositories and additional reading material are provided.
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
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.