溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringAOP中怎么訪問目標方法的參數

發布時間:2021-07-22 15:54:04 來源:億速云 閱讀:141 作者:Leah 欄目:編程語言

這篇文章給大家介紹SpringAOP中怎么訪問目標方法的參數,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

一 配置

<?xml version="1.0" encoding="GBK"?><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-4.0.xsd   http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-4.0.xsd   http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">   <!-- 指定自動搜索Bean組件、自動搜索切面類 -->   <context:component-scan      base-package="org.crazyit.app.service      ,org.crazyit.app.aspect">      <context:include-filter type="annotation"        expression="org.aspectj.lang.annotation.Aspect" />   </context:component-scan>   <!-- 啟動@AspectJ支持 -->   <aop:aspectj-autoproxy /></beans>

二 切面類

package org.crazyit.app.aspect;import org.aspectj.lang.annotation.*;import org.aspectj.lang.*;import java.util.Arrays;// 定義一個切面@Aspectpublic class FourAdviceTest{  // 定義Around增強處理  @Around("execution(* org.crazyit.app.service.impl.*.*(..))")  public Object processTx(ProceedingJoinPoint jp)    throws java.lang.Throwable  {    System.out.println("Around增強:執行目標方法之前,模擬開始事務...");    // 訪問執行目標方法的參數    Object[] args = jp.getArgs();    // 當執行目標方法的參數存在,    // 且第一個參數是字符串參數    if (args != null && args.length > 0      && args[0].getClass() == String.class)    {      // 修改目標方法調用參數的第一個參數      args[0] = "【增加的前綴】" + args[0];    }    //執行目標方法,并保存目標方法執行后的返回值    Object rvt = jp.proceed(args);    System.out.println("Around增強:執行目標方法之后,模擬結束事務...");    // 如果rvt的類型是Integer,將rvt改為它的平方    if(rvt != null && rvt instanceof Integer)      rvt = (Integer)rvt * (Integer)rvt;    return rvt;  }  // 定義Before增強處理  @Before("execution(* org.crazyit.app.service.impl.*.*(..))")  public void authority(JoinPoint jp)  {    System.out.println("Before增強:模擬執行權限檢查");    // 返回被織入增強處理的目標方法    System.out.println("Before增強:被織入增強處理的目標方法為:"      + jp.getSignature().getName());    // 訪問執行目標方法的參數    System.out.println("Before增強:目標方法的參數為:"      + Arrays.toString(jp.getArgs()));    // 訪問被增強處理的目標對象    System.out.println("Before增強:被織入增強處理的目標對象為:"      + jp.getTarget());  }  //定義AfterReturning增強處理  @AfterReturning(pointcut="execution(* org.crazyit.app.service.impl.*.*(..))"    , returning="rvt")  public void log(JoinPoint jp , Object rvt)  {    System.out.println("AfterReturning增強:獲取目標方法返回值:"      + rvt);    System.out.println("AfterReturning增強:模擬記錄日志功能...");    // 返回被織入增強處理的目標方法    System.out.println("AfterReturning增強:被織入增強處理的目標方法為:"      + jp.getSignature().getName());    // 訪問執行目標方法的參數    System.out.println("AfterReturning增強:目標方法的參數為:"      + Arrays.toString(jp.getArgs()));    // 訪問被增強處理的目標對象    System.out.println("AfterReturning增強:被織入增強處理的目標對象為:"      + jp.getTarget());  }  // 定義After增強處理  @After("execution(* org.crazyit.app.service.impl.*.*(..))")  public void release(JoinPoint jp)  {    System.out.println("After增強:模擬方法結束后的釋放資源...");    // 返回被織入增強處理的目標方法    System.out.println("After增強:被織入增強處理的目標方法為:"      + jp.getSignature().getName());    // 訪問執行目標方法的參數    System.out.println("After增強:目標方法的參數為:"      + Arrays.toString(jp.getArgs()));    // 訪問被增強處理的目標對象    System.out.println("After增強:被織入增強處理的目標對象為:"      + jp.getTarget());  }}

三 接口

Hello

