t>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
第三步:创建/resources/springmvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.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"/>
<!--spring批量扫描-->
<context:component-scan base-package="com.itheima" />
<context:annotation-config/>
<!--springMVC注解驱动-->
<mvc:annotation-driven/>
</beans>
第四步:创建规则文件/resources/rules/helloworld.drl
package helloworld
rule "rule_helloworld"
when
eva l(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;
@Service
public 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
<?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 h