使用SpringAOP怎么訪問目標方法的參數(shù)-創(chuàng)新互聯(lián)

這篇文章將為大家詳細講解有關(guān)使用Spring AOP怎么訪問目標方法的參數(shù),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設(shè)計、做網(wǎng)站與策劃設(shè)計,衛(wèi)濱網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:衛(wèi)濱等地區(qū)。衛(wèi)濱做網(wǎng)站價格咨詢:028-86922220

一 配置

<?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;
// 定義一個切面
@Aspect
public class FourAdviceTest
{
  // 定義Around增強處理
  @Around("execution(* org.crazyit.app.service.impl.*.*(..))")
  public Object processTx(ProceedingJoinPoint jp)
    throws java.lang.Throwable
  {
    System.out.println("Around增強:執(zhí)行目標方法之前,模擬開始事務(wù)...");
    // 訪問執(zhí)行目標方法的參數(shù)
    Object[] args = jp.getArgs();
    // 當執(zhí)行目標方法的參數(shù)存在,
    // 且第一個參數(shù)是字符串參數(shù)
    if (args != null && args.length > 0
      && args[0].getClass() == String.class)
    {
      // 修改目標方法調(diào)用參數(shù)的第一個參數(shù)
      args[0] = "【增加的前綴】" + args[0];
    }
    //執(zhí)行目標方法,并保存目標方法執(zhí)行后的返回值
    Object rvt = jp.proceed(args);
    System.out.println("Around增強:執(zhí)行目標方法之后,模擬結(jié)束事務(wù)...");
    // 如果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增強:模擬執(zhí)行權(quán)限檢查");
    // 返回被織入增強處理的目標方法
    System.out.println("Before增強:被織入增強處理的目標方法為:"
      + jp.getSignature().getName());
    // 訪問執(zhí)行目標方法的參數(shù)
    System.out.println("Before增強:目標方法的參數(shù)為:"
      + 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());
    // 訪問執(zhí)行目標方法的參數(shù)
    System.out.println("AfterReturning增強:目標方法的參數(shù)為:"
      + 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增強:模擬方法結(jié)束后的釋放資源...");
    // 返回被織入增強處理的目標方法
    System.out.println("After增強:被織入增強處理的目標方法為:"
      + jp.getSignature().getName());
    // 訪問執(zhí)行目標方法的參數(shù)
    System.out.println("After增強:目標方法的參數(shù)為:"
      + Arrays.toString(jp.getArgs()));
    // 訪問被增強處理的目標對象
    System.out.println("After增強:被織入增強處理的目標對象為:"
      + jp.getTarget());
  }
}

三 接口

Hello

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

World

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

四 實現(xiàn)類

HelloImpl

package org.crazyit.app.service.impl;
import org.springframework.stereotype.Component;
import org.crazyit.app.service.*;
@Component("hello")
public class HelloImpl implements Hello {
  // 定義一個簡單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
  public void foo() {
    System.out.println("執(zhí)行Hello組件的foo()方法");
  }
  // 定義一個addUser()方法,模擬應(yīng)用中的添加用戶的方法
  public int addUser(String name, String pass) {
    System.out.println("執(zhí)行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 {
  // 定義一個簡單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
  public void bar() {
    System.out.println("執(zhí)行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) {
    // 創(chuàng)建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();
  }
}

六 測試結(jié)果

Around增強:執(zhí)行目標方法之前,模擬開始事務(wù)...
Before增強:模擬執(zhí)行權(quán)限檢查
Before增強:被織入增強處理的目標方法為:foo
Before增強:目標方法的參數(shù)為:[]
Before增強:被織入增強處理的目標對象為:org.crazyit.app.service.impl.HelloImpl@694abbdc
執(zhí)行Hello組件的foo()方法
Around增強:執(zhí)行目標方法之后,模擬結(jié)束事務(wù)...
After增強:模擬方法結(jié)束后的釋放資源...
After增強:被織入增強處理的目標方法為:foo
After增強:目標方法的參數(shù)為:[]
After增強:被織入增強處理的目標對象為:org.crazyit.app.service.impl.HelloImpl@694abbdc
AfterReturning增強:獲取目標方法返回值:null
AfterReturning增強:模擬記錄日志功能...
AfterReturning增強:被織入增強處理的目標方法為:foo
AfterReturning增強:目標方法的參數(shù)為:[]
AfterReturning增強:被織入增強處理的目標對象為:org.crazyit.app.service.impl.HelloImpl@694abbdc
Around增強:執(zhí)行目標方法之前,模擬開始事務(wù)...
Before增強:模擬執(zhí)行權(quán)限檢查
Before增強:被織入增強處理的目標方法為:addUser
Before增強:目標方法的參數(shù)為:[【增加的前綴】孫悟空, 7788]
Before增強:被織入增強處理的目標對象為:org.crazyit.app.service.impl.HelloImpl@694abbdc
執(zhí)行Hello組件的addUser添加用戶:【增加的前綴】孫悟空
Around增強:執(zhí)行目標方法之后,模擬結(jié)束事務(wù)...
After增強:模擬方法結(jié)束后的釋放資源...
After增強:被織入增強處理的目標方法為:addUser
After增強:目標方法的參數(shù)為:[【增加的前綴】孫悟空, 7788]
After增強:被織入增強處理的目標對象為:org.crazyit.app.service.impl.HelloImpl@694abbdc
AfterReturning增強:獲取目標方法返回值:400
AfterReturning增強:模擬記錄日志功能...
AfterReturning增強:被織入增強處理的目標方法為:addUser
AfterReturning增強:目標方法的參數(shù)為:[【增加的前綴】孫悟空, 7788]
AfterReturning增強:被織入增強處理的目標對象為:org.crazyit.app.service.impl.HelloImpl@694abbdc
Around增強:執(zhí)行目標方法之前,模擬開始事務(wù)...
Before增強:模擬執(zhí)行權(quán)限檢查
Before增強:被織入增強處理的目標方法為:bar
Before增強:目標方法的參數(shù)為:[]
Before增強:被織入增強處理的目標對象為:org.crazyit.app.service.impl.WorldImpl@2e005c4b
執(zhí)行World組件的bar()方法
Around增強:執(zhí)行目標方法之后,模擬結(jié)束事務(wù)...
After增強:模擬方法結(jié)束后的釋放資源...
After增強:被織入增強處理的目標方法為:bar
After增強:目標方法的參數(shù)為:[]
After增強:被織入增強處理的目標對象為:org.crazyit.app.service.impl.WorldImpl@2e005c4b
AfterReturning增強:獲取目標方法返回值:null
AfterReturning增強:模擬記錄日志功能...
AfterReturning增強:被織入增強處理的目標方法為:bar
AfterReturning增強:目標方法的參數(shù)為:[]
AfterReturning增強:被織入增強處理的目標對象為:org.crazyit.app.service.impl.WorldImpl@2e005c4b

關(guān)于使用Spring AOP怎么訪問目標方法的參數(shù)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.muchs.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)站欄目:使用SpringAOP怎么訪問目標方法的參數(shù)-創(chuàng)新互聯(lián)
標題網(wǎng)址:http://www.muchs.cn/article28/dhjscp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管標簽優(yōu)化、面包屑導(dǎo)航、網(wǎng)站排名響應(yīng)式網(wǎng)站、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計