http://justsee.iteye.com/blog/1279082
Drools5.2.0.Final与Spring3集成测试

在drools5.2,有一个jar包:drools-spring-5.2.0.Final.jar,其中定义了在spring中应用的drools的扩展。通过这些扩展,可以直接在spring的配置文件中,配置knowledgebase、session等bean,从而在spring配置的程序中直接应用。

drools-spring-5.2.0.Final.jar在droolsjbpm-integration-distribution-5.2.0.Final\binaries文件夹下。

登录例子部分代码:

beans.xml

Xml代码 
1.<?xml version="1.0" encoding="UTF-8"?> 
2.<beans xmlns="http://www.springframework.org/schema/beans" 
3.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
4.    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 
5.    xmlns:p="http://www.springframework.org/schema/p" 
6.    xsi:schemaLocation="http://www.springframework.org/schema/beans     
7.        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     
8.        http://www.springframework.org/schema/context     
9.         http://www.springframework.org/schema/context/spring-context-3.0.xsd     
10.        http://www.springframework.org/schema/tx     
11.        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd     
12.        http://www.springframework.org/schema/aop      
13.         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">   
14.     <import resource="classpath:com/jsptpd/rjy/zyj/drools/beans-drools.xml"/> 
15.</beans> 
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans  
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
   http://www.springframework.org/schema/context  
   http://www.springframework.org/schema/context/spring-context-3.0.xsd  
   http://www.springframework.org/schema/tx  
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
   http://www.springframework.org/schema/aop   
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
     <import resource="classpath:com/jsptpd/rjy/zyj/drools/beans-drools.xml"/>
</beans> 
beans-drools.xml

Xml代码 
1.<?xml version="1.0" encoding="UTF-8"?> 
2.<beans xmlns="http://www.springframework.org/schema/beans" 
3.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
4.       xmlns:drools="http://drools.org/schema/drools-spring"   
5.       xmlns:camel="http://camel.apache.org/schema/spring" 
6.       xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
7.                           http://drools.org/schema/drools-springhttp://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-container/drools-spring/src/main/resources/org/drools/container/spring/drools-spring-1.0.0.xsd  
8.                           http://camel.apache.org/schema/springhttp://camel.apache.org/schema/spring/camel-spring.xsd"> 
9. 
10.  <drools:kbase id="kbase1"> 
11.     <drools:resources> 
12.          <!--不是<drools:resource type="DRL" source="classpath:com/jsptpd/rjy/zyj/service/Login.drl"/> --> 
13.         <drools:resource type="DRL" source="classpath:Login.drl"/> 
14.     </drools:resources> 
15.  </drools:kbase> 
16. 
17.  <drools:ksession id="ksession1" type="stateful" kbase="kbase1"/> 
18. 
19.   <bean id="vip" class="com.jsptpd.rjy.zyj.pojo.Vip" /> 
20.   <bean id="loginService" class="com.jsptpd.rjy.zyj.service.LoginServiceImpl" > 
21.        <property name="vip" ref="vip" /> 
22.   </bean> 
23.</beans> 
<?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:drools="http://drools.org/schema/drools-spring"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                           http://drools.org/schema/drools-springhttp://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-container/drools-spring/src/main/resources/org/drools/container/spring/drools-spring-1.0.0.xsd
                           http://camel.apache.org/schema/springhttp://camel.apache.org/schema/spring/camel-spring.xsd">

<drools:kbase id="kbase1">
     <drools:resources>
          <!--不是<drools:resource type="DRL" source="classpath:com/jsptpd/rjy/zyj/service/Login.drl"/> -->
         <drools:resource type="DRL" source="classpath:Login.drl"/>
     </drools:resources>
  </drools:kbase>

<drools:ksession id="ksession1" type="stateful" kbase="kbase1"/>

<bean id="vip" class="com.jsptpd.rjy.zyj.pojo.Vip" />
   <bean id="loginService" class="com.jsptpd.rjy.zyj.service.LoginServiceImpl" >
        <property name="vip" ref="vip" />
   </bean>
</beans>
 
LoginTest.java

