spring后置通知@AfterReturning的使用
后置通知
在目標(biāo)方法執(zhí)行之后,增加的業(yè)務(wù)功能,由于目標(biāo)方法執(zhí)行之后執(zhí)行,所有可以獲取到目標(biāo)方法返回值,該注解是 returning屬性就是用于指定接收方法返回值的變量名的。
所有被注解為后置通知的方法,除了可以加入JoinPoint參數(shù)外,還可以包含一個用于接收返回值的變量,該變量最好使用Object類型的,目標(biāo)方法的返回值可以是任何類型的。
后置定義方法,方法是實現(xiàn)切面功能
方法定義要求
- public公共方法
- 方法沒有返回值 void
- 方法名稱自定義
- 方法有參數(shù),推薦使用Object,參數(shù)名自定義,用于接收目標(biāo)方法的返回值
屬性
- value 切入點表達式
- returning 自定義的變量,表示目標(biāo)方法的返回值的
- 自定義變量名必須和通知方法的形參名一樣
位置:在方法定義的上面
特點:
- 1 . 在目標(biāo)方法之后執(zhí)行的
- 2. 能夠獲取到目標(biāo)方法的返回值,可以根據(jù)這個返回值做不同的處理操作,可以修改這個返回值
- 3. 可以修改這個返回值
接口類
public interface Someservice {
String doOther(String name);
stdent doOther2(String anme,int age);
}
接口實現(xiàn)類
@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ù)功能類
@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"這個的變量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>
測試類
@Test
public void test02(){
String config="ApplicationContesxt1.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
//從容器獲取目標(biāo)對象
cn.com.Ycy.spring_aspectJ.bao02.Someservice someservice = (cn.com.Ycy.spring_aspectJ.bao02.Someservice) ac.getBean("SomeserviceImpl");
//通過代理對象執(zhí)行方法,實現(xiàn)目標(biāo)方法執(zhí)行時,增強了功能
String str = someservice.doOther("ycy");
System.out.println(str);
}
//返回一個對象,是否發(fā)生改變
@Test
public void test03(){
String config="ApplicationContesxt1.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
//從容器獲取目標(biāo)對象
cn.com.Ycy.spring_aspectJ.bao02.Someservice someservice = (cn.com.Ycy.spring_aspectJ.bao02.Someservice) ac.getBean("SomeserviceImpl");
//通過代理對象執(zhí)行方法,實現(xiàn)目標(biāo)方法執(zhí)行時,增強了功能
stdent str = someservice.doOther2("ycy",24);
System.out.println(str);
//someService
}
注意:
在后置通知中也是可以使用JoinPoint的,并且這個必須放在第一個位置
@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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot 如何通過SpringTemplateEngine渲染html
通過Spring的Thymeleaf模板引擎可以實現(xiàn)將模板渲染為HTML字符串,而不是直接輸出到瀏覽器,這樣可以對渲染后的字符串進行其他操作,如保存到文件或進一步處理,感興趣的朋友跟隨小編一起看看吧2024-10-10
spring task @Scheduled注解各參數(shù)的用法
這篇文章主要介紹了spring task @Scheduled注解各參數(shù)的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
淺析IDEA如何正確配置Gradle? GRADLE_USER_HOME 和 Gradle user home的區(qū)別
這篇文章主要介紹了IDEA如何正確配置Gradle? GRADLE_USER_HOME 和 Gradle user home的區(qū)別,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
Java不借助第三變量實現(xiàn)兩數(shù)交換的示例
本文主要介紹了Java不借助第三變量實現(xiàn)兩數(shù)交換的示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
解決idea刪除模塊后重新創(chuàng)建顯示該模塊已經(jīng)被注冊的問題
這篇文章主要介紹了解決idea刪除模塊后重新創(chuàng)建顯示該模塊已經(jīng)被注冊的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02

