1. 程式人生 > 程式設計 >Spring AOP中使用args表示式的方法示例

Spring AOP中使用args表示式的方法示例

本文例項講述了Spring AOP中使用args表示式的方法。分享給大家供大家參考,具體如下:

一 配置

<?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.*;
@Aspect
public class AccessArgAspect
{
  // 下面的args(arg0,arg1)會限制目標方法必須有2個形參
  @AfterReturning(returning="rvt",pointcut=
    "execution(* org.crazyit.app.service.impl.*.*(..)) && args(arg0,arg1)")
  // 此處指定arg0、arg1為String型別
  // 則args(arg0,arg1)還要求目標方法的兩個形參都是String型別
  public void access(Object rvt,String arg0,String arg1)
  {
    System.out.println("呼叫目標方法第1個引數為:" + arg0);
    System.out.println("呼叫目標方法第2個引數為:" + arg1);
    System.out.println("獲取目標方法返回值:" + rvt);
    System.out.println("模擬記錄日誌功能...");
  }
}

三 介面

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

六 測試結果

執行Hello元件的foo()方法
執行Hello元件的addUser新增使用者:孫悟空
呼叫目標方法第1個引數為:孫悟空
呼叫目標方法第2個引數為:7788
獲取目標方法返回值:20
模擬記錄日誌功能...
執行World元件的bar()方法

更多關於java相關內容感興趣的讀者可檢視本站專題:《Spring框架入門與進階教程》、《Java資料結構與演算法教程》、《Java操作DOM節點技巧總結》、《Java檔案與目錄操作技巧彙總》和《Java快取操作技巧彙總》

希望本文所述對大家java程式設計有所幫助。