欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot集成easy-rules規(guī)則引擎流程詳解

 更新時間:2023年03月28日 09:58:42   作者:QiHY  
這篇文章主要介紹了SpringBoot集成easy-rules規(guī)則引擎流程,合理的使用規(guī)則引擎可以極大的減少代碼復(fù)雜度,提升代碼可維護性。業(yè)界知名的開源規(guī)則引擎有Drools,功能豐富,但也比較龐大

合理的使用規(guī)則引擎可以極大的減少代碼復(fù)雜度,提升代碼可維護性。業(yè)界知名的開源規(guī)則引擎有Drools,功能豐富,但也比較龐大。在一些簡單的場景中,我們只需要簡易的規(guī)則引擎就能滿足要求。

本文介紹一個小巧的規(guī)則引擎 easy-rules,作為一個lib庫提供,支持spring的SPEL表達(dá)式,可以很好的集成在spring項目中。

具體的代碼參照 示例項目 https://github.com/qihaiyan/springcamp/tree/master/spring-easy-rule

一、概述

通過將業(yè)務(wù)規(guī)則配置的配置文件中,可以精簡代碼,同時已于維護,當(dāng)規(guī)則修改時,只需要修改配置文件即可。easy-rules是一個小巧的規(guī)則引擎,支持spring的SPEL表達(dá)式,同時還支持 Apache JEXL 表達(dá)式和 MVL 表達(dá)式。

二、項目中加入依賴

在項目的gradle中增加依賴關(guān)系。

build.gradle:

plugins {
    id 'org.springframework.boot' version '3.0.5'
    id 'io.spring.dependency-management' version '1.1.0'
    id 'java'
}

group = 'cn.springcamp'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
    testCompileOnly {
        extendsFrom testAnnotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation "org.springframework.boot:spring-boot-starter-json"
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.jeasy:easy-rules-core:4.1.0'
    implementation 'org.jeasy:easy-rules-spel:4.1.0'
    implementation 'org.jeasy:easy-rules-support:4.1.0'
    annotationProcessor 'org.projectlombok:lombok'
    testAnnotationProcessor 'org.projectlombok:lombok'
    testImplementation "org.springframework.boot:spring-boot-starter-test"
    testImplementation 'org.junit.vintage:junit-vintage-engine'
    testImplementation 'org.junit.vintage:junit-vintage-engine'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:2022.0.1"
    }
}

test {
    useJUnitPlatform()
}

三、配置文件

示例程序?qū)I(yè)務(wù)規(guī)則放到配置文件中,業(yè)務(wù)規(guī)則配置文件(demo-rule.yml)代碼:

name: "age rule"
description: ""
priority: 1
condition: "#person.getAdult() == false"
actions:
  - "T(java.lang.System).out.println(\"Shop: Sorry, you are not allowed to buy alcohol\")"
  - "#person.setAdult(true)"
  - "#person.setAge(18)"
---
name: "alcohol rule"
description: ""
priority: 1
condition: "#person.getAdult() == true"
actions:
  - "T(java.lang.System).out.println(\"Shop: you are now allowed to buy alcohol\")"

配置文件中的規(guī)則通過 condition 進(jìn)行配置,當(dāng)滿足規(guī)則時,會調(diào)用 actions 中配置的動作。

示例項目使用了spring的SPEL表達(dá)式進(jìn)行規(guī)則配置,配置文件中配置了2個規(guī)則,第一個規(guī)則通過 person這個spring bean中的getAdult()判斷是否滿足規(guī)則,滿足規(guī)則時調(diào)用三個方法。

在spring-boot本身的配置文件中 application.yml 配置規(guī)則文件:

rule:
  skip-on-first-failed-rule: true
  skip-on-first-applied-rule: false
  skip-on-first-non-triggered-rule: true
  rules:
    - rule-id: "demo"
      rule-file-location: "classpath:demo-rule.yml"

四、代碼中對規(guī)則引擎進(jìn)行配置

通過 RuleEngineConfig這個spring的配置類對規(guī)則引擎進(jìn)行配置:

@Slf4j
@EnableConfigurationProperties(RuleEngineConfigProperties.class)
@Configuration
public class RuleEngineConfig implements BeanFactoryAware {
    @Autowired(required = false)
    private List<RuleListener> ruleListeners;
    @Autowired(required = false)
    private List<RulesEngineListener> rulesEngineListeners;
    private BeanFactory beanFactory;
    @Bean
    public RulesEngineParameters rulesEngineParameters(RuleEngineConfigProperties properties) {
        RulesEngineParameters parameters = new RulesEngineParameters();
        parameters.setSkipOnFirstAppliedRule(properties.isSkipOnFirstAppliedRule());
        parameters.setSkipOnFirstFailedRule(properties.isSkipOnFirstFailedRule());
        parameters.setSkipOnFirstNonTriggeredRule(properties.isSkipOnFirstNonTriggeredRule());
        return parameters;
    }
    @Bean
    public RulesEngine rulesEngine(RulesEngineParameters rulesEngineParameters) {
        DefaultRulesEngine rulesEngine = new DefaultRulesEngine(rulesEngineParameters);
        if (!CollectionUtils.isEmpty(ruleListeners)) {
            rulesEngine.registerRuleListeners(ruleListeners);
        }
        if (!CollectionUtils.isEmpty(rulesEngineListeners)) {
            rulesEngine.registerRulesEngineListeners(rulesEngineListeners);
        }
        return rulesEngine;
    }
    @Bean
    public BeanResolver beanResolver() {
        return new BeanFactoryResolver(beanFactory);
    }
    @Bean
    public RuleEngineTemplate ruleEngineTemplate(RuleEngineConfigProperties properties, RulesEngine rulesEngine) {
        RuleEngineTemplate ruleEngineTemplate = new RuleEngineTemplate();
        ruleEngineTemplate.setBeanResolver(beanResolver());
        ruleEngineTemplate.setProperties(properties);
        ruleEngineTemplate.setRulesEngine(rulesEngine);
        return ruleEngineTemplate;
    }
    @Bean
    public RuleListener defaultRuleListener() {
        return new RuleListener() {
            @Override
            public boolean beforeEvaluate(Rule rule, Facts facts) {
                return true;
            }
            @Override
            public void afterEvaluate(Rule rule, Facts facts, boolean b) {
                log.info("-----------------afterEvaluate-----------------");
                log.info(rule.getName() + rule.getDescription() + facts.toString());
            }
            @Override
            public void beforeExecute(Rule rule, Facts facts) {
                log.info("-----------------beforeExecute-----------------");
                log.info(rule.getName() + rule.getDescription() + facts.toString());
            }
            @Override
            public void onSuccess(Rule rule, Facts facts) {
                log.info("-----------------onSuccess-----------------");
                log.info(rule.getName() + rule.getDescription() + facts.toString());
            }
            @Override
            public void onFailure(Rule rule, Facts facts, Exception e) {
                log.info("-----------------onFailure-----------------");
                log.info(rule.getName() + "----------" + rule.getDescription() + facts.toString() + e.toString());
            }
        };
    }
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }
}

