Spring_AOP_XML使用Aspect實現動態代理(常用),springaopaspect

來源:互聯網
上載者:User

Spring_AOP_XML使用Aspect實現動態代理(常用),springaopaspect

Spring_AOP_XML使用Aspect實現動態代理(常用)

XML使用Aspect實現動態代理此方式比較常用,和使用註解最大的好處是我們不用每個方法前面定義橫切點上面加入PointCut的說明,在XML中只需要定義一次就可以多出使用。

在上面Spring_AOP_Annotation使用Aspect實現動態代理的基礎上修改,去除LogAspect中方法上的註解。

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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"><!-- 開啟Spring的Annotation的支援 --><context:annotation-config /><!-- 設定Spring去哪些包中找Annotation --><context:component-scan base-package="com.spring" /><!-- 開啟基於Annotation的aop自動代理 --><aop:aspectj-autoproxy /><aop:config><!-- 定義切面 --><aop:aspect id="myLogAspect" ref="logAspect"><!-- 在哪些位置加入相應的Aspect --><aop:pointcut id="logPointCut"expression="execution(* com.spring.dao.*.add*(..))||execution(* com.spring.dao.*.update*(..))||execution(* com.spring.dao.*.delete*(..))" /><!-- 開始之前加入日誌 --><aop:before method="logStart" pointcut-ref="logPointCut" /><!-- 結束之後加入日誌 --><aop:after method="logEnd" pointcut-ref="logPointCut" /><!-- 執行過程中加入日誌 --><aop:around method="logAround" pointcut-ref="logPointCut"/></aop:aspect></aop:config></beans>

LogAspect類:
package com.spring.proxy;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.stereotype.Component;//讓這個切面類被Spring所管理@Component("logAspect")// 聲明這是一個切面類@Aspectpublic class LogAspect {public void logStart(JoinPoint jp) {// 得到執行的對象System.out.println(jp.getTarget());// 得到執行的方法System.out.println(jp.getSignature().getName());Logger.info("方法執行前加入日誌,來自LogAspect");}public void logEnd(JoinPoint jp) {// 得到執行的對象System.out.println(jp.getTarget());// 得到執行的方法System.out.println(jp.getSignature().getName());Logger.info("方法執行後加入日誌,來自LogAspect");}public void logAround(ProceedingJoinPoint pjp) throws Throwable {Logger.info("開始在Around中加入日誌,來自LogAspect");//執行程式pjp.proceed();Logger.info("結束Around");}}

測試代碼及測試結果:





spring已經實現了aop,為何還要用aspectj

spring AOP就是用aspectj來實現的,是依賴關係!AspectJ是動態代理的一種實現!而spring預設使用的就是AspectJ來實現的動態代理,spring自己的AOP就是使用AspectJ來實現的!當然你也可以使用其他的實現,如cglib!
 
實現Spring AOP 應用的幾種方式

spring開發aop應用有三種方法:一:Spring 1.2版本中通過ProxyFactoryBean來實現aop,即通過動態代理來實現的,Aspect必須繼承MethodBeforeAdvice,MethodAfterAdvice等<!--被代理的對象--><bean id="man" class="Man">
<property name="name">
<value type="java.lang.String">張三</value>
</property>
</bean><!--繼承了MethodBeforeAdvice類的 Aspect->
<bean id="fbi" class="FBI" />
<bean id="civilian"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref bean="man" />
</property><property name="interceptorNames">
<list>
<value>fbi</value>
</list>
</property>
</bean>二:Spring 2.0 AOP 應用需要改的是FBI 這個類,而且它也不需要再實現某些介面了public class FBI {
public void before(JoinPoint point){
Man man = (Man)point.getTarget();
System.err.println("FBI 發現" + man.getName() + "進行中 " +
point.getSignature().getName() + " 活動。");
}
}注意這個類裡面的方法 before(JoinPoint),方法名可以是任意的,可以帶一個JoinPoint 類
型的參數,也可以不帶參數直接寫成before(),但是這個連接點(JoinPoint)對象帶來了所
有和這次方法調用有關的資訊,包括方法參數,目標對象等等,所以一般要做日誌記錄的話
會帶上它。
接下來是測試類別的代碼,和以前的幾乎沒有任何不同,只不過現在直接存取的是man
這個bean。<beans xmlns=" www.springframework.org/schema/beans"
xmlns:xsi=" ......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.