spring后置通知@AfterReturning的使用
后置通知
在目標(biāo)方法執(zhí)行之后,增加的業(yè)務(wù)功能,由于目標(biāo)方法執(zhí)行之后執(zhí)行,所有可以獲取到目標(biāo)方法返回值,該注解是 returning屬性就是用于指定接收方法返回值的變量名的。
所有被注解為后置通知的方法,除了可以加入JoinPoint參數(shù)外,還可以包含一個(gè)用于接收返回值的變量,該變量最好使用Object類(lèi)型的,目標(biāo)方法的返回值可以是任何類(lèi)型的。
后置定義方法,方法是實(shí)現(xiàn)切面功能
方法定義要求
- public公共方法
- 方法沒(méi)有返回值 void
- 方法名稱(chēng)自定義
- 方法有參數(shù),推薦使用Object,參數(shù)名自定義,用于接收目標(biāo)方法的返回值
屬性
- value 切入點(diǎn)表達(dá)式
- returning 自定義的變量,表示目標(biāo)方法的返回值的
- 自定義變量名必須和通知方法的形參名一樣
位置:在方法定義的上面
特點(diǎn):
- 1 . 在目標(biāo)方法之后執(zhí)行的
- 2. 能夠獲取到目標(biāo)方法的返回值,可以根據(jù)這個(gè)返回值做不同的處理操作,可以修改這個(gè)返回值
- 3. 可以修改這個(gè)返回值
接口類(lèi)
public interface Someservice {
String doOther(String name);
stdent doOther2(String anme,int age);
}
接口實(shí)現(xiàn)類(lèi)
@Component("SomeserviceImpl")
public class SomeserviceImpl implements Someservice {
@Override
public String doOther(String name) {
System.out.println("------目標(biāo)方法執(zhí)行doOther()-------");
return name;
}
@Override
public stdent doOther2(String name, int age) {
System.out.println("------目標(biāo)方法執(zhí)行doOther()-------");
stdent st = new stdent();
st.setAge(age);
st.setName(name);
return st;
}
}
增加業(yè)務(wù)功能類(lèi)
@Component("myAspect2")
@Aspect
public class MyaspectJ {
@AfterReturning(value ="execution(* *..SomeserviceImpl.doOther(..))",returning = "res")
public void myaspectJ(Object res){
System.out.println("后置通知的返回值為:"+res);
res = "18204229-"+res;
System.out.println("修改之后的:"+res);
}
@AfterReturning(value ="execution(* *..SomeserviceImpl.doOther2(..))",returning = "res")
public void myaspectJ2(JoinPoint joinPoint ,Object res){
System.out.println(joinPoint.getSignature());
System.out.println(joinPoint.getSignature().getName());
Object[] args = joinPoint.getArgs();
for (Object arg : args) {
System.out.println(arg);
}
stdent stdent = new stdent();
stdent.setAge(22);
stdent.setName("44455");
res = stdent;
System.out.println("后置通知中:"+res);
}
}
returning = "res"這個(gè)的變量res要和Object res的res命名一樣
主配置文件:
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="cn.com.Ycy.spring_aspectJ.bao02"/>
<aop:aspectj-autoproxy/>
</beans>
測(cè)試類(lèi)
@Test
public void test02(){
String config="ApplicationContesxt1.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
//從容器獲取目標(biāo)對(duì)象
cn.com.Ycy.spring_aspectJ.bao02.Someservice someservice = (cn.com.Ycy.spring_aspectJ.bao02.Someservice) ac.getBean("SomeserviceImpl");
//通過(guò)代理對(duì)象執(zhí)行方法,實(shí)現(xiàn)目標(biāo)方法執(zhí)行時(shí),增強(qiáng)了功能
String str = someservice.doOther("ycy");
System.out.println(str);
}
//返回一個(gè)對(duì)象,是否發(fā)生改變
@Test
public void test03(){
String config="ApplicationContesxt1.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
//從容器獲取目標(biāo)對(duì)象
cn.com.Ycy.spring_aspectJ.bao02.Someservice someservice = (cn.com.Ycy.spring_aspectJ.bao02.Someservice) ac.getBean("SomeserviceImpl");
//通過(guò)代理對(duì)象執(zhí)行方法,實(shí)現(xiàn)目標(biāo)方法執(zhí)行時(shí),增強(qiáng)了功能
stdent str = someservice.doOther2("ycy",24);
System.out.println(str);
//someService
}
注意:
在后置通知中也是可以使用JoinPoint的,并且這個(gè)必須放在第一個(gè)位置
@Component("myAspect2")
@Aspect
public class MyaspectJ {
@AfterReturning(value ="execution(* *..SomeserviceImpl.doOther2(..))",returning = "res")
public void myaspectJ2(JoinPoint joinPoint ,Object res){
System.out.println(joinPoint.getSignature());
System.out.println(joinPoint.getSignature().getName());
Object[] args = joinPoint.getArgs();
for (Object arg : args) {
System.out.println(arg);
}
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
log4j如何根據(jù)變量動(dòng)態(tài)生成文件名
這篇文章主要介紹了log4j如何根據(jù)變量動(dòng)態(tài)生成文件名方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java Management Extensions管理擴(kuò)展原理解析
這篇文章主要介紹了Java Management Extensions管理擴(kuò)展原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
java?JIT調(diào)優(yōu)的實(shí)現(xiàn)
JIT編譯器調(diào)優(yōu)方法包括啟用JIT日志、優(yōu)化熱點(diǎn)代碼、循環(huán)展開(kāi)、內(nèi)聯(lián)優(yōu)化、逃逸分析以及使用性能分析工具等,本文主要介紹了java?JIT調(diào)優(yōu)的實(shí)現(xiàn),感興趣的可以了解一下2025-02-02
JPA中@JoinColumn的name和referencedColumnName屬性的區(qū)別及說(shuō)明
這篇文章主要介紹了JPA中@JoinColumn的name和referencedColumnName屬性的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
SpringBoot 配置文件中配置的中文,程序讀取出來(lái)是亂碼的解決
這篇文章主要介紹了SpringBoot 配置文件中配置的中文,程序讀取出來(lái)是亂碼的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
mybatis plus 自動(dòng)轉(zhuǎn)駝峰配置小結(jié)
SpringBoot提供兩種配置Mybatis的方式,第一種是通過(guò)yml或application.properties文件開(kāi)啟配置,第二種是使用自定義配置類(lèi),通過(guò)給容器添加一個(gè)ConfigurationCustomizer來(lái)實(shí)現(xiàn)更靈活的配置,這兩種方法可以根據(jù)項(xiàng)目需求和個(gè)人喜好選擇使用2024-10-10
Spring 使用Validation 驗(yàn)證框架的問(wèn)題詳解
Spring Boot在內(nèi)部通過(guò)集成hibernate-validation已經(jīng)實(shí)現(xiàn)了JSR-349驗(yàn)證規(guī)范接口,在Spring Boot項(xiàng)目中只要直接使用就行了。 一般用在Controller中用于驗(yàn)證前端傳來(lái)的參數(shù)。這篇文章給大家介紹Spring Validation 驗(yàn)證框架的相關(guān)知識(shí),感興趣的朋友一起看看吧2021-07-07