配置文件中配置了 ruleEngineTemplate這個spring bean,通過ruleEngineTemplate觸發(fā)規(guī)則引擎的執(zhí)行。

五、執(zhí)行規(guī)則引擎

ruleEngineTemplate配置好后,我們可以在業(yè)務(wù)代碼中執(zhí)行規(guī)則引擎,處理配置文件中配置的業(yè)務(wù)規(guī)則:

最為演示,我們將規(guī)則引擎的執(zhí)行代碼放到了 Application 的 run 方法中,程序啟動后立即執(zhí)行規(guī)則引擎:

@SpringBootApplication
public class Application implements CommandLineRunner {
    @Autowired
    RuleEngineTemplate ruleEngineTemplate;
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    @Override
    public void run(String... args) {
        Person person = new Person();
        Facts facts = new Facts();
        facts.put("person", person);
        ruleEngineTemplate.fire("demo", facts);
    }
}

程序執(zhí)行后可以看到控制臺里打印了 Shop: Sorry, you are not allowed to buy alcohol,這個內(nèi)容對應(yīng)的是我們在規(guī)則文件中的actions中配置的 "T(java.lang.System).out.println(\"Shop: Sorry, you are not allowed to buy alcohol\")",說明規(guī)則成功執(zhí)行了。

到此這篇關(guān)于SpringBoot集成easy-rules規(guī)則引擎流程詳解的文章就介紹到這了,更多相關(guān)SpringBoot集成easy-rules內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • idea中同一SpringBoot項目多端口啟動

    idea中同一SpringBoot項目多端口啟動

    本文主要介紹了idea中同一SpringBoot項目多端口啟動,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Java實現(xiàn)在Word指定位置插入分頁符

    Java實現(xiàn)在Word指定位置插入分頁符

    在Word插入分頁符可以在指定段落后插入,也可以在特定文本位置處插入。本文將以Java代碼來操作以上兩種文檔分頁需求,需要的可以參考一下
    2022-04-04
  • 利用Spring MVC+Mybatis實現(xiàn)Mysql分頁數(shù)據(jù)查詢的過程詳解

    利用Spring MVC+Mybatis實現(xiàn)Mysql分頁數(shù)據(jù)查詢的過程詳解

    這篇文章主要給大家介紹了關(guān)于利用Spring MVC+Mybatis實現(xiàn)Mysql分頁數(shù)據(jù)查詢的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • JDK安裝配置教程

    JDK安裝配置教程

    這篇文章主要為大家詳細(xì)介紹了JDK安裝配置教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • 一文帶你了解gson如何序列化子類

    一文帶你了解gson如何序列化子類

    這篇文章主要為大家詳細(xì)介紹了gson如何序列化子類的相關(guān)知識,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)和借鑒價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-10-10
  • IntelliJ IDEA 編輯器的全局搜索中使用正則表達(dá)式的操作方法

    IntelliJ IDEA 編輯器的全局搜索中使用正則表達(dá)式的操作方法

    這篇文章主要介紹了IntelliJ IDEA 編輯器的全局搜索中使用正則表達(dá)式的相關(guān)知識,補充介紹了IDEA查找和替換實用正則,感興趣的朋友跟隨小編一起看看吧
    2024-01-01
  • spring整合struts2過程詳解

    spring整合struts2過程詳解

    這篇文章主要介紹了spring整合struts2過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-01-01
  • mybatis中使用oracle關(guān)鍵字出錯的解決方法

    mybatis中使用oracle關(guān)鍵字出錯的解決方法

    這篇文章主要給大家介紹了關(guān)于mybatis中使用oracle關(guān)鍵字出錯的解決方法,文中通過示例代碼將解決的方法介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2017-08-08
  • SpringMVC中RequestMapping注解(作用、出現(xiàn)的位置、屬性)

    SpringMVC中RequestMapping注解(作用、出現(xiàn)的位置、屬性)

    這篇文章主要介紹了SpringMVC中RequestMapping注解(作用、出現(xiàn)的位置、屬性),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • 詳解springboot解決第三方依賴jar包的問題

    詳解springboot解決第三方依賴jar包的問題

    本篇文章主要介紹了詳解springboot解決第三方依賴jar包的問題,解決了第三方依賴jar包的問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09

最新評論