spring21:Aspectj实现后置通知@AfterReturning
生活随笔
收集整理的這篇文章主要介紹了
spring21:Aspectj实现后置通知@AfterReturning
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?切面類:
package com.atChina.Test2;import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before;/*** @Aspect:來自aspectj框架,表示當前類是切面類* 切面類是用來給業務方法增強功能的類*/ @Aspect public class MyAspect {/*** @AfterReturning:后置通知,在目標方法之后調用* 屬性:1. value,表示切入點表達式(切面功能加入的位置)* 2. returning,自定義變量名,表示目標方法的返回值.自定義的變量名需要和通知方法的參數名一樣* 位置:在方法的上面* * 后置通知的特點:* 1.在目標方法之后執行* 2.能夠獲取目標方法的執行結果,并且還可以對執行結果進行修改* 1).目標方法返回值是簡單類型(String和java基本數據類型),在通知方法中修改返回值不會影響目標方法的最終結果* 2).目標方法返回值是非簡單類型,在通知方法中修改返回值的屬性值,這樣就會影響目標方法的最終結果* 3.不會影響目標方法的執行* *//** Object result = Student getStudent() 返回值是字符串* afterStudent(result); // 參數傳遞的是值* * 后置通知方法中也可以有JoinPoint參數,如果通知方法中有多個參數,JoinPoint一定是第一個參數*/@AfterReturning(value="execution(* *..SomeServiceImpl.do*(..))", returning="result")public void afterFunc(JoinPoint jp, Object result){System.out.println("獲取切入點的方法名稱:"+jp.getSignature().getName());if(result instanceof String){result = ((String)result).toUpperCase();}System.out.println("目標方法的返回值result:"+result);System.out.println("執行了后置通知......");}/** Object result = Student getStudent() 返回值是對象* afterStudent(result); // 參數傳遞的是引用*/@AfterReturning(value="execution(* *..SomeServiceImpl.get*(..))", returning="result")public void afterStudent(Object result){if(result != null){Student st = (Student)result;st.setAge(21);st.setName("八戒");}System.out.println("執行了后置通知......");} }?普通bean
package com.atChina.Test2;public interface SomeService {public void doSome();public String doOther(String params);public Student getStudent(); }package com.atChina.Test2;public class SomeServiceImpl implements SomeService {@Overridepublic void doSome() {System.out.println("執行了doSome業務方法...");}@Overridepublic String doOther(String params) {// TODO Auto-generated method stubSystem.out.println("執行了doSome業務方法..."+params);return params;}@Overridepublic Student getStudent() {Student st = new Student();st.setAge(22);st.setName("孫悟空");return st;}}package com.atChina.Test2;public class Student {private String name;private int age;public void setName(String name) {this.name = name;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + "]";} }配置bean以及聲明自定代理生成器
<?xml version="1.0" encoding="UTF-8"?> <!-- 引用Spring的多個Schema空間的格式定義文件 --> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd "><!-- 聲明目標類對象 --><bean id="someService" class="com.atChina.Test2.SomeServiceImpl" /><!-- 聲明切面類對象 --><bean id="myAspect" class="com.atChina.Test2.MyAspect" /><!-- 聲明自動代理生成器,創建代理對象 --><aop:aspectj-autoproxy /> <!-- 尋找aspectj框架能夠識別的標簽 --> </beans>?測試類以及測試結果:
@Testpublic void test1(){String configLocation = "com/atChina/Test2/applicationContext.xml"; // 類路徑的根目錄ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);// 目標對象有接口,aspectj默認使用的是jdk動態代理SomeService proxy = (SomeService) ctx.getBean("someService");System.out.println(proxy.getClass().getName());// proxy.doSome();System.out.println("===================================");String result = proxy.doOther("taimxai");System.out.println("返回值result:"+result);System.out.println("===================================");Student st = proxy.getStudent();System.out.println("返回值result:"+st);}測試結果: com.sun.proxy.$Proxy8 =================================== 執行了doSome業務方法...taimxai 獲取切入點的方法名稱:doOther 目標方法的返回值result:TAIMXAI 執行了后置通知...... 返回值result:taimxai =================================== 執行了后置通知...... 返回值result:Student [name=八戒, age=21]?
總結
以上是生活随笔為你收集整理的spring21:Aspectj实现后置通知@AfterReturning的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybaits二:通过接口类,查询数据
- 下一篇: spring22:Aspectj实现环绕