Spring中基于xml的AOP實現(xiàn)詳解
基于xml的AOP實現(xiàn)
基于xml與基于注解的AOP本質(zhì)上是非常相似的,都是需要封裝橫切關(guān)注點,封裝到切面中,然后把橫切關(guān)注點封裝為一個方法,再把該方法設(shè)置為當(dāng)前的一個通知,再通過切入點表達(dá)式定位到橫切點就可以了,思路是非常相似的。
我們的接口代碼如下所示:
package com.rgf.spring.aop.annotation.xml; import org.springframework.stereotype.Component; @Component public interface Calculator { int add(int i,int j); int sub(int i,int j); int mul(int i,int j); int div(int i,int j); }
我們的實現(xiàn)類如下所示:
package com.rgf.spring.aop.annotation.xml; import org.springframework.stereotype.Component; /** * */ @Component public class CalculatorImpl implements Calculator { @Override public int add(int i, int j) { int result=i+j; System.out.println("方法內(nèi)部,result:"+result); return result; } @Override public int sub(int i, int j) { int result=i-j; System.out.println("方法內(nèi)部,result:"+result); return result; } @Override public int mul(int i, int j) { int result=i*j; System.out.println("方法內(nèi)部,result:"+result); return result; } @Override public int div(int i, int j) { int result=i/j; System.out.println("方法內(nèi)部,result:"+result); return result; } }
我們封裝的切面方法如下所示:
package com.rgf.spring.aop.annotation.xml; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; import java.util.Arrays; @Component public class LoggerAspect { public void beforeAdviceMethod(JoinPoint joinPoint){ Signature signature = joinPoint.getSignature(); Object[] args = joinPoint.getArgs(); System.out.println("LoggerAspect,方法:"+signature.getName()+",參數(shù):"+ Arrays.toString(args)); } public void afterAdviceMethod(JoinPoint joinPoint){ Signature signature = joinPoint.getSignature(); Object[] args = joinPoint.getArgs(); System.out.println("LoggerAspect,方法:"+signature.getName()+",執(zhí)行完畢"); } public void afterReturningAdviceMethod(JoinPoint joinPoint,Object result){ Signature signature = joinPoint.getSignature(); Object[] args = joinPoint.getArgs(); System.out.println("LoggerAspect,方法:"+signature.getName()+",結(jié)果:"+result); } public void afterThrowingAdviceMethod(JoinPoint joinPoint,Throwable ex){ Signature signature = joinPoint.getSignature(); System.out.println("LoggerAspect,方法:"+signature.getName()+",異常:"+ex); } public Object aroundAdviceMethod(ProceedingJoinPoint joinPoint){ Object result=null; try { System.out.println("環(huán)繞通知-->前置通知"); //表示目標(biāo)對象方法的執(zhí)行 result = joinPoint.proceed(); System.out.println("環(huán)繞通知-->返回通知"); } catch (Throwable throwable) { throwable.printStackTrace(); System.out.println("環(huán)繞通知-->異常通知"); }finally { System.out.println("環(huán)繞通知-->后置通知"); } return result; } }
我們封裝的切面如下所示:
package com.rgf.spring.aop.annotation.xml; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; /** * 切面的優(yōu)先級: * 可以通過@Order注解的value屬性設(shè)置優(yōu)先級,默認(rèn)值Integer的最大值 * @Order注解的value屬性值越小,優(yōu)先級越高 * */ @Component public class ValidateAspect { public void beforeMethod(){ System.out.println("ValidateAspect-->前置通知"); } }
我們的配置文件如下:
<?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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--掃描組件--> <context:component-scan base-package="com.rgf.spring.aop.annotation.xml"></context:component-scan> <!-- <cop:aspectj-autoproxy:開啟基于注解的aop--> <aop:config> <!--設(shè)置一個公共的切入點表達(dá)式--> <aop:pointcut id="pointcut" expression="execution(* com.rgf.spring.aop.annotation.xml.CalculatorImpl.*(..))"/> <!--aop:aspect:將IOC容器中的某個bean(某個組件)設(shè)置為切面,通過ref引用某一個besn的id,就可以將當(dāng)前這一個bean來設(shè)置為一個切面--> <!--aop:advisor:設(shè)置當(dāng)前的通知--> <!--aop:pointcut:設(shè)置切入點表達(dá)式--> <aop:aspect ref="loggerAspect"> <aop:before method="beforeAdviceMethod" pointcut-ref="pointcut"></aop:before> <aop:after method="afterAdviceMethod" pointcut-ref="pointcut"></aop:after> <aop:after-returning method="afterReturningAdviceMethod" returning="result" pointcut-ref="pointcut"></aop:after-returning> <aop:after-throwing method="afterThrowingAdviceMethod" throwing="ex" pointcut-ref="pointcut"></aop:after-throwing> <aop:around method="aroundAdviceMethod" pointcut-ref="pointcut"></aop:around> </aop:aspect> <!--order進(jìn)行設(shè)置他的優(yōu)先級.設(shè)置的值越小,優(yōu)先級越高--> <aop:aspect ref="validateAspect" order="1"> <aop:before method="beforeMethod" pointcut-ref="pointcut"></aop:before> </aop:aspect> </aop:config> </beans>
我們進(jìn)行測試如下所示:
package com.rgf.spring.test; import com.rgf.spring.aop.annotation.xml.Calculator; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AOPByXMLTest { @Test public void testAOPByXML(){ ApplicationContext ioc = new ClassPathXmlApplicationContext("aop-xml.xml"); Calculator calculator = ioc.getBean(Calculator.class); calculator.add(1,1); } }
我們進(jìn)行運(yùn)行之后如下所示:
到此這篇關(guān)于Spring中基于xml的AOP實現(xiàn)詳解的文章就介紹到這了,更多相關(guān)基于xml的AOP實現(xiàn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java用數(shù)組實現(xiàn)循環(huán)隊列的示例
下面小編就為大家?guī)硪黄狫ava用數(shù)組實現(xiàn)循環(huán)隊列的示例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09Spring-Boot 集成Solr客戶端的詳細(xì)步驟
本篇文章主要介紹了Spring-Boot 集成Solr客戶端的詳細(xì)步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11解決spring mvc 多數(shù)據(jù)源切換,不支持事務(wù)控制的問題
下面小編就為大家?guī)硪黄鉀Qspring mvc 多數(shù)據(jù)源切換,不支持事務(wù)控制的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09基于SpringBoot生成二維碼的幾種實現(xiàn)方式
本文將基于Spring Boot介紹兩種生成二維碼的實現(xiàn)方式,一種是基于Google開發(fā)工具包,另一種是基于Hutool來實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2022-03-03SpringSecurity 自定義認(rèn)證登錄的項目實踐
本文主要介紹了SpringSecurity 自定義認(rèn)證登錄的項目實踐,以手機(jī)驗證碼登錄為例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08SpringBoot配置MyBatis-Plus實現(xiàn)增刪查改
本文主要介紹了SpringBoot配置MyBatis-Plus實現(xiàn)增刪查改,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08MyBatisPlus3如何向數(shù)據(jù)庫中存入List
本文主要介紹了Mybatis Plus的類型處理器的使用,通過User.java和UserMapper.xml示例進(jìn)行詳細(xì)的解析,并提供了JSON解析器的使用方法,希望通過這篇文章,可以幫助大家更好的理解和掌握Mybatis Plus的類型處理器2024-10-10