javascript
Drools与Spring集成 登录测试
http://justsee.iteye.com/blog/1279082
Drools5.2.0.Final與Spring3集成測(cè)試
在drools5.2,有一個(gè)jar包:drools-spring-5.2.0.Final.jar,其中定義了在spring中應(yīng)用的drools的擴(kuò)展。通過(guò)這些擴(kuò)展,可以直接在spring的配置文件中,配置knowledgebase、session等bean,從而在spring配置的程序中直接應(yīng)用。
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.??????????? //實(shí)際可以到數(shù)據(jù)庫(kù)匹配??
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){
???//實(shí)際可以到數(shù)據(jù)庫(kù)匹配
???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("請(qǐng)輸入用戶名: ");?????
26.??????? tempName=(console!=null?console.readLine():scanner.nextLine());??
27.??????? System.out.print("請(qǐng)輸入密碼: ");??
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 "沒(méi)有輸入密碼"?
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("請(qǐng)輸入密碼: ");??
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 "沒(méi)有輸入用戶名"?
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("請(qǐng)輸入用戶名: ");?????
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 "輸入錯(cuò)誤的用戶名和密碼"?
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(" 輸入錯(cuò)誤用戶名或密碼,請(qǐng)重新輸入 !!!\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("請(qǐng)輸入用戶名: ");?
??tempName=(console!=null?console.readLine():scanner.nextLine());
??System.out.print("請(qǐng)輸入密碼: ");
??tempPassword=(console!=null?new String(console.readPassword()):scanner.nextLine());
??????? $vip.setName(tempName.trim());
??????? $vip.setPassword(tempPassword.trim());
??????? update($vip);
end
rule "沒(méi)有輸入密碼"
??? 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("請(qǐng)輸入密碼: ");
??tempPassword=(console!=null?new String(console.readPassword()):scanner.nextLine());
??????? $vip.setPassword(tempPassword.trim());
??????? update($vip);
end
rule "沒(méi)有輸入用戶名"
??? 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("請(qǐng)輸入用戶名: ");?
??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 "輸入錯(cuò)誤的用戶名和密碼"
??? salience? 80
??? when
?????? $vip:Vip((name!=null&&name!=""),
???????????????? (password!=null&&password!=""),!LoginServiceImpl.checkDB(name,password) )
??? then
??????? System.out.print(" 輸入錯(cuò)誤用戶名或密碼,請(qǐng)重新輸入 !!!\n");?
??????? $vip.setName("");
??????? $vip.setPassword("");
??????? update($vip);
end
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的Drools与Spring集成 登录测试的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Drools 在告警关联分析中的应用
- 下一篇: Spring batch Job def