溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

args表達(dá)式如何在Spring AOP中使用

發(fā)布時(shí)間:2021-03-22 16:41:06 來源:億速云 閱讀:166 作者:Leah 欄目:編程語(yǔ)言

這篇文章將為大家詳細(xì)講解有關(guān)args表達(dá)式如何在Spring AOP中使用,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(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.*;
@Aspect
public class AccessArgAspect
{
  // 下面的args(arg0,arg1)會(huì)限制目標(biāo)方法必須有2個(gè)形參
  @AfterReturning(returning="rvt" , pointcut=
    "execution(* org.crazyit.app.service.impl.*.*(..)) && args(arg0,arg1)")
  // 此處指定arg0、arg1為String類型
  // 則args(arg0,arg1)還要求目標(biāo)方法的兩個(gè)形參都是String類型
  public void access(Object rvt, String arg0 , String arg1)
  {
    System.out.println("調(diào)用目標(biāo)方法第1個(gè)參數(shù)為:" + arg0);
    System.out.println("調(diào)用目標(biāo)方法第2個(gè)參數(shù)為:" + arg1);
    System.out.println("獲取目標(biāo)方法返回值:" + rvt);
    System.out.println("模擬記錄日志功能...");
  }
}

三 接口

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é)果

執(zhí)行Hello組件的foo()方法
執(zhí)行Hello組件的addUser添加用戶:孫悟空
調(diào)用目標(biāo)方法第1個(gè)參數(shù)為:孫悟空
調(diào)用目標(biāo)方法第2個(gè)參數(shù)為:7788
獲取目標(biāo)方法返回值:20
模擬記錄日志功能...
執(zhí)行World組件的bar()方法

關(guān)于args表達(dá)式如何在Spring AOP中使用就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI