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)文章
Spring Boot 與 Kotlin 使用JdbcTemplate連接MySQL數(shù)據(jù)庫(kù)的方法
本文介紹在Spring Boot基礎(chǔ)下配置數(shù)據(jù)源和通過(guò) JdbcTemplate 編寫(xiě)數(shù)據(jù)訪問(wèn)的示例。感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-01-01
Java多線程編程中的并發(fā)安全問(wèn)題及解決方法
保障多線程并發(fā)安全,解決線程同步與鎖競(jìng)爭(zhēng)問(wèn)題,提高應(yīng)用性能與可靠性。多線程編程需要考慮線程安全性,使用同步機(jī)制保證共享變量的一致性,避免線程競(jìng)爭(zhēng)導(dǎo)致的數(shù)據(jù)不一致與死鎖等問(wèn)題。常用的同步機(jī)制包括synchronized、ReentrantLock、volatile等2023-04-04
基于mybatis中<include>標(biāo)簽的作用說(shuō)明
這篇文章主要介紹了基于mybatis中<include>標(biāo)簽的作用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
ANSI,Unicode,BMP,UTF等編碼概念實(shí)例講解
這篇文章主要介紹了ANSI,Unicode,BMP,UTF等編碼概念實(shí)例講解,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
Spring?Boot整合郵箱發(fā)送郵件實(shí)例
大家好,本篇文章主要講的是Spring?Boot整合郵箱發(fā)送郵件實(shí)例,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02
細(xì)談java同步之JMM(Java Memory Model)
Java內(nèi)存模型是在硬件內(nèi)存模型上的更高層的抽象,它屏蔽了各種硬件和操作系統(tǒng)訪問(wèn)的差異性,保證了Java程序在各種平臺(tái)下對(duì)內(nèi)存的訪問(wèn)都能達(dá)到一致的效果。下面我們來(lái)一起學(xué)習(xí)下JMM2019-05-05
JAVA基于Redis實(shí)現(xiàn)計(jì)數(shù)器限流的使用示例
計(jì)數(shù)器法是限流算法里最簡(jiǎn)單也是最容易實(shí)現(xiàn)的一種算法,本文主要介紹了JAVA基于Redis實(shí)現(xiàn)計(jì)數(shù)器限流的使用示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
java中數(shù)組list map三者之間的互轉(zhuǎn)介紹
java中 數(shù)組 list map之間的互轉(zhuǎn)一張圖清晰呈現(xiàn)并附有代碼,不懂的朋友可以參考下2013-10-10
java構(gòu)造函數(shù)示例(構(gòu)造方法)
這篇文章主要介紹了java構(gòu)造函數(shù)示例(構(gòu)造方法),需要的朋友可以參考下2014-03-03

