目录

  • 7. Spring整合Drools
    • 7.1 Spring简单整合Drools
      • 7.1.1 以上代码均在drools_spring项目中
    • 7.2 Spring整合Drools+web
      • 7.2 以上代码均在drools_springweb项目中
    • 7.3 Spring Boot整合Drools
      • 7.3.1 以上代码均在drools_springboot项目中

7. Spring整合Drools

全套代码及资料全部完整提供,点此处下载

7.1 Spring简单整合Drools

在项目中使用Drools时往往会跟Spring整合来使用。具体整合步骤如下:

第一步:创建maven工程drools_spring并配置pom.xml

    4.0.0    com.itheima    drools_spring    1.0-SNAPSHOT            7.10.0.Final        5.0.5.RELEASE                            org.drools            drools-compiler            ${drools.version}                            junit            junit            4.12                            org.kie            kie-spring            ${drools.version}                                                            org.springframework                    spring-tx                                                    org.springframework                    spring-beans                                                    org.springframework                    spring-core                                                    org.springframework                    spring-context                                                        org.springframework            spring-context            ${spring.version}                            org.springframework            spring-context-support            ${spring.version}                            org.springframework            spring-test            ${spring.version}                            org.springframework            spring-tx            ${spring.version}            

第二步:创建规则目录/resources/rules,中rules目录中创建规则文件helloworld.drl

package helloworldrule "rule_helloworld"    when        eval(true)    then        System.out.println("规则:rule_helloworld触发...");end

第三步:创建Spring配置文件/resources/spring.xml

                                        

第四步:编写单元测试类

package com.itheima.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();    }}

7.1.1 以上代码均在drools_spring项目中

7.2 Spring整合Drools+web

本小节我们来进行Drools和Spring Web的整合。具体操作步骤如下:

第一步:创建maven的war工程drools_springweb并在pom.xml文件中导入相关maven坐标

  4.0.0  com.itheima  drools_springweb  1.0-SNAPSHOT  war      UTF-8    1.8    1.8    7.10.0.Final    5.0.5.RELEASE              org.drools      drools-compiler      ${drools.version}              junit      junit      4.12              org.kie      kie-spring      ${drools.version}                              org.springframework          spring-tx                          org.springframework          spring-beans                          org.springframework          spring-core                          org.springframework          spring-context                            org.springframework      spring-context      ${spring.version}              org.springframework      spring-context-support      ${spring.version}              org.springframework      spring-test      ${spring.version}              org.springframework      spring-tx      ${spring.version}              org.springframework      spring-web      ${spring.version}              org.springframework      spring-webmvc      ${spring.version}                          org.apache.tomcat.maven        tomcat7-maven-plugin                            80                    /                    

第二步:配置web.xml

  Archetype Created Web Application      springmvc    org.springframework.web.servlet.DispatcherServlet              contextConfigLocation      classpath:springmvc.xml        1        springmvc    *.do  

第三步:创建/resources/springmvc.xml文件

                                                            

第四步:创建规则文件/resources/rules/helloworld.drl

package helloworldrule "rule_helloworld"    when        eval(true)    then        System.out.println("规则:rule_helloworld触发...");end

第五步:创建RuleService

package com.itheima.service;import org.kie.api.KieBase;import org.kie.api.cdi.KBase;import org.kie.api.runtime.KieSession;import org.springframework.stereotype.Service;@Servicepublic class RuleService {    @KBase("kbase")    private KieBase kieBase;    public void rule(){        KieSession kieSession = kieBase.newKieSession();        kieSession.fireAllRules();        kieSession.dispose();    }}

第六步:创建HelloController

package com.itheima.controller;import com.itheima.service.RuleService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/hello")public class HelloController {    @Autowired    private RuleService ruleService;    @RequestMapping("/rule")    public String rule(){        ruleService.rule();        return "OK";    }}

7.2 以上代码均在drools_springweb项目中

7.3 Spring Boot整合Drools

目前在企业开发中Spring Boot已经成为主流,本小节我们来进行Spring Boot整合Drools。具体操作步骤:

