Posted on 2006-04-23 15:17
oxl 閱讀(1148)
評(píng)論(0) 編輯 收藏 所屬分類:
J2EE 設(shè)計(jì)模式與應(yīng)用
這一周我都在用空余的時(shí)間來(lái)看Spring這個(gè)東西,突然覺(jué)得這個(gè)東西并沒(méi)有想像中那么深不可測(cè),最多只有AOP這方面的東西不是我所熟悉的,不過(guò)經(jīng)過(guò)自己努力,也不是不可理解的。
AOP,面向切面的編程,是一種從各個(gè)方面對(duì)軟件系統(tǒng)進(jìn)行關(guān)注的開(kāi)發(fā)方法,也就是說(shuō),可以把要實(shí)現(xiàn)的系統(tǒng)從多個(gè)方面進(jìn)行理解和開(kāi)發(fā)。比如一個(gè)系統(tǒng),它包含了業(yè)務(wù)邏輯,日志管理,安全管理(認(rèn)證管理)等,一般而言,我在開(kāi)發(fā)包含這些方面的程序時(shí),就很容易進(jìn)入單面一維的操作去:比如下面是一個(gè)用戶管理程序的部份:
1、驗(yàn)證用戶是否已經(jīng)登錄
2、記錄日志
3、進(jìn)行操作,對(duì)重要的操作都可能要進(jìn)行日志記錄。
4、記錄用戶離開(kāi)時(shí)間
......
由上面的步驟可以看到,真正的業(yè)務(wù)邏輯只有第3步,其它的都是附加的功能,或者是從另一方面加入的功能,他們和業(yè)務(wù)邏輯是相輔相成,可是業(yè)務(wù)邏輯并不需要知道這些附加的功能,又從另一方面來(lái)看,每一進(jìn)行的操作都會(huì)觸發(fā)附加的操作,而且這些應(yīng)該是自動(dòng)的,業(yè)務(wù)邏輯所不知道的。
所以就引進(jìn)一個(gè)攔截器的概念,對(duì)用戶的操作進(jìn)行攔截,然后進(jìn)行附加的操作。而用戶的操作并不知道這些攔截器的存在,從而可以使開(kāi)發(fā)業(yè)務(wù)邏輯的程序員可以專心地寫(xiě)他的業(yè)務(wù)邏輯,而其它程序員則專心寫(xiě)他的攔截器,從而就可以使程序員從多個(gè)方面進(jìn)行關(guān)注,進(jìn)行編程。而Spring AOP就起到了這個(gè)攔截器的作用,而這些就演化出很多的術(shù)語(yǔ),也無(wú)非是攔截器而已,現(xiàn)在俗一點(diǎn)來(lái)理解這些個(gè)術(shù)語(yǔ):
切面:宏觀上要關(guān)注的編程方面,比如安全管理方面,日志記錄方面,而這些都只是宏觀上的,沒(méi)有任何代碼實(shí)現(xiàn),就好像這樣說(shuō):我這個(gè)系統(tǒng)要一個(gè)安全管理系統(tǒng)等等,就是大至上需要這個(gè)切面功能。
通知(Advice):切面功能的代碼實(shí)現(xiàn),也就是說(shuō)具體的切面功能。
連接點(diǎn)(Joinpoint):程序運(yùn)行中可以把通知道插入的所有地方,比如某方法前,后,或拋出異常,而所以這些方法的插入點(diǎn)都是連接點(diǎn)。
切入點(diǎn)(Pointcut):定義了通知可以插入到哪些連接點(diǎn)。
Advisor:這里只有Spring才有,它包含了通知和切入點(diǎn),以定義在什么連接點(diǎn)插入什么通知。
下面我用一個(gè)代碼例子來(lái)說(shuō)明:
文件:SayHelloService.java
1?package
?com.bullonline.study.spring.ch03;
2?
3?public?interface
?SayHelloService?{
4?????public?void
?printMessage(String?message);
5?????public?void
?printInfomation(String?info);
6?
}
7
?
文件:SayHelloServiceImpl.java
文件:SayHelloAdvice.java
?1?package
?com.bullonline.study.spring.ch03;
?2?
?3?import
?java.lang.reflect.Method;
?4?
?5?import
?org.springframework.aop.MethodBeforeAdvice;
?6?
?7?public?class?SayHelloAdvice?implements
?MethodBeforeAdvice?{
?8?
?9?????public?void
?before(Method?method,?Object[]?args,?Object?target)
10?????????????throws
?Throwable?{
11?????????System.out.println(method.toString()?+?":?say?hello?to?you!!"
);
12?
????}
13?
14?}
文件:MyClassFilter.java
?1?package
?com.bullonline.study.spring.ch03;
?2?
?3?import
?org.springframework.aop.ClassFilter;
?4?
?5?public?class?MyClassFilter?implements
?ClassFilter?{
?6?
?7?????public?boolean
?matches(Class?target)?{
?8?????????return?true
;
?9?
????}
10?
11?
}
12
?
文件:MyMethodMatcher.java
?1?package
?com.bullonline.study.spring.ch03;
?2?
?3?import
?java.lang.reflect.Method;
?4?
?5?import
?org.springframework.aop.MethodMatcher;
?6?
?7?public?class?MyMethodMatcher?implements
?MethodMatcher?{
?8?
?9?????public?boolean
?matches(Method?method,?Class?target)?{
10?????????String?methodName?=
?method.getName();
11?????????if?(methodName.equals("printMessage"
))?{
12?????????????return?true
;
13?????????}?else
?{
14?????????????return?false
;
15?
????????}
16?
????}
17?
18?????public?boolean
?isRuntime()?{
19?????????return?false
;
20?
????}
21?
22?????public?boolean
?matches(Method?arg0,?Class?arg1,?Object[]?arg2)?{
23?????????return?false
;
24?
????}
25?
26?
}
27
?
文件:MyPointcut.java
?1?package?com.bullonline.study.spring.ch03;
?2?
?3?import?org.springframework.aop.ClassFilter;
?4?import?org.springframework.aop.MethodMatcher;
?5?import?org.springframework.aop.Pointcut;
?6?
?7?public?class?MyPointcut?implements?Pointcut?{
?8?????private?ClassFilter?filter;
?9?????private?MethodMatcher?matcher;
10?????
11?????public?void?setClassFilter(ClassFilter?filter)?{
12?????????this.filter?=?filter;
13?????}
14?
15?????public?void?setMethodMatcher(MethodMatcher?matcher)?{
16?????????this.matcher?=?matcher;
17?????}
18?????
19?????public?ClassFilter?getClassFilter()?{
20?????????return?this.filter;
21?????}
22?
23?????public?MethodMatcher?getMethodMatcher()?{
24?????????return?this.matcher;
25?????}
26?
27?}
28?
文件:MyPointcutAdvisor.java
?1?package?com.bullonline.study.spring.ch03;
?2?
?3?import?org.aopalliance.aop.Advice;
?4?import?org.springframework.aop.Pointcut;
?5?import?org.springframework.aop.PointcutAdvisor;
?6?
?7?public?class?MyPointcutAdvisor?implements?PointcutAdvisor?{
?8?????private?Pointcut?pointcut;
?9?????private?Advice?advice;
10?????
11?????public?void?setPointcut(Pointcut?pointcut)?{
12?????????this.pointcut?=?pointcut;
13?????}
14?????
15?????public?void?setAdvice(Advice?advice)?{
16?????????this.advice?=?advice;
17?????}
18?
19?????public?Pointcut?getPointcut()?{
20?????????return?this.pointcut;
21?????}
22?
23?????public?boolean?isPerInstance()?{
24?????????return?false;
25?????}
26?
27?????public?Advice?getAdvice()?{
28?????????return?this.advice;
29?????}
30?
31?}
32?
文件:TestApp.java
?1?
package?com.bullonline.study.spring.ch03;
?2?
?3?
import?org.springframework.beans.factory.BeanFactory;
?4?
import?org.springframework.beans.factory.xml.XmlBeanFactory;
?5?
import?org.springframework.core.io.ClassPathResource;
?6?
?7?
public?class?TestApp?{
?8?
????public?static?void?main(String[]?args)?{
?9?
????????BeanFactory?factory?=?new?XmlBeanFactory(new?ClassPathResource("applicationContext.xml"));
10?
????????SayHelloService?service?=?(SayHelloService)?factory.getBean("sayHelloService");
11?
????????service.printMessage("Hello,?World!!");
12?
????????service.printInfomation("haha
..");
13?
????????
14?
????}
15?
16?
}
17?
文件:applicationContext.xml
?1?
<?xml?version="1.0"?encoding="UTF-8"?>
?2?
<!DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"?"http://www.springframework.org/dtd/spring-beans.dtd">
?3?
?4?
<beans>
?5?
????<bean?id="helloWorld"?class="com.bullonline.study.spring.ch01.HelloWorld">
?6?
????????<property?name="message"?value="Haha
.Hello,?World!!"></property>
?7?
????</bean>
?8?
????
?9?
????<bean?id="sayHelloServiceImpl"?class="com.bullonline.study.spring.ch03.SayHelloServiceImpl"?/>
10?
????<bean?id="classFilter"?class="com.bullonline.study.spring.ch03.MyClassFilter"?/>
11?
????<bean?id="methodMatcher"?class="com.bullonline.study.spring.ch03.MyMethodMatcher"?/>
12?
????<bean?id="sayHelloAdvice"?class="com.bullonline.study.spring.ch03.SayHelloAdvice"?/>
13?
????
14?
????<bean?id="myPointcut"?class="com.bullonline.study.spring.ch03.MyPointcut">
15?
????????<property?name="classFilter">
16?
????????????<ref?bean="classFilter"/>
17?
????????</property>
18?
????????<property?name="methodMatcher">
19?
????????????<ref?bean="methodMatcher"/>
20?
????????</property>
21?
????</bean>
22?
????
23?
????<bean?id="myPointcutAdvisor"?class="com.bullonline.study.spring.ch03.MyPointcutAdvisor">
24?
????????<property?name="pointcut">
25?
????????????<ref?bean="myPointcut"/>
26?
????????</property>
27?
????????<property?name="advice">
28?
????????????<ref?bean="sayHelloAdvice"/>
29?
????????</property>
30?
????</bean>
31?
????
32?
????<bean?id="sayHelloService"?class="org.springframework.aop.framework.ProxyFactoryBean">
33?
????????<property?name="proxyInterfaces">
34?
????????????<value>com.bullonline.study.spring.ch03.SayHelloService</value>
35?
????????</property>
36?
????????<property?name="interceptorNames">
37?
????????????<list>
38?
????????????????<value>myPointcutAdvisor</value>
39?
????????????</list>
40?
????????</property>
41?
????????<property?name="target">
42?
????????????<ref?bean="sayHelloServiceImpl"?/>
43?
????????</property>
44?
????</bean>
45?
</beans>
46?