基于schema的風(fēng)格
先看一下配置文件(aop_config_schema.xml):
<?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:aop=”http://www.springframework.org/schema/aop”
xsi:schemaLocation=”
[url]http://www.springframework.org/schema/beans[/url]
[url]http://www.springframework.org/schema/beans/spring-beans-2.0.xsd[/url]
[url]http://www.springframework.org/schema/aop[/url]
[url]http://www.springframework.org/schema/aop/spring-aop-2.0.xsd[/url]“>
<!–
有了schema的支持,切面就和常規(guī)的Java對(duì)象一樣被定義成application context中的一個(gè)bean。
對(duì)象的字段和方法提供了狀態(tài)和行為信息,XML文件則提供了切入點(diǎn)和通知信息。
正如下面這個(gè)bean指向一個(gè)沒有使用 @Aspect 注解的bean類,
但是這個(gè)類會(huì)在下面被配置為一個(gè)切面的backing bean(支持bean)。
–>
<bean id=”aBean” class=”com.xyz.myapp.AspectExample2″>
…
</bean>
<!–
配置文件中:
所有的AOP配置是在<aop:config>標(biāo)簽中設(shè)置的,所有的切面和通知器都必須定義在 <aop:config> 元素內(nèi)部。
一個(gè)application context可以包含多個(gè) <aop:config>。
一個(gè) <aop:config> 可以包含pointcut,advisor和aspect元素(注意它們必須按照這樣的順序進(jìn)行聲明)。
如果想強(qiáng)制使用CGLIB代理,需要將 <aop:config> 的 proxy-target-class 屬性設(shè)為true
–>
<aop:config>
<!–頂級(jí)(<aop:config>)切入點(diǎn):
直接在<aop:config>下定義,這樣就可以使多個(gè)切面和通知器共享該切入點(diǎn)。–>
<aop:pointcut id=”businessService”
expression=”execution(* com.xyz.myapp.service.*.*(..))”/>
<!–這里使用命名式切入點(diǎn),只在JDK1.5及以上版本中支持。–>
<aop:pointcut id=”businessService”
expression=”com.xyz.myapp.SystemArchitecture.businessService()”/>
<!–切面使用<aop:aspect>來聲明,backing bean(支持bean)通過 ref 屬性來引用–>
<aop:aspect id=”myAspect” ref=”aBean”>
<!–在切面里面聲明一個(gè)切入點(diǎn):這種情況下切入點(diǎn)只在切面內(nèi)部可見。–>
<aop:pointcut id=”businessService”
expression=”execution(* com.xyz.myapp.service.*.*(..))”/>
<!–Before通知–>
<aop:before
pointcut-ref=”dataAccessOperation”
method=”doAccessCheck”/>
<!–使用內(nèi)置切入點(diǎn):將 pointcut-ref 屬性替換為 pointcut 屬性–>
<aop:before
pointcut=”execution(* com.xyz.myapp.dao.*.*(..))”
method=”doAccessCheck”/>
<!–返回后通知–>
<aop:after-returning
pointcut-ref=”dataAccessOperation”
method=”doAccessCheck”/>
<!–和@AspectJ風(fēng)格一樣,通知主體可以接收返回值。使用returning屬性來指定接收返回值的參數(shù)名–>
<aop:after-returning
pointcut-ref=”dataAccessOperation”
returning=”retVal”
method=”doAccessCheck”/>
<!–拋出異常后通知–>
<aop:after-throwing
pointcut-ref=”dataAccessOperation”
method=”doRecoveryActions”/>
<!–和@AspectJ風(fēng)格一樣,可以從通知體中獲取拋出的異常。
使用throwing屬性來指定異常的名稱,用這個(gè)名稱來獲取異常–>
<aop:after-throwing
pointcut-ref=”dataAccessOperation”
thowing=”dataAccessEx”
method=”doRecoveryActions”/>
<!–后通知–>
<aop:after
pointcut-ref=”dataAccessOperation”
method=”doReleaseLock”/>
<!–Around通知:通知方法的第一個(gè)參數(shù)的類型必須是 ProceedingJoinPoint 類型–>
<aop:around
pointcut-ref=”businessService”
method=”doBasicProfiling”/>
</aop:aspect>
</aop:config>
</beans>