规则引擎 Drools:Spring整合Drools
程序员文章站
2022-05-28 11:09:59
...
在项目中使用Drools时往往会跟Spring整合来使用。具体整合步骤如下:
第一步:创建maven工程drools_spring并配置pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.com.javakf</groupId>
<artifactId>drools_spring</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<drools.version>7.10.0.Final</drools.version>
<spring.version>5.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<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>
</dependencies>
</project>
第二步:创建规则目录/resources/rules,中rules目录中创建规则文件helloworld.drl
package helloworld
rule "rule_helloworld"
when
eval(true)
then
System.out.println("规则:rule_helloworld触发...");
end
第三步:创建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>
第四步:编写单元测试类
package cn.com.javakf.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kie.api.KieBase;
import org.kie.api.cdi.KBase;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@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();
}
}
下一篇: Drools记录执行的规则
推荐阅读
-
Spring Boot与Kotlin 整合全文搜索引擎Elasticsearch的示例代码
-
Spring Boot与Kotlin 整合全文搜索引擎Elasticsearch的示例代码
-
drools规则语法(一)
-
Drools 5.2 发布:开源的业务规则引擎
-
SpringBoot整合Drools的实现步骤
-
drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)
-
SpringBoot整合Drools规则引擎动态生成业务规则的实现
-
Drools规则引擎-Drools入门案例
-
开源力量公开课第70期:新一代规则引擎Drools概览
-
规则引擎Drools的资料整理