使用Spring管理整合drools,首先需要引入依赖,然后配置spring文件。
引入依赖
<properties>
<drools.version>7.10.0.Final</drools.version>
<spring.version>5.0.5.RELEASE</spring.version>
</properties>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>${drools.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-spring</artifactId>
<version>${drools.version}</version>
<!--注意:此处必须排除传递过来的依赖,否则会跟我们自己导入的Spring jar包产生冲突-->
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
创建规则目录
在resources资源路径下创建目录rules,之后编写的规则文件都可以放在该目录下
先写一个helloWorld的规则文件为了测试整合效果:
package helloworld
rule "rule_helloworld"
when
eval(true)
then
System.out.println("规则:rule_helloworld触发...");
end
配置Spring文件
创建Spring配置文件/resources/spring.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"></kie:ksession>
</kie:kbase>
</kie:kmodule>
<bean class="org.kie.spring.annotations.KModuleAnnotationPostProcessor"></bean>
</beans>
测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring.xml")
public class DroolsSpringTest {
@KBase("kbase")
private KieBase kieBase;//注入KieBase对象
@Test
public void test1(){
KieSession kieSession = kieBase.newKieSession();
kieSession.fireAllRules();
kieSession.dispose();
}
}
spring整合drools主要是引入kieBase ,注入drools容器,drools规则触发主要还是使用KieSession 。