亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring AOP訪問目標(biāo)方法的參數(shù)操作示例

 更新時(shí)間:2020年01月09日 09:03:54   作者:cakincqm  
這篇文章主要介紹了Spring AOP訪問目標(biāo)方法的參數(shù)操作,結(jié)合實(shí)例形式詳細(xì)分析了spring面向切面AOP訪問目標(biāo)方法的參數(shù)相關(guān)實(shí)現(xiàn)步驟與操作注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了Spring AOP訪問目標(biāo)方法的參數(shù)操作。分享給大家供大家參考,具體如下:

一 配置

<?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">
   <!-- 指定自動(dòng)搜索Bean組件、自動(dòng)搜索切面類 -->
   <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>
   <!-- 啟動(dòng)@AspectJ支持 -->
   <aop:aspectj-autoproxy />
</beans>

二 切面類

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

三 接口

Hello

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

World

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

四 實(shí)現(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 {
  // 定義一個(gè)簡(jiǎn)單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
  public void foo() {
    System.out.println("執(zhí)行Hello組件的foo()方法");
  }
  // 定義一個(gè)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 {
  // 定義一個(gè)簡(jiǎn)單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
  public void bar() {
    System.out.println("執(zhí)行World組件的bar()方法");
  }
}

五 測(cè)試類

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();
  }
}

六 測(cè)試結(jié)果

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

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Java之String、StringBuffer、StringBuilder的區(qū)別分析

    Java之String、StringBuffer、StringBuilder的區(qū)別分析

    今天搞安卓在看書的時(shí)候遇到了StringBuilder這個(gè)類型的東東,有點(diǎn)小迷,不知道它跟string、stringbuffer的關(guān)系式怎么樣的,趕快查閱相關(guān)資料,了解了個(gè)大概,拿出來(lái)分享一下
    2012-11-11
  • 深入分析Java內(nèi)存區(qū)域的使用詳解

    深入分析Java內(nèi)存區(qū)域的使用詳解

    本篇文章對(duì)Java內(nèi)存區(qū)域的使用進(jìn)行了詳細(xì)的介紹。需要的朋友參考下
    2013-05-05
  • Spring Boot與ActiveMQ整合的步驟

    Spring Boot與ActiveMQ整合的步驟

    今天小編就為大家分享一篇關(guān)于Spring Boot與ActiveMQ整合的步驟,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • SpringBoot3.x中spring.factories?SPI?服務(wù)發(fā)現(xiàn)機(jī)制的改變問題小結(jié)

    SpringBoot3.x中spring.factories?SPI?服務(wù)發(fā)現(xiàn)機(jī)制的改變問題小結(jié)

    spring.factories其實(shí)是SpringBoot提供的SPI機(jī)制,底層實(shí)現(xiàn)是基于SpringFactoriesLoader檢索ClassLoader中所有jar引入的META-INF/spring.factories文件,這篇文章主要介紹了SpringBoot3.x中spring.factories?SPI?服務(wù)發(fā)現(xiàn)機(jī)制的改變,需要的朋友可以參考下
    2023-05-05
  • Java Spring Cloud 負(fù)載均衡詳解

    Java Spring Cloud 負(fù)載均衡詳解

    這篇文章主要介紹了Spring Cloud負(fù)載均衡及遠(yuǎn)程調(diào)用實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2021-09-09
  • Tomcat 多端口 多應(yīng)用

    Tomcat 多端口 多應(yīng)用

    當(dāng)你使用Tomcat作為Web Server的時(shí)候,是不是會(huì)想過這樣的一個(gè)問題:如何利用Tomcat建立兩個(gè)Web Server,同時(shí)偵聽來(lái)自不同端口的請(qǐng)求呢?要實(shí)現(xiàn)這一點(diǎn)是很簡(jiǎn)單的。以下假設(shè)Tomcat安裝在D:/Tomcat5.0目錄下。
    2008-02-02
  • MybatisPlus開啟二級(jí)緩存的方法詳解

    MybatisPlus開啟二級(jí)緩存的方法詳解

    這篇文章主要介紹了MybatisPlus開啟二級(jí)緩存的方法詳解,二級(jí)緩存是基于mapper文件的namespace級(jí)別,也就是說(shuō)多個(gè)sqlSession可以共享一個(gè)mapper中的二級(jí)緩存區(qū)域,需要的朋友可以參考下
    2023-11-11
  • 最新評(píng)論