本文springboot采用1.5.1.RELEASE版本,drools采用的6.5.0.Final,一共会讲三种方式,方式一因为资源文件总找不到,困扰了我许久,所以在这里想记下来;
方式二网上博客比较多,不过不实用;方式三采用@Configuration自动配置,是springboot项目最常用的的做法,所以一般选用方式三。这里先讲方式一。
详细的配置见码云上的代码工程:springbootDroolsMvn
org.kie
kie-api
6.5.0.Final
org.drools
drools-compiler
6.5.0.Final
org.kie
kie-spring
6.5.0.Final
src/main/resources
在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
编写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并配置
或者
使用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需要额外进行如下设置:
src/main/resources
这样maven构建时就会把src/main/resources目录下的资源文件也编译一份到target/test-classes目录下