您好,登錄后才能下訂單哦!
SpringAOP中args表達(dá)式如何使用,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
一 配置
<?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)搜索切面類(lèi) --> <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>
二 切面類(lèi)
package org.crazyit.app.aspect;import org.aspectj.lang.annotation.*;import org.aspectj.lang.*;@Aspectpublic 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類(lèi)型 // 則args(arg0,arg1)還要求目標(biāo)方法的兩個(gè)形參都是String類(lèi)型 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)用中的添加用戶(hù)的方法 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)類(lèi)
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)用中的添加用戶(hù)的方法 public int addUser(String name, String pass) { System.out.println("執(zhí)行Hello組件的addUser添加用戶(hù):" + 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è)試類(lèi)
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添加用戶(hù):孫悟空調(diào)用目標(biāo)方法第1個(gè)參數(shù)為:孫悟空調(diào)用目標(biāo)方法第2個(gè)參數(shù)為:7788獲取目標(biāo)方法返回值:20模擬記錄日志功能...執(zhí)行World組件的bar()方法
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。