Spring AOP中使用args表達(dá)式的方法示例
本文實(shí)例講述了Spring AOP中使用args表達(dá)式的方法。分享給大家供大家參考,具體如下:
一 配置
<?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)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門(mén)與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- Spring-AOP @AspectJ切點(diǎn)函數(shù)之@annotation()用法
- SpringAOP切點(diǎn)函數(shù)實(shí)現(xiàn)原理詳解
- Spring AOP中定義切點(diǎn)的實(shí)現(xiàn)方法示例
- Spring AOP如何在注解上使用SPEL表達(dá)式注入對(duì)象
- SpringBoot?AOP?@Pointcut切入點(diǎn)表達(dá)式排除某些類方式
- SpringAop切入點(diǎn)execution表達(dá)式的深入講解
- Spring-AOP 靜態(tài)正則表達(dá)式方法如何匹配切面
- spring aop execution表達(dá)式的用法
- Spring中AOP的切點(diǎn)、通知、切點(diǎn)表達(dá)式及知識(shí)要點(diǎn)整理
相關(guān)文章
Java多線程編程小實(shí)例模擬停車(chē)場(chǎng)系統(tǒng)
這是一個(gè)關(guān)于Java多線程編程的例子,用多線程的思想模擬停車(chē)場(chǎng)管理系統(tǒng),這里分享給大家,供需要的朋友參考。2017-10-10全面剖析java 數(shù)據(jù)類型與運(yùn)算符
這篇文章主要介紹了Java基本數(shù)據(jù)類型和運(yùn)算符,結(jié)合實(shí)例形式詳細(xì)分析了java基本數(shù)據(jù)類型、數(shù)據(jù)類型轉(zhuǎn)換、算術(shù)運(yùn)算符、邏輯運(yùn)算符等相關(guān)原理與操作技巧,需要的朋友可以參考下2021-09-09SpringCloud使用Ribbon實(shí)現(xiàn)負(fù)載均衡的流程步驟
在微服務(wù)架構(gòu)中,負(fù)載均衡是一項(xiàng)關(guān)鍵的技術(shù),它可以確保各個(gè)服務(wù)節(jié)點(diǎn)間的負(fù)載分布均勻,提高整個(gè)系統(tǒng)的穩(wěn)定性和性能,Spring Cloud 中的 Ribbon 就是一種負(fù)載均衡的解決方案,本文將深入探討 Ribbon 的原理和在微服務(wù)中的應(yīng)用,需要的朋友可以參考下2024-02-02Java 數(shù)組聲明、創(chuàng)建、初始化詳解
本文主要介紹Java 數(shù)組聲明、創(chuàng)建、初始化的資料,這里整理相關(guān)知識(shí),及簡(jiǎn)單實(shí)現(xiàn)代碼,幫助大家學(xué)習(xí),有興趣的小伙伴可以參考下2016-09-09Java8新特性之StampedLock_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
本文從synchronized、Lock到Java8新增的StampedLock進(jìn)行對(duì)比分析,對(duì)Java8新特性之StampedLock相關(guān)知識(shí)感興趣的朋友一起看看吧2017-06-06最全LocalDateTime、LocalDate、Date、String相互轉(zhuǎn)化的方法
大家在開(kāi)發(fā)過(guò)程中必不可少的和日期打交道,對(duì)接別的系統(tǒng)時(shí),時(shí)間日期格式不一致,每次都要轉(zhuǎn)化,本文為大家準(zhǔn)備了最全的LocalDateTime、LocalDate、Date、String相互轉(zhuǎn)化方法,需要的可以參考一下2023-06-06Spring Cloud學(xué)習(xí)教程之Zuul統(tǒng)一異常處理與回退
Spring Cloud Zuul對(duì)異常的處理整體來(lái)說(shuō)還是比較方便的,流程也比較清晰,下面這篇文章主要給大家介紹了關(guān)于Spring Cloud學(xué)習(xí)教程之Zuul統(tǒng)一異常處理與回退的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04史上最簡(jiǎn)單的MyBatis動(dòng)態(tài)SQL入門(mén)示例代碼
動(dòng)態(tài)sql,可以根據(jù)用戶對(duì)字段選擇和輸入,動(dòng)態(tài)生成一條sql執(zhí)行。接下來(lái)通過(guò)本文給大家分享MyBatis動(dòng)態(tài)SQL入門(mén)示例代碼,一起看看吧2017-03-03