Spring筆記之九(AOP in Spring)
??? AOP概念:
??? Advice:如何將before通知、afterReturning通知和afterThrowing通知聲明為bean。
Pointcut:如何聲明靜態切入點邏輯以將XML Spring Bean Configuration文件中的所有內容聯系在一起。
Advisor:關聯切入點定義與通知bean的方式。
??? Spring AOP是使用代理來完成的,Spring 兩種方式:JDK動態代理,需要設定一組代理接口;CGLIB 代理,可代理接口和類。Spring提供了5種Advice類型:Interception Around、Before、After Returning、Throw和Introduction。它們分別在以下情況下被調用:在JointPoint前后、JointPoint前、 JointPoint后、JointPoint拋出異常時、JointPoint調用完畢后。
配置文件:
?1?<beans>
?2?????<bean?id="myAOPProxy"
???????????? class="org.springframework.aop.framework.ProxyFactoryBean">
?3?????????<property?name="proxyInterfaces">
?4?????????????<value>ITest</value>
?5?????????</property>
?6?????????<property?name="target">
?7?????????????<ref?local?=?"test"/>
?8?????????</property>
?9?????????<property?name="interceptorNames">
10?????????????<value>myPotincutAdvisor</value>
11?????????</property>
12?????</bean>
13?????
14?????<bean?id="test"?class="Test"/>
15?????
16?????<bean?id="MyInterceptor"?class="MethodTimeCostInterceptor"/>
17?????
18?????<bean?id="myPotincutAdvisor"
???????????? class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
19?????????<property?name="advice">
20?????????????<ref?local="MyInterceptor"/>
21?????????</property>
22?????????<property?name="patterns">
23?????????????<list>
24?????????????????<value>.*</value>
25?????????????????<value>.*</value>
26?????????????</list>
27?????????</property>
28?????</bean>
29?</beans>
?2?????<bean?id="myAOPProxy"
???????????? class="org.springframework.aop.framework.ProxyFactoryBean">
?3?????????<property?name="proxyInterfaces">
?4?????????????<value>ITest</value>
?5?????????</property>
?6?????????<property?name="target">
?7?????????????<ref?local?=?"test"/>
?8?????????</property>
?9?????????<property?name="interceptorNames">
10?????????????<value>myPotincutAdvisor</value>
11?????????</property>
12?????</bean>
13?????
14?????<bean?id="test"?class="Test"/>
15?????
16?????<bean?id="MyInterceptor"?class="MethodTimeCostInterceptor"/>
17?????
18?????<bean?id="myPotincutAdvisor"
???????????? class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
19?????????<property?name="advice">
20?????????????<ref?local="MyInterceptor"/>
21?????????</property>
22?????????<property?name="patterns">
23?????????????<list>
24?????????????????<value>.*</value>
25?????????????????<value>.*</value>
26?????????????</list>
27?????????</property>
28?????</bean>
29?</beans>
分析代碼:
1、<bean?id="myAOPProxy"
???? class="org.springframework.aop.framework.ProxyFactoryBean">聲明注入了代理實例myAOPProxy。 ??????
2、 proxyInterfaces聲明將被代理接口ITest。
3、 target聲明被代理目的類。
4、 interceptorNames設置攔截器為myPotincutAdvisor。
5、 patterns為攔截器設置配匹方式,即在所被配匹成功的方法被調用時執行攔截器內容。
??? 該配置文件,指定要加載一個接口與ITest相匹配的bean。該bean隨后被關聯到Test實現類。看起來好像是費了很大力氣只為了加載一個簡單的bean并調用一個方法,但是這個配置文件只是使 Spring框架可以透明地對應用程序應用其組件的眾多特性的一個體現。
?
鳳凰涅槃/浴火重生/馬不停蹄/只爭朝夕
???? 隱姓埋名/低調華麗/簡單生活/完美人生
posted on 2007-09-29 01:50 poetguo 閱讀(1047) 評論(1) 編輯 收藏 所屬分類: Spring