Backend Development 16 min read

Integrating URule Rule Engine into Java Spring Boot Projects

The article explains how to embed the open‑source URule rule engine into a Spring Boot application, covering installation, datasource configuration, library definitions for variables, constants, parameters and actions, creating visual or script rule sets and decision tables, and demonstrates a membership‑upgrade use case.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
Integrating URule Rule Engine into Java Spring Boot Projects

This article introduces the URule rule engine, a visual, browser‑based component that can be embedded in Java applications to separate complex business logic from code.

Background : During a project refactor the author explored rule engines and chose URule for its zero‑installation, browser‑editable UI and cross‑platform support.

Introduction : URule provides a designer and an execution engine. It supports both open‑source and Pro versions; the open‑source edition is sufficient for most use cases.

Installation & Usage : Four deployment modes exist (embedded, local, distributed, standalone). The author uses the open‑source version integrated with Spring Boot. After creating an empty database and configuring the datasource, the service starts and the UI is reachable at http://localhost:8090/urule/frame .

Datasource configuration example:

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

Basic Concepts

The engine uses four types of library files:

Variable library – maps Java POJOs (e.g., Stu ) to rule variables.

Constant library – holds enums or constant values.

Parameter library – a dynamic map of temporary variables.

Action library – exposes Spring beans/methods to rules.

Variable library example (Java class with @Label annotations):

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;
}

Action library example (exposes methods via @ExposeAction):

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()); }
}

Rule Sets : URule supports wizard‑style (visual) and script‑style rule sets. A wizard rule set consists of if , then , and else parts, configured through the UI.

Example of invoking a rule set from code:

package com.cicada;
import cn.hutool.core.bean.BeanUtil;
import com.Result;
import com.bstek.urule.Utils;
import com.bstek.urule.runtime.*;
import com.bstek.urule.runtime.service.KnowledgeService;
import com.cicada.req.StuReq;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/rule")
public class RuleDataController {
@PostMapping("/stu")
    public Result rule(@RequestBody StuReq stuReq) throws IOException {
KnowledgeService ks = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
        KnowledgePackage kp = ks.getKnowledge("xxx/xxx");
        KnowledgeSession session = KnowledgeSessionFactory.newKnowledgeSession(kp);
        Stu stu = BeanUtil.copyProperties(stuReq, Stu.class);
        session.insert(stu);
        session.fireRules();
        return Result.success(stu.getTeacher());
    }
}

Decision Tables : An alternative representation of rule sets; the author prefers them for clarity. A sample decision table with columns for conditions and an output column is shown.

Use Cases : The author describes a membership‑upgrade scenario where different rules determine promotion from ordinary user to member and from member to elite member based on registration counts, order amounts, and order continuation rates.

Conclusion : Rule engines like URule can help decouple complex decision logic from business code, making maintenance easier. However, developers must understand the domain to model rules effectively.

Javarule engineBackend DevelopmentSpring BootURule
Java Tech Enthusiast
Written by

Java Tech Enthusiast

Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.