当前位置: 代码迷 >> 综合 >> 规则引擎 Drools:Spring整合Drools
  详细解决方案

规则引擎 Drools:Spring整合Drools

热度:45   发布时间:2024-02-19 14:14:48.0

在项目中使用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"wheneval(true)thenSystem.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://drools.org/schema/kie-springhttp://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对象@Testpublic void test1(){
    KieSession kieSession = kieBase.newKieSession();kieSession.fireAllRules();kieSession.dispose();}
}
  相关解决方案