最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

合理的使用規(guī)則引擎可以極大的減少代碼復(fù)雜度,提升代碼可維護(hù)性。業(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ī)則配置的配置文件中,可以精簡代碼,同時已于維護(hù),當(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)文章

  • 初次體驗MyBatis的注意事項

    初次體驗MyBatis的注意事項

    今天給大家?guī)淼氖顷P(guān)于MyBatis的相關(guān)知識,文章圍繞著MyBatis的用法展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • JDK動態(tài)代理過程原理及手寫實現(xiàn)詳解

    JDK動態(tài)代理過程原理及手寫實現(xiàn)詳解

    這篇文章主要為大家介紹了JDK動態(tài)代理過程原理及手寫實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • xxl-job的部署及springboot集成使用示例詳解

    xxl-job的部署及springboot集成使用示例詳解

    XXL-Job是一個分布式任務(wù)調(diào)度平臺,可進(jìn)行任務(wù)調(diào)度、管理和監(jiān)控,并提供任務(wù)分片、失敗重試、動態(tài)分配等功能,這篇文章主要介紹了xxl-job的部署及springboot集成使用,需要的朋友可以參考下
    2023-06-06
  • Spring很常用的@Conditional注解的使用場景和源碼解析

    Spring很常用的@Conditional注解的使用場景和源碼解析

    今天要分享的是Spring的注解@Conditional,@Conditional是一個條件注解,它的作用是判斷Bean是否滿足條件,本文詳細(xì)介紹了@Conditional注解的使用場景和源碼,需要的朋友可以參考一下
    2023-04-04
  • Java枚舉類實現(xiàn)Key-Value映射的多種實現(xiàn)方式

    Java枚舉類實現(xiàn)Key-Value映射的多種實現(xiàn)方式

    在 Java 開發(fā)中,枚舉(Enum)是一種特殊的類,本文將詳細(xì)介紹 Java 枚舉類實現(xiàn) key-value 映射的多種方式,有需要的小伙伴可以根據(jù)需要進(jìn)行選擇
    2025-04-04
  • btrace定位生產(chǎn)故障的方法示例

    btrace定位生產(chǎn)故障的方法示例

    這篇文章主要介紹了btrace定位生產(chǎn)故障的方法示例,文中通過示例代碼介紹的很詳細(xì),相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-02-02
  • spring聲明式事務(wù)@Transactional開發(fā)常犯的幾個錯誤及最新解決方案

    spring聲明式事務(wù)@Transactional開發(fā)常犯的幾個錯誤及最新解決方案

    使用聲明式事務(wù)@Transactional進(jìn)行事務(wù)一致性的管理,在開發(fā)過程中,發(fā)現(xiàn)很多開發(fā)同學(xué)都用錯了spring聲明式事務(wù)@Transactional或使用不規(guī)范,導(dǎo)致出現(xiàn)各種事務(wù)問題,這篇文章主要介紹了spring聲明式事務(wù)@Transactional開發(fā)常犯的幾個錯誤及解決辦法,需要的朋友可以參考下
    2024-02-02
  • 關(guān)于web項目讀取classpath下面文件的心得分享

    關(guān)于web項目讀取classpath下面文件的心得分享

    這篇文章主要介紹了關(guān)于web項目讀取classpath下面文件的心得,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Springboot消除switch-case過程解析

    Springboot消除switch-case過程解析

    這篇文章主要介紹了Springboot消除switch-case過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • java實現(xiàn)微信搶紅包算法

    java實現(xiàn)微信搶紅包算法

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)微信搶紅包算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-09-09

最新評論

日照市| 抚远县| 虞城县| 克什克腾旗| 新兴县| 抚顺市| 南通市| 莱阳市| 杂多县| 丹凤县| 阿图什市| 寿宁县| 顺昌县| 贞丰县| 琼海市| 信宜市| 祁阳县| 霍邱县| 扶余县| 方正县| 仙游县| 临邑县| 交口县| 隆德县| 宜良县| 竹溪县| 舒兰市| 延庆县| 蒲江县| 上杭县| 和田市| 胶南市| 将乐县| 昭苏县| 无为县| 定州市| 宜城市| 库尔勒市| 六盘水市| 淳安县| 雷山县|