Java代码 
1.package com.jsptpd.rjy.zyj.junit;  
2. 
3.import org.drools.runtime.StatefulKnowledgeSession;  
4.import org.junit.Test;  
5.import org.springframework.context.support.ClassPathXmlApplicationContext;  
6. 
7.import com.jsptpd.rjy.zyj.service.LoginServiceImpl;  
8. 
9.public class LoginTest {  
10.    @Test 
11.    public void testLogin(){  
12.        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "beans.xml" );  
13.        LoginServiceImpl loginServiceImpl= (LoginServiceImpl) context.getBean( "loginService" );  
14.        StatefulKnowledgeSession kstateless = (StatefulKnowledgeSession) context.getBean( "ksession1" );  
15.        loginServiceImpl.checkLogin(kstateless);  
16.        System.out.println("aa");  
17.    }  
18.} 
package com.jsptpd.rjy.zyj.junit;

import org.drools.runtime.StatefulKnowledgeSession;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jsptpd.rjy.zyj.service.LoginServiceImpl;

public class LoginTest {
 @Test
 public void testLogin(){
     ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "beans.xml" );
     LoginServiceImpl loginServiceImpl= (LoginServiceImpl) context.getBean( "loginService" );
     StatefulKnowledgeSession kstateless = (StatefulKnowledgeSession) context.getBean( "ksession1" );
     loginServiceImpl.checkLogin(kstateless);
     System.out.println("aa");
 }
}
 
LoginServiceImpl.java

Java代码 
1.package com.jsptpd.rjy.zyj.service;  
2. 
3.import org.drools.runtime.StatefulKnowledgeSession;  
4.import org.drools.runtime.StatelessKnowledgeSession;  
5.import org.springframework.context.support.ClassPathXmlApplicationContext;  
6. 
7.import com.jsptpd.rjy.zyj.pojo.Vip;  
8. 
9.public class LoginServiceImpl {  
10.        private Vip vip;  
11. 
12.        public Vip getVip() {  
13.            return vip;  
14.        }  
15. 
16.        public void setVip(Vip vip) {  
17.            this.vip = vip;  
18.        }  
19.             
20.        public void checkLogin(StatefulKnowledgeSession kstateless ){  
21.            System.out.println("s");  
22.            kstateless.insert(vip);  
23.            kstateless.fireAllRules();  
24.            kstateless.dispose();  
25.            System.out.println("e");  
26.        }  
27.           
28.        public static boolean checkDB(String name,String password){  
29.            //实际可以到数据库匹配  
30.            return name.trim().equals("jack")&&password.trim().equals("123");  
31.        }  
32.          
33.} 
package com.jsptpd.rjy.zyj.service;

import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.StatelessKnowledgeSession;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jsptpd.rjy.zyj.pojo.Vip;

public class LoginServiceImpl {
        private Vip vip;

public Vip getVip() {
   return vip;
  }

public void setVip(Vip vip) {
   this.vip = vip;
  }
          
  public void checkLogin(StatefulKnowledgeSession kstateless ){
   System.out.println("s");
   kstateless.insert(vip);
   kstateless.fireAllRules();
   kstateless.dispose();
   System.out.println("e");
  }
        
  public static boolean checkDB(String name,String password){
   //实际可以到数据库匹配
   return name.trim().equals("jack")&&password.trim().equals("123");
  }
  
}
 
Login.drl