第一步:创建maven工程drools_springboot并配置pom.xml

            org.springframework.boot        spring-boot-starters        2.0.6.RELEASE        4.0.0    com.itheima    drools_springboot    1.0-SNAPSHOT                        org.springframework.boot            spring-boot-starter-web                            org.springframework.boot            spring-boot-starter-aop                            org.springframework.boot            spring-boot-starter-test                            commons-lang            commons-lang            2.6                                    org.drools            drools-core            7.6.0.Final                            org.drools            drools-compiler            7.6.0.Final                            org.drools            drools-templates            7.6.0.Final                            org.kie            kie-api            7.6.0.Final                            org.kie            kie-spring                                                org.springframework                    spring-tx                                                    org.springframework                    spring-beans                                                    org.springframework                    spring-core                                                    org.springframework                    spring-context                                        7.6.0.Final                        ${project.artifactId}                                    src/main/java                                    **/*.xml                                false                                        src/main/resources                                    **/*.*                                false                                                        org.apache.maven.plugins                maven-compiler-plugin                2.3.2                                    1.8                    1.8                                        

第二步:创建/resources/application.yml文件

server:  port: 8080spring:  application:    name: drools_springboot

第三步:创建规则文件/resources/rules/helloworld.drl

package helloworldrule "rule_helloworld"    when        eval(true)    then        System.out.println("规则:rule_helloworld触发...");end

第四步:编写配置类DroolsConfig

package com.itheima.drools.config;import org.kie.api.KieBase;import org.kie.api.KieServices;import org.kie.api.builder.KieBuilder;import org.kie.api.builder.KieFileSystem;import org.kie.api.builder.KieRepository;import org.kie.api.runtime.KieContainer;import org.kie.api.runtime.KieSession;import org.kie.internal.io.ResourceFactory;import org.kie.spring.KModuleBeanFactoryPostProcessor;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.core.io.support.ResourcePatternResolver;import org.springframework.core.io.Resource;import java.io.IOException;/** * 规则引擎配置类 */@Configurationpublic class DroolsConfig {    //指定规则文件存放的目录    private static final String RULES_PATH = "rules/";    private final KieServices kieServices = KieServices.Factory.get();    @Bean    @ConditionalOnMissingBean    public KieFileSystem kieFileSystem() throws IOException {        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();        ResourcePatternResolver resourcePatternResolver =             new PathMatchingResourcePatternResolver();        Resource[] files =             resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "*.*");        String path = null;        for (Resource file : files) {            path = RULES_PATH + file.getFilename();            kieFileSystem.write(ResourceFactory.newClassPathResource(path, "UTF-8"));        }        return kieFileSystem;    }    @Bean    @ConditionalOnMissingBean    public KieContainer kieContainer() throws IOException {        KieRepository kieRepository = kieServices.getRepository();        kieRepository.addKieModule(kieRepository::getDefaultReleaseId);        KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem());        kieBuilder.buildAll();        return kieServices.newKieContainer(kieRepository.getDefaultReleaseId());    }    @Bean    @ConditionalOnMissingBean    public KieBase kieBase() throws IOException {        return kieContainer().getKieBase();    }    @Bean    @ConditionalOnMissingBean    public KModuleBeanFactoryPostProcessor kiePostProcessor() {        return new KModuleBeanFactoryPostProcessor();    }}

第五步:创建RuleService类

package com.itheima.drools.service;import org.kie.api.KieBase;import org.kie.api.runtime.KieSession;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class RuleService {    @Autowired    private KieBase kieBase;    public void rule(){        KieSession kieSession = kieBase.newKieSession();        kieSession.fireAllRules();        kieSession.dispose();    }}

第六步:创建HelloController类

package com.itheima.drools.controller;import com.itheima.drools.service.RuleService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/hello")public class HelloController {    @Autowired    private RuleService ruleService;    @RequestMapping("/rule")    public String rule(){        ruleService.rule();        return "OK";    }}

第七步:创建启动类DroolsApplication

package com.itheima.drools;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class DroolsApplication {    public static void main(String[] args) {        SpringApplication.run(DroolsApplication.class,args);    }}

第八步:启动服务,访问http://localhost:8080/hello/rule

7.3.1 以上代码均在drools_springboot项目中


全套代码及资料全部完整提供,点此处下载