spring后置通知@AfterReturning的使用
后置通知
在目標方法執(zhí)行之后,增加的業(yè)務(wù)功能,由于目標方法執(zhí)行之后執(zhí)行,所有可以獲取到目標方法返回值,該注解是 returning屬性就是用于指定接收方法返回值的變量名的。
所有被注解為后置通知的方法,除了可以加入JoinPoint參數(shù)外,還可以包含一個用于接收返回值的變量,該變量最好使用Object類型的,目標方法的返回值可以是任何類型的。
后置定義方法,方法是實現(xiàn)切面功能
方法定義要求
- public公共方法
- 方法沒有返回值 void
- 方法名稱自定義
- 方法有參數(shù),推薦使用Object,參數(shù)名自定義,用于接收目標方法的返回值
屬性
- value 切入點表達式
- returning 自定義的變量,表示目標方法的返回值的
- 自定義變量名必須和通知方法的形參名一樣
位置:在方法定義的上面
特點:
- 1 . 在目標方法之后執(zhí)行的
- 2. 能夠獲取到目標方法的返回值,可以根據(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("------目標方法執(zhí)行doOther()-------"); return name; } @Override public stdent doOther2(String name, int age) { System.out.println("------目標方法執(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); //從容器獲取目標對象 cn.com.Ycy.spring_aspectJ.bao02.Someservice someservice = (cn.com.Ycy.spring_aspectJ.bao02.Someservice) ac.getBean("SomeserviceImpl"); //通過代理對象執(zhí)行方法,實現(xiàn)目標方法執(zhí)行時,增強了功能 String str = someservice.doOther("ycy"); System.out.println(str); } //返回一個對象,是否發(fā)生改變 @Test public void test03(){ String config="ApplicationContesxt1.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(config); //從容器獲取目標對象 cn.com.Ycy.spring_aspectJ.bao02.Someservice someservice = (cn.com.Ycy.spring_aspectJ.bao02.Someservice) ac.getBean("SomeserviceImpl"); //通過代理對象執(zhí)行方法,實現(xiàn)目標方法執(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)文章
Java Management Extensions管理擴展原理解析
這篇文章主要介紹了Java Management Extensions管理擴展原理解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04java?JIT調(diào)優(yōu)的實現(xiàn)
JIT編譯器調(diào)優(yōu)方法包括啟用JIT日志、優(yōu)化熱點代碼、循環(huán)展開、內(nèi)聯(lián)優(yōu)化、逃逸分析以及使用性能分析工具等,本文主要介紹了java?JIT調(diào)優(yōu)的實現(xiàn),感興趣的可以了解一下2025-02-02JPA中@JoinColumn的name和referencedColumnName屬性的區(qū)別及說明
這篇文章主要介紹了JPA中@JoinColumn的name和referencedColumnName屬性的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05SpringBoot 配置文件中配置的中文,程序讀取出來是亂碼的解決
這篇文章主要介紹了SpringBoot 配置文件中配置的中文,程序讀取出來是亂碼的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09mybatis plus 自動轉(zhuǎn)駝峰配置小結(jié)
SpringBoot提供兩種配置Mybatis的方式,第一種是通過yml或application.properties文件開啟配置,第二種是使用自定義配置類,通過給容器添加一個ConfigurationCustomizer來實現(xiàn)更靈活的配置,這兩種方法可以根據(jù)項目需求和個人喜好選擇使用2024-10-10