规则引擎Drools使用 第十五篇 Spring Boot整合Drools
程序员文章站
2022-05-28 11:17:17
...
在实际开发中,主要使用的还是以Spring Boot为主,所有下面介绍下Spring Boot整合Drools.
Spring Boot整合Drools 引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!--drools规则引擎-->
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>7.6.0.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>7.6.0.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-templates</artifactId>
<version>7.6.0.Final</version>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-api</artifactId>
<version>7.6.0.Final</version>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-spring</artifactId>
<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>
<version>7.6.0.Final</version>
</dependency>
注意要配置加载资源路径,你可以在pom里面配置:
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
但是不推荐,配置静态资源路径可以直接在启动文件application.properties配置或者直接写一个配置类,一般推荐使用编写配置类。
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
/**
* 静态资源加载配置
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
registry.addResourceHandler("/rules/**").addResourceLocations("classpath:/rules/");
}
}
编写配置类DroolsConfig
@Configuration
public class DroolsConfig {
private static final String RULES_PATH = "rules/";
private final KieServices kieServices = KieServices.Factory.get();
private KieServices getKieServices() {
return KieServices.Factory.get();
}
@Bean
@ConditionalOnMissingBean
public KieFileSystem kieFileSystem() throws IOException {
KieFileSystem kieFileSystem = getKieServices().newKieFileSystem();
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
//得到单个文件路径
// org.kie.api.io.Resource resource = kieServices.getResources().newClassPathResource("classpath*:" + RULES_PATH + "*.*");
// resource.setResourceType(ResourceType.DRL);
Resource[] files = resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "*.*");
String path = null;
for (Resource file : files) {
path = RULES_PATH + file.getFilename();
org.kie.api.io.Resource resource = ResourceFactory.newClassPathResource(path, "UTF-8");
resource.setResourceType(ResourceType.DRL);
kieFileSystem.write(resource);
}
return kieFileSystem;
}
@Bean
@ConditionalOnMissingBean
public KieContainer kieContainer() throws IOException {
KieRepository kieRepository = getKieServices().getRepository();
kieRepository.addKieModule(kieRepository::getDefaultReleaseId);
KieBuilder kieBuilder = getKieServices().newKieBuilder(kieFileSystem());
kieBuilder.buildAll();
KieContainer kieContainer = getKieServices().newKieContainer(kieRepository.getDefaultReleaseId());
return kieContainer;
}
@Bean
@ConditionalOnMissingBean
public KieBase kieBase() throws IOException {
KieBase kieBase = kieContainer().getKieBase();
return kieBase;
}
@Bean
@ConditionalOnMissingBean
public KieSession kieSession() throws IOException {
KieSession kieSession = kieBase().newKieSession();
// kieSession.dispose();
return kieSession;
}
@Bean
@ConditionalOnMissingBean
public KModuleBeanFactoryPostProcessor kiePostProcessor() {
return new KModuleBeanFactoryPostProcessor();
}
@Bean
@ConditionalOnMissingBean
public KieHelper kieHelper() {
return new KieHelper();
}
}
创建RuleService类
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class RuleService {
@Autowired
private KieBase kieBase;
public void rule(){
KieSession kieSession = kieBase.newKieSession();
kieSession.fireAllRules();
kieSession.dispose();
}
}
上面就完成了sring Boot整合Drools
上一篇: 数据库开发四:JDBC数据库开发进阶三(JDBC工具类-JdbcUtils)
下一篇: 数据库开发四:JDBC数据库开发进阶三(jdbcutils拓展QueryRunner处理事务以及处理多线程并发)
推荐阅读
-
Spring Boot中使用Drools规则引擎
-
【Drools规则引擎】Spring 整合Drools实例
-
规则引擎Drools使用讲解 第六篇 Drools 指定规则运行
-
规则引擎Drools使用 第十二篇 Drools 的高级语法之RHS加强
-
规则引擎Drools使用讲解 第五篇 Drools内置方法
-
规则引擎Drools使用讲解 第三篇Drools规则引擎简单使用
-
规则引擎Drools使用 第九篇 Drools 的高级语法之query使用
-
规则引擎Drools使用 第十篇 Drools 的高级语法之function使用
-
规则引擎Drools使用 第十五篇 Spring Boot整合Drools
-
规则引擎 Drools:Spring整合Drools