日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring22:Aspectj实现环绕通知@Around

發布時間:2025/6/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring22:Aspectj实现环绕通知@Around 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

切面類:

package com.atChina.Test3;import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before;/*** @Aspect:來自aspectj框架,表示當前類是切面類* 切面類是用來給業務方法增強功能的類*/ @Aspect public class MyAspect {/*** @Around:環繞通知,在目標方法前和后都能增強功能* 屬性: value,表示切入點表達式(切面功能加入的位置) * 位置:在方法的上面* * 后置通知的特點:* 1.在目標方法前和后都能增強功能* 2.能夠修改目標方法的執行結果* 3.能夠控制目標方法是否執行* * 參數 ProceedingJoinPoint 繼承自org.aspectj.lang.JoinPoint,表示切入點* * 返回值: Object,表示目標方法的執行結果*/@Around(value="execution(* *..SomeServiceImpl.doAround(..))")public Object myAround(ProceedingJoinPoint pjp) throws Throwable{Object result=null;System.out.println("環繞通知,在目標方法之前...增強功能");result = pjp.proceed(); // 執行目標方法, 注釋該語句后,目標方法就不會被執行,從而控制了目標方法的執行if(null != result){// 修改目標方法的執行結果result = ((String)result).toUpperCase();}System.out.println("環繞通知,在目標方法之后...增強功能");return result;} }

普通bean

package com.atChina.Test3;public interface SomeService {public void doSome();public String doOther(String params);public Student getStudent();public String doAround(); }package com.atChina.Test3;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;}@Overridepublic String doAround() {System.out.println("執行了doAround業務方法...");return "doAround";} }package com.atChina.Test3;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.Test3.SomeServiceImpl" /><!-- 聲明切面類對象 --><bean id="myAspect" class="com.atChina.Test3.MyAspect" /><!-- 聲明自動代理生成器,創建代理對象 --><aop:aspectj-autoproxy /> <!-- 尋找aspectj框架能夠識別的標簽 --> </beans>

?測試類以及測試結果:

@Testpublic void test1(){String configLocation = "com/atChina/Test3/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.doAround();System.out.println("返回值result:"+result);}測試結果: com.sun.proxy.$Proxy8 =================================== 環繞通知,在目標方法之前...增強功能 執行了doAround業務方法... 環繞通知,在目標方法之后...增強功能 返回值result:DOAROUND

?

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的spring22:Aspectj实现环绕通知@Around的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。