zoukankan      html  css  js  c++  java
  • springboot集成drools的方式一

    springboot集成drools的方式一(spring-drools.xml)

    本文springboot采用1.5.1.RELEASE版本,drools采用的6.5.0.Final,一共会讲三种方式,方式一因为资源文件总找不到,困扰了我许久,所以在这里想记下来;
    方式二网上博客比较多,不过不实用;方式三采用@Configuration自动配置,是springboot项目最常用的的做法,所以一般选用方式三。这里先讲方式一。

    maven配置

    详细的配置见码云上的代码工程:springbootDroolsMvn

        <dependencies>
            <dependency>
                <groupId>org.kie</groupId>
                <artifactId>kie-api</artifactId>
                <version>6.5.0.Final</version>
            </dependency>
            <dependency>
                <groupId>org.drools</groupId>
                <artifactId>drools-compiler</artifactId>
                <version>6.5.0.Final</version>
            </dependency>
            <dependency>
                <groupId>org.kie</groupId>
                <artifactId>kie-spring</artifactId>
                <version>6.5.0.Final</version>
            </dependency>
        </dependencies>
    
    &lt;build&gt;
        &lt;testResources&gt;
            &lt;!--单元测试时引用src/main/resources下的资源文件,
             把src/main/resources的资源文件拷贝到src/test/resources
             ,否则单元测试找不到规则文件
            --&gt;
            &lt;testResource&gt;
                &lt;directory&gt;src/main/resources&lt;/directory&gt;
            &lt;/testResource&gt;
        &lt;/testResources&gt;
    &lt;/build&gt;    
    

    代码

    在springboot启动类配置,使用@ImportResourcel配置spring-drools.xml

    package com.pingan.core;
    

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ImportResource;

    @SpringBootApplication
    @ImportResource("classpath:/spring-drools.xml")
    public class App {

    public static void main(String[] args) {
    
        SpringApplication.run(App.class, args);
    }
    

    }

    在src/main/resource目录下新建,spring-drools.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:kie="http://drools.org/schema/kie-spring"
           xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
    	http://drools.org/schema/kie-spring http://drools.org/schema/kie-spring.xsd" >
    
    &lt;kie:kmodule id="kmodule"&gt;
        &lt;kie:kbase name="kbase" packages="rules"&gt;
            &lt;kie:ksession name="ksession-rules" /&gt;
        &lt;/kie:kbase&gt;
    &lt;/kie:kmodule&gt;
    
    &lt;bean id="kiePostProcessor" class="org.kie.spring.KModuleBeanFactoryPostProcessor" /&gt;
    &lt;!--&lt;bean id="kiePostProcessor" class="org.kie.spring.annotations.KModuleAnnotationPostProcessor " /&gt;--&gt;
    

    </beans>

    编写DroolRuleController类,用于测试

    package com.pingan.core.controller;
    

    import com.pingan.core.entity.UserInfo;
    import lombok.extern.slf4j.Slf4j;
    import org.kie.api.KieBase;
    import org.kie.api.KieServices;
    import org.kie.api.runtime.KieContainer;
    import org.kie.api.runtime.KieSession;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;

    @Slf4j
    @Controller
    public class DroolRuleController {

    @Autowired
    //@KBase("kbase")
    //@KReleaseId(groupId = "com.pingan.core",artifactId = "drools",version = "LATEST")
    private KieBase kieBase;
    
    //http://localhost:8080/rule1
    @RequestMapping("/rule1")
    @ResponseBody
    public String rule1(){
        //StatefulKnowledgeSession
        KieSession kieSession = kieBase.newKieSession();
    
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("superbing");
        userInfo.setTelephone("13618607409");
    
        kieSession.insert(userInfo);
        //kieSession.setGlobal("log",log);
        int firedCount = kieSession.fireAllRules();
        //kieSession.dispose();
        System.out.println("触发了" + firedCount + "条规则");
        return "触发了" + firedCount + "条规则";
    }
    

    }

    编写个单元测试类UserDroolTest

    package com.pingan.core;
    

    import com.pingan.core.entity.UserInfo;
    import lombok.extern.slf4j.Slf4j;

    import java.io.File;
    import java.io.IOException;
    import java.net.URL;

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.kie.api.KieBase;
    import org.kie.api.KieServices;
    import org.kie.api.runtime.KieContainer;
    import org.kie.api.runtime.KieSession;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = App.class)
    @Slf4j
    public class UserDroolTest {

    @Autowired
    //@KBase("kbase")
    //@KReleaseId(groupId = "com.pingan.core",artifactId = "drools",version = "LATEST")
    private KieBase kieBase;
    
    @Test
    public void rule1(){
        //StatefulKnowledgeSession
        KieSession kieSession = kieBase.newKieSession();
    
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("superbing");
        userInfo.setTelephone("13618607409");
    
        kieSession.insert(userInfo);
        //kieSession.setGlobal("log",log);
        int firedCount = kieSession.fireAllRules();
        //kieSession.dispose();
        System.out.println("触发了" + firedCount + "条规则");
    }
    

    }

    总结

    1、 此方式需要在springboot启动类App里通过
    @ImportResource配置spring-drools.xml并配置

    <bean id="kiePostProcessor" class="org.kie.spring.KModuleBeanFactoryPostProcessor" />
    或者
    <bean id="kiePostProcessor"class="org.kie.spring.annotations.KModuleAnnotationPostProcessor" />
    使用KModuleBeanFactoryPostProcessor只能使用@Autowired方式,
    使用KModuleAnnotationPostProcessor可以使用@Autowired和@KBase("kbase")两种方式
    

    2、 另外使用@KReleaseId(groupId = “com.pingan.core”,artifactId = “drools”,version = “LATEST”)
    需要在resources目录下新建META-INF/maven/pom.properties
    并在pom.properties里配置:

    groupId = com.pingan.core
    artifactId = drools
    version = 1
    

    不过谁这么蛋疼会使用这种方式呢,肯定还是使用@Autowired方式简单,如下:

    @Autowired
    private KieBase kieBase;
    

    3、另外由于KModuleBeanFactoryPostProcessor找drools文件时,这里有个大前提是用maven构建。

    非测试环境下,会在项目目录target/classes目录下找,而maven构建时,刚好resoucce下的资源文件是编译在classes
    目录下的,所以能找到;
    但是测试环境下,会在项目目录target/test-classe目录找,所以在pom.xml需要额外进行如下设置:
    <testResource>
        <directory>src/main/resources</directory>
    </testResource>
    

    这样maven构建时就会把src/main/resources目录下的资源文件也编译一份到target/test-classes目录下

    原文地址:https://blog.csdn.net/bladeandmaster88/article/details/89648353
  • 相关阅读:
    drag element
    时间会改变一切
    Windows下MySQL的my.ini文件字符集测试(二)
    Windows下MySQL的my.ini文件字符集测试
    SQL Server和MySQL主外键写法对比
    SQL Server外键约束
    [SQL Server]重命名数据库【转】
    SQL Server 2008 R2链接MySQL 5.6
    空值的日期类型和update 中的null
    删除数据库中所有表
  • 原文地址:https://www.cnblogs.com/jpfss/p/12014906.html
Copyright © 2011-2022 走看看