Java代码 
1.#created on: 2011-10-24 
2.package com.jsptpd.rjy.zyj.service  
3. 
4.#list any import classes here.  
5.import com.jsptpd.rjy.zyj.pojo.Vip;  
6.import java.io.Console;  
7.import java.util.Scanner;  
8.import com.jsptpd.rjy.zyj.service.LoginServiceImpl  
9. 
10.#declare any global variables here  
11. 
12. 
13. 
14. 
15.rule "vip初次登录" 
16.    salience 100 
17.    when  
18.        $vip:Vip((name==null||name=="")&&  
19.                 (password==null||password=="") )  
20.    then  
21.        String tempName;  
22.        String tempPassword;  
23.        Console console=System.console();  
24.        Scanner scanner = new Scanner(System.in);  
25.        System.out.print("请输入用户名: ");     
26.        tempName=(console!=null?console.readLine():scanner.nextLine());  
27.        System.out.print("请输入密码: ");  
28.        tempPassword=(console!=null?new String(console.readPassword()):scanner.nextLine());  
29.        $vip.setName(tempName.trim());  
30.        $vip.setPassword(tempPassword.trim());  
31.        update($vip);  
32.end  
33. 
34.rule "没有输入密码" 
35.    salience  90 
36.    when  
37.       $vip:Vip((name!=null&&name!="")&&  
38.                 (password==null||password==""),$name:name)  
39.    then  
40.        String tempPassword="";  
41.        Console console=System.console();  
42.        Scanner scanner = new Scanner(System.in);  
43.        System.out.print("请输入密码: ");  
44.        tempPassword=(console!=null?new String(console.readPassword()):scanner.nextLine());  
45.        $vip.setPassword(tempPassword.trim());  
46.        update($vip);  
47. 
48.end  
49. 
50. 
51.rule "没有输入用户名" 
52.    salience  90 
53.    when  
54.       $vip:Vip((name==null||name=="")&&  
55.                 (password!=null&&password!=""),$password:password )  
56.    then  
57.        String tempName="";  
58.        Scanner scanner = new Scanner(System.in);  
59.        System.out.print("请输入用户名: ");     
60.        tempName=scanner.nextLine();  
61.        $vip.setName(tempName.trim());  
62.        update($vip);  
63. 
64.end  
65. 
66. 
67.rule "输入正确的用户名和密码" 
68.    salience  80 
69.    when  
70.       $vip:Vip((name!=null&&name!=""),  
71.                 (password!=null&&password!=""),LoginServiceImpl.checkDB(name,password) )  
72.    then  
73.        System.out.print(" 欢迎 !!!"+$vip.getName());   
74. 
75.end  
76. 
77.rule "输入错误的用户名和密码" 
78.    salience  80 
79.    when  
80.       $vip:Vip((name!=null&&name!=""),  
81.                 (password!=null&&password!=""),!LoginServiceImpl.checkDB(name,password) )  
82.    then  
83.        System.out.print(" 输入错误用户名或密码,请重新输入 !!!\n");      
84.        $vip.setName("");  
85.        $vip.setPassword("");  
86.        update($vip);  
87.end 
#created on: 2011-10-24
package com.jsptpd.rjy.zyj.service

#list any import classes here.
import com.jsptpd.rjy.zyj.pojo.Vip;
import java.io.Console;
import java.util.Scanner;
import com.jsptpd.rjy.zyj.service.LoginServiceImpl

#declare any global variables here

rule "vip初次登录"
    salience 100
    when
        $vip:Vip((name==null||name=="")&&
                 (password==null||password=="") )
    then
        String tempName;
     String tempPassword;
     Console console=System.console();
     Scanner scanner = new Scanner(System.in);
     System.out.print("请输入用户名: "); 
  tempName=(console!=null?console.readLine():scanner.nextLine());
  System.out.print("请输入密码: ");
  tempPassword=(console!=null?new String(console.readPassword()):scanner.nextLine());
        $vip.setName(tempName.trim());
        $vip.setPassword(tempPassword.trim());
        update($vip);
end

rule "没有输入密码"
    salience  90
    when
       $vip:Vip((name!=null&&name!="")&&
                 (password==null||password==""),$name:name)
    then
     String tempPassword="";
     Console console=System.console();
     Scanner scanner = new Scanner(System.in);
  System.out.print("请输入密码: ");
  tempPassword=(console!=null?new String(console.readPassword()):scanner.nextLine());
        $vip.setPassword(tempPassword.trim());
        update($vip);

end

rule "没有输入用户名"
    salience  90
    when
       $vip:Vip((name==null||name=="")&&
                 (password!=null&&password!=""),$password:password )
    then
        String tempName="";
     Scanner scanner = new Scanner(System.in);
     System.out.print("请输入用户名: "); 
  tempName=scanner.nextLine();
        $vip.setName(tempName.trim());
        update($vip);

end

rule "输入正确的用户名和密码"
    salience  80
    when
       $vip:Vip((name!=null&&name!=""),
                 (password!=null&&password!=""),LoginServiceImpl.checkDB(name,password) )
    then
        System.out.print(" 欢迎 !!!"+$vip.getName());

end

rule "输入错误的用户名和密码"
    salience  80
    when
       $vip:Vip((name!=null&&name!=""),
                 (password!=null&&password!=""),!LoginServiceImpl.checkDB(name,password) )
    then
        System.out.print(" 输入错误用户名或密码,请重新输入 !!!\n"); 
        $vip.setName("");
        $vip.setPassword("");
        update($vip);
