Backend Development 15 min read

Using URule Rule Engine with Spring Boot: Installation, Configuration, and Practical Examples

This article introduces the URule rule engine, explains its background, installation steps, core concepts such as variable, constant, parameter, and action libraries, demonstrates both wizard‑style and script‑style rule sets, and provides complete Spring Boot integration code with real‑world usage scenarios.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Using URule Rule Engine with Spring Boot: Installation, Configuration, and Practical Examples

Background: The author needed a flexible way to handle numerous conditional checks during a project refactor and explored various rule engines, ultimately choosing URule for its browser‑based visual configuration and cross‑platform support.

Introduction: URule is a component that separates complex business logic from code, allowing rules to be defined via a visual editor in the browser without additional installations. It offers both open‑source and Pro versions.

Installation and Usage: Four deployment modes exist (embedded, local, distributed, standalone), but the article focuses on the open‑source version integrated with Spring Boot. After creating an empty database and adjusting spring.datasource properties, the service can be started and accessed 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=mysql

Core Concepts: URule consists of a designer (library files and rule files) and an execution engine. Library files include variable, constant, parameter, and action libraries, each mapping to Java constructs such as POJOs, enums, Maps, and Spring beans.

Variable Library Example:

package com.cicada;

import com.bstek.urule.model.Label;
import lombok.Data;

/**
 * @author 往事如风
 */
@Data
public class Stu {
    @Label("姓名")
    private String name;

    @Label("年龄")
    private int age;

    @Label("班级")
    private String classes;
}

Action Library Example (exposes Spring beans to rules):

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 Set Creation: URule supports wizard‑style (visual) and script‑style rule sets. A wizard‑style rule set consists of three parts—condition (if), action (then), and else—configured through the UI. The article shows a simple example with these three sections.

Executing Rules from 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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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, displayed as a table with columns for conditions and a column for the resulting action. The article includes a screenshot of a decision table used for user promotion logic.

Use Cases: The author describes a real‑world scenario where users are promoted from regular to member to elite member based on registration counts, order amounts, and continuity rates, illustrating how URule can encapsulate such business policies.

Conclusion: While URule is optional, it helps separate complex decision logic from business code, making maintenance easier and providing a visual way to manage rules, which is an essential step in robust software architecture.

Javarule engineBackend DevelopmentSpring BootURuleDecision Table
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

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.