Spring中基于xml的AOP實(shí)現(xiàn)詳解
基于xml的AOP實(shí)現(xiàn)
基于xml與基于注解的AOP本質(zhì)上是非常相似的,都是需要封裝橫切關(guān)注點(diǎn),封裝到切面中,然后把橫切關(guān)注點(diǎn)封裝為一個(gè)方法,再把該方法設(shè)置為當(dāng)前的一個(gè)通知,再通過(guò)切入點(diǎn)表達(dá)式定位到橫切點(diǎn)就可以了,思路是非常相似的。
我們的接口代碼如下所示:
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);
}我們的實(shí)現(xiàn)類(lèi)如下所示:
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)對(duì)象方法的執(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)先級(jí):
* 可以通過(guò)@Order注解的value屬性設(shè)置優(yōu)先級(jí),默認(rèn)值Integer的最大值
* @Order注解的value屬性值越小,優(yōu)先級(jí)越高
*
*/
@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:開(kāi)啟基于注解的aop-->
<aop:config>
<!--設(shè)置一個(gè)公共的切入點(diǎn)表達(dá)式-->
<aop:pointcut id="pointcut" expression="execution(* com.rgf.spring.aop.annotation.xml.CalculatorImpl.*(..))"/>
<!--aop:aspect:將IOC容器中的某個(gè)bean(某個(gè)組件)設(shè)置為切面,通過(guò)ref引用某一個(gè)besn的id,就可以將當(dāng)前這一個(gè)bean來(lái)設(shè)置為一個(gè)切面-->
<!--aop:advisor:設(shè)置當(dāng)前的通知-->
<!--aop:pointcut:設(shè)置切入點(diǎn)表達(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)先級(jí).設(shè)置的值越小,優(yōu)先級(jí)越高-->
<aop:aspect ref="validateAspect" order="1">
<aop:before method="beforeMethod" pointcut-ref="pointcut"></aop:before>
</aop:aspect>
</aop:config>
</beans>我們進(jìn)行測(cè)試如下所示:
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實(shí)現(xiàn)詳解的文章就介紹到這了,更多相關(guān)基于xml的AOP實(shí)現(xiàn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java用數(shù)組實(shí)現(xiàn)循環(huán)隊(duì)列的示例
下面小編就為大家?guī)?lái)一篇Java用數(shù)組實(shí)現(xiàn)循環(huán)隊(duì)列的示例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
Spring-Boot 集成Solr客戶(hù)端的詳細(xì)步驟
本篇文章主要介紹了Spring-Boot 集成Solr客戶(hù)端的詳細(xì)步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
解決spring mvc 多數(shù)據(jù)源切換,不支持事務(wù)控制的問(wèn)題
下面小編就為大家?guī)?lái)一篇解決spring mvc 多數(shù)據(jù)源切換,不支持事務(wù)控制的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
基于SpringBoot生成二維碼的幾種實(shí)現(xiàn)方式
本文將基于Spring Boot介紹兩種生成二維碼的實(shí)現(xiàn)方式,一種是基于Google開(kāi)發(fā)工具包,另一種是基于Hutool來(lái)實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2022-03-03
SpringSecurity 自定義認(rèn)證登錄的項(xiàng)目實(shí)踐
本文主要介紹了SpringSecurity 自定義認(rèn)證登錄的項(xiàng)目實(shí)踐,以手機(jī)驗(yàn)證碼登錄為例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08
SpringBoot配置MyBatis-Plus實(shí)現(xiàn)增刪查改
本文主要介紹了SpringBoot配置MyBatis-Plus實(shí)現(xiàn)增刪查改,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
MyBatisPlus3如何向數(shù)據(jù)庫(kù)中存入List
本文主要介紹了Mybatis Plus的類(lèi)型處理器的使用,通過(guò)User.java和UserMapper.xml示例進(jìn)行詳細(xì)的解析,并提供了JSON解析器的使用方法,希望通過(guò)這篇文章,可以幫助大家更好的理解和掌握Mybatis Plus的類(lèi)型處理器2024-10-10

