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>
    
    <build>
        <testResources>
            <!--单元测试时引用src/main/resources下的资源文件,
             把src/main/resources的资源文件拷贝到src/test/resources
             ,否则单元测试找不到规则文件
            -->
            <testResource>
                <directory>src/main/resources</directory>
            </testResource>
        </testResources>
    </build>    

代码

在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" >

    <kie:kmodule id="kmodule">
        <kie:kbase name="kbase" packages="rules">
            <kie:ksession name="ksession-rules" />
        </kie:kbase>
    </kie:kmodule>

    <bean id="kiePostProcessor" class="org.kie.spring.KModuleBeanFactoryPostProcessor" />
    <!--<bean id="kiePostProcessor" class="org.kie.spring.annotations.KModuleAnnotationPostProcessor " />-->
</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目录下

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值