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è)簡單方法,模擬應(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è)簡單方法,模擬應(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è)簡單方法,模擬應(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è)簡單方法,模擬應(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框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
上一篇:java通過反射創(chuàng)建對(duì)象并調(diào)用方法
欄 目:Java
下一篇:沒有了
本文標(biāo)題:Spring AOP中使用args表達(dá)式的方法示例
本文地址:http://mengdiqiu.com.cn/a1/Java/8696.html
您可能感興趣的文章
- 01-10Springboot中@Value的使用詳解
- 01-10淺談Java中真的只有值傳遞么
- 01-10springboot實(shí)現(xiàn)文件上傳步驟解析
- 01-10springboot jta atomikos實(shí)現(xiàn)分布式事物管理
- 01-10SpringBoot使用RabbitMQ延時(shí)隊(duì)列(小白必備)
- 01-10Java搭建RabbitMq消息中間件過程詳解
- 01-10如何基于SpringBoot部署外部Tomcat過程解析
- 01-10SPRING BOOT啟動(dòng)命令參數(shù)及源碼詳析
- 01-10springboot集成fastDfs過程代碼實(shí)例
- 01-10springmvc級(jí)聯(lián)屬性處理無法轉(zhuǎn)換異常問題解決


閱讀排行
本欄相關(guān)
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)模擬時(shí)鐘
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
- 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時(shí)間
- 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
- 01-10Java動(dòng)態(tài)顯示當(dāng)前日期和時(shí)間
- 01-10淺談Java中真的只有值傳遞么
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?