Spring AOP訪問目標方法的參數(shù)操作示例
本文實例講述了Spring AOP訪問目標方法的參數(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">
<!-- 指定自動搜索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í)行目標方法之前,模擬開始事務...");
// 訪問執(zhí)行目標方法的參數(shù)
Object[] args = jp.getArgs();
// 當執(zhí)行目標方法的參數(shù)存在,
// 且第一個參數(shù)是字符串參數(shù)
if (args != null && args.length > 0
&& args[0].getClass() == String.class)
{
// 修改目標方法調用參數(shù)的第一個參數(shù)
args[0] = "【增加的前綴】" + args[0];
}
//執(zhí)行目標方法,并保存目標方法執(zhí)行后的返回值
Object rvt = jp.proceed(args);
System.out.println("Around增強:執(zhí)行目標方法之后,模擬結束事務...");
// 如果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í)行權限檢查");
// 返回被織入增強處理的目標方法
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增強:模擬方法結束后的釋放資源...");
// 返回被織入增強處理的目標方法
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è)務邏輯方法
void foo();
// 定義一個addUser()方法,模擬應用中的添加用戶的方法
int addUser(String name, String pass);
}
World
package org.crazyit.app.service;
public interface World {
// 定義一個簡單方法,模擬應用中的業(yè)務邏輯方法
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è)務邏輯方法
public void foo() {
System.out.println("執(zhí)行Hello組件的foo()方法");
}
// 定義一個addUser()方法,模擬應用中的添加用戶的方法
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è)務邏輯方法
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();
}
}
六 測試結果
Around增強:執(zhí)行目標方法之前,模擬開始事務...
Before增強:模擬執(zhí)行權限檢查
Before增強:被織入增強處理的目標方法為:foo
Before增強:目標方法的參數(shù)為:[]
Before增強:被織入增強處理的目標對象為:org.crazyit.app.service.impl.HelloImpl@694abbdc
執(zhí)行Hello組件的foo()方法
Around增強:執(zhí)行目標方法之后,模擬結束事務...
After增強:模擬方法結束后的釋放資源...
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í)行目標方法之前,模擬開始事務...
Before增強:模擬執(zhí)行權限檢查
Before增強:被織入增強處理的目標方法為:addUser
Before增強:目標方法的參數(shù)為:[【增加的前綴】孫悟空, 7788]
Before增強:被織入增強處理的目標對象為:org.crazyit.app.service.impl.HelloImpl@694abbdc
執(zhí)行Hello組件的addUser添加用戶:【增加的前綴】孫悟空
Around增強:執(zhí)行目標方法之后,模擬結束事務...
After增強:模擬方法結束后的釋放資源...
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í)行目標方法之前,模擬開始事務...
Before增強:模擬執(zhí)行權限檢查
Before增強:被織入增強處理的目標方法為:bar
Before增強:目標方法的參數(shù)為:[]
Before增強:被織入增強處理的目標對象為:org.crazyit.app.service.impl.WorldImpl@2e005c4b
執(zhí)行World組件的bar()方法
Around增強:執(zhí)行目標方法之后,模擬結束事務...
After增強:模擬方法結束后的釋放資源...
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
更多關于java相關內容感興趣的讀者可查看本站專題:《Spring框架入門與進階教程》、《Java數(shù)據(jù)結構與算法教程》、《Java操作DOM節(jié)點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。
相關文章
SpringMVC結合ajaxfileupload.js實現(xiàn)文件無刷新上傳
這篇文章主要介紹了SpringMVC結合ajaxfileupload.js實現(xiàn)文件無刷新上傳,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
MybatisPlus實現(xiàn)對象嵌套關聯(lián)查詢一對多List集合查詢
這篇文章主要介紹了MybatisPlus實現(xiàn)對象嵌套關聯(lián)查詢一對多List集合查詢,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
springboot @Value實現(xiàn)獲取計算機中絕對路徑文件的內容
這篇文章主要介紹了springboot @Value實現(xiàn)獲取計算機中絕對路徑文件的內容,具有很好的參考價值,希望對大家有所幫助。2021-09-09
java網(wǎng)絡編程之識別示例 獲取主機網(wǎng)絡接口列表
一個客戶端想要發(fā)起一次通信,先決條件就是需要知道運行著服務器端程序的主機的IP地址是多少。然后我們才能夠通過這個地址向服務器發(fā)送信息。2014-01-01