end

Drools与Spring集成 登录测试相关推荐

  1. Spring集成TestNg测试

    1,在eclipse中安装TestNg插件,这里省略.. 2,编写测试spring Dao层的代码 package test.com.smart.dao; import com.smart.dao.U ...

  2. Spring集成文件轮询和测试

    我最近实施了一个小项目,在该项目中,我们必须轮询文件夹中的新文件,然后在文件内容上触发服务流. Spring Integration非常适合此要求,因为它带有一个通道适配器 ,该适配器可以扫描文件夹中 ...

  3. Spring的junit4测试集成

    Spring的junit测试集成 Spring提供spring-test-4.2.4.RELEASE.jar 可以整合junit. 优势:可以简化测试代码(不需要手动创建上下文,即手动创建spring ...

  4. Spring集成Shiro框架实战

    文章目录 一:什么是Shiro框架 二:Shiro框架简介 1.Shiro基础功能点介绍 2.Shiro的工作原理 3.Shiro的内部工作结构 4.Shiro的身份认证流程 三:Spring集成Sh ...

  5. 第12章 与Spring集成

    Shiro的组件都是JavaBean/POJO式的组件,所以非常容易使用Spring进行组件管理,可以非常方便的从ini配置迁移到Spring进行管理,且支持JavaSE应用及Web应用的集成.在示例 ...

  6. 引入Spring集成

    在本文中,我们介绍Spring Integration . 如果您以前没有使用过Spring Integration,那么可能会帮助您复习Gregor Hohpe的Enterprise Integra ...

  7. SSM + Shiro 整合 (2)- 实现 Spring 集成 MyBatis

    项目源码:https://github.com/weimingge14/Shiro-project 演示地址:http://liweiblog.duapp.com/Shiro-project/logi ...

  8. spring集成mina 实现消息推送以及转发

    spring集成mina: 在学习mina这块时,在网上找了很多资料,只有一些demo,只能实现客户端向服务端发送消息.建立长连接之类.但是实际上在项目中,并不简单实现这些,还有业务逻辑之类的处理以及 ...

  9. Spring集成Redis方案(spring-data-redis)(基于Jedis的单机模式)(待实践)

    说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...

最新文章

  1. 快上车!“正经”文章告诉你如何构建与使用分布式中间件平台实践
  2. vue中如何使用i18n实现国际化
  3. PlanAhead工具应用
  4. 关于面试的部分内容总结#1
  5. mysql 1539_MySQL:半同步(三)从库端初始化和回调函数
  6. Windows窗体编程基础学习:更改TabControl 的外观(如qq用的)
  7. 无交换机实现集群网络互联
  8. 深入解析JQuery中的isPlainObject()使用方法 1
  9. tablelayout
  10. Unity3D shader简介
  11. Python游戏汇总:三十个pygame游戏代码【附源码免费分享】
  12. 人工智能(第一章 绪论)
  13. 造轮子,layuiAdmin——基于layui的后台管理模板
  14. MTK_on_line_FAQ_SW_ALPS_System+-+Bootup
  15. 明星开餐饮店,逃不过凉凉的魔咒?
  16. linux邮箱客户端安装指令,如何在Ubuntu 18.04中安装邮件客户端Geary 0.12.2
  17. 解决:更改短信中心号码不能及时更新显示
  18. PAZU -- 4Fang WEB 打印控件
  19. 像科学家一样思考python列表_像计算机科学家一样思考python-第3章 函数
  20. Unquotted string 错误

热门文章

  1. LeetCode Algorithm 204. 计数质数
  2. 2013\National _C_C++_C\1.好好学习
  3. 2017 年全国大学生电子设计竞赛试题——四旋翼自主飞行器探测跟踪系统(C 题)【本科组】1
  4. 2018第九届蓝桥杯C/C++ B国赛 —— 第三题:格雷码
  5. 征战蓝桥 —— 2013年第四届 —— C/C++A组第7题——错误票据
  6. Java 套接字Socket
  7. Java 静态导入 、可变参数
  8. python:从入门到实践-----外星人入侵的图片问题
  9. 【IT资讯】阿里新推出“阿里云网盘” 速度10MBs,百度网盘或迎来最强对手!
  10. windows如何调整某个应用程序的音量 c++_微软对重启Windows 10的应用程序进行控制测试...