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