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è)通知,再通過切入點(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)類如下所示:
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í):
* 可以通過@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:開啟基于注解的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è)置為切面,通過ref引用某一個(gè)besn的id,就可以將當(dāng)前這一個(gè)bean來設(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)文章
SpringBoot配置Druid數(shù)據(jù)監(jiān)控代碼實(shí)例
這篇文章主要介紹了SpringBoot配置Druid數(shù)據(jù)監(jiān)控代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
SpringBoot動(dòng)態(tài)Feign服務(wù)調(diào)用詳解
Feign是Netflix公司開發(fā)的一個(gè)聲明式的REST調(diào)用客戶端; Ribbon負(fù)載均衡、 Hystrⅸ服務(wù)熔斷是我們Spring Cloud中進(jìn)行微服務(wù)開發(fā)非?;A(chǔ)的組件,在使用的過程中我們也發(fā)現(xiàn)它們一般都是同時(shí)出現(xiàn)的,而且配置也都非常相似2022-12-12
解決MyBatis-Plus使用動(dòng)態(tài)表名selectPage不生效的問題
這篇文章主要介紹了如惡化解決MyBatis-Plus使用動(dòng)態(tài)表名selectPage不生效的問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
spring-boot-maven-plugin:unknown的完美解決方法
這篇文章主要介紹了spring-boot-maven-plugin:unknown的完美解決方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
Java?KeyGenerator.generateKey的19個(gè)方法代碼示例
在下文中一共展示了KeyGenerator.generateKey方法的19個(gè)代碼示例,這些例子默認(rèn)根據(jù)受歡迎程度排序2021-12-12
java 操作gis geometry類型數(shù)據(jù)方式
這篇文章主要介紹了java 操作gis geometry類型數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