package org.crazyit.app.service;public interface Hello {   // 定義一個簡單方法,模擬應用中的業務邏輯方法   void foo();   // 定義一個addUser()方法,模擬應用中的添加用戶的方法   int addUser(String name, String pass);}

World

package org.crazyit.app.service;public interface World {   // 定義一個簡單方法,模擬應用中的業務邏輯方法   public void bar();}

四 實現類

HelloImpl

package org.crazyit.app.service.impl;import org.springframework.stereotype.Component;import org.crazyit.app.service.*;@Component("hello")public class HelloImpl implements Hello {  // 定義一個簡單方法,模擬應用中的業務邏輯方法  public void foo() {    System.out.println("執行Hello組件的foo()方法");  }  // 定義一個addUser()方法,模擬應用中的添加用戶的方法  public int addUser(String name, String pass) {    System.out.println("執行Hello組件的addUser添加用戶:" + name);    return 20;  }}

WorldImpl

package org.crazyit.app.service.impl;import org.springframework.stereotype.Component;import org.crazyit.app.service.*;@Component("world")public class WorldImpl implements World {  // 定義一個簡單方法,模擬應用中的業務邏輯方法  public void bar() {    System.out.println("執行World組件的bar()方法");  }}

五 測試類

package lee;import org.springframework.context.*;import org.springframework.context.support.*;import org.crazyit.app.service.*;public class BeanTest {  public static void main(String[] args) {    // 創建Spring容器    ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");    Hello hello = ctx.getBean("hello", Hello.class);    hello.foo();    hello.addUser("孫悟空", "7788");    World world = ctx.getBean("world", World.class);    world.bar();  }}

六 測試結果

Around增強:執行目標方法之前,模擬開始事務...Before增強:模擬執行權限檢查Before增強:被織入增強處理的目標方法為:fooBefore增強:目標方法的參數為:[]Before增強:被織入增強處理的目標對象為:org.crazyit.app.service.impl.HelloImpl@694abbdc執行Hello組件的foo()方法Around增強:執行目標方法之后,模擬結束事務...After增強:模擬方法結束后的釋放資源...After增強:被織入增強處理的目標方法為:fooAfter增強:目標方法的參數為:[]After增強:被織入增強處理的目標對象為:org.crazyit.app.service.impl.HelloImpl@694abbdcAfterReturning增強:獲取目標方法返回值:nullAfterReturning增強:模擬記錄日志功能...AfterReturning增強:被織入增強處理的目標方法為:fooAfterReturning增強:目標方法的參數為:[]AfterReturning增強:被織入增強處理的目標對象為:org.crazyit.app.service.impl.HelloImpl@694abbdcAround增強:執行目標方法之前,模擬開始事務...Before增強:模擬執行權限檢查Before增強:被織入增強處理的目標方法為:addUserBefore增強:目標方法的參數為:[【增加的前綴】孫悟空, 7788]Before增強:被織入增強處理的目標對象為:org.crazyit.app.service.impl.HelloImpl@694abbdc執行Hello組件的addUser添加用戶:【增加的前綴】孫悟空Around增強:執行目標方法之后,模擬結束事務...After增強:模擬方法結束后的釋放資源...After增強:被織入增強處理的目標方法為:addUserAfter增強:目標方法的參數為:[【增加的前綴】孫悟空, 7788]After增強:被織入增強處理的目標對象為:org.crazyit.app.service.impl.HelloImpl@694abbdcAfterReturning增強:獲取目標方法返回值:400AfterReturning增強:模擬記錄日志功能...AfterReturning增強:被織入增強處理的目標方法為:addUserAfterReturning增強:目標方法的參數為:[【增加的前綴】孫悟空, 7788]AfterReturning增強:被織入增強處理的目標對象為:org.crazyit.app.service.impl.HelloImpl@694abbdcAround增強:執行目標方法之前,模擬開始事務...Before增強:模擬執行權限檢查Before增強:被織入增強處理的目標方法為:barBefore增強:目標方法的參數為:[]Before增強:被織入增強處理的目標對象為:org.crazyit.app.service.impl.WorldImpl@2e005c4b執行World組件的bar()方法Around增強:執行目標方法之后,模擬結束事務...After增強:模擬方法結束后的釋放資源...After增強:被織入增強處理的目標方法為:barAfter增強:目標方法的參數為:[]After增強:被織入增強處理的目標對象為:org.crazyit.app.service.impl.WorldImpl@2e005c4bAfterReturning增強:獲取目標方法返回值:nullAfterReturning增強:模擬記錄日志功能...AfterReturning增強:被織入增強處理的目標方法為:barAfterReturning增強:目標方法的參數為:[]AfterReturning增強:被織入增強處理的目標對象為:org.crazyit.app.service.impl.WorldImpl@2e005c4b

關于SpringAOP中怎么訪問目標方法的參數就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女