javascript
Spring-AOP @AspectJ进阶之绑定连接点方法入参
文章目錄
- 概述
- 實例
概述
我們前面的博文在講解切點函數時說過args()、this()、target()、@args()、@within()、@target()和@annotation()這7個函數除了可以指定類名外,還可以指定參數名將目標對象連接點上的方法入參綁定到增強的方法中。
其中args()用于綁定連接點方法的入參,@annotation()用于綁定連接點方法的注解對象,而@args()用于綁定連接點方法入參的注解
實例
來看一個args()綁定參數的實例
代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster
業務類
package com.xgj.aop.spring.advisor.aspectJAdvance.bindJointParameter;import org.springframework.stereotype.Component;/*** * * @ClassName: LogicBindService* * @Description: @Component標注的Bean* * @author: Mr.Yang* * @date: 2017年9月12日 上午1:39:23*/@Component public class LogicBindService {public void dealLogic(String bussiness, int number) {System.out.println("deal Logic:" + bussiness + ", number:" + number);} }編寫切面
package com.xgj.aop.spring.advisor.aspectJAdvance.bindJointParameter;import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before;/*** * * @ClassName: BindJointPointParameterAspect* * @Description: @Aspect標注的切面* * @author: Mr.Yang* * @date: 2017年9月12日 上午1:10:40*/@Aspect public class BindJointPointParameterAspect {// ①綁定連接點參數,首先args(name,number,..)根據②處的增強方法入參找到name和number對應的類型,以得到真實的切點表達式:// target(com.xgj.aop.spring.advisor.aspectJAdvance.bindJointParameter.LogicBindService)// && args(String,int,..)// 在該增強方法織入到目標連接點時,增強方法可以通過num和name訪問到連接點方法的入參。@Before("target(com.xgj.aop.spring.advisor.aspectJAdvance.bindJointParameter.LogicBindService) && args(name,number,..)")public void crossCodeCutting(int number, String name) throws Throwable { // ②增強方法接受連接點的參數System.out.println("----bindJoinPointParams()----");System.out.println("name:" + name);System.out.println("number:" + number);System.out.println("----bindJoinPointParams()----");} }在①處,我們通過args(name,number,…)進行連接點參數的綁定,和前面我們所講述的方式不一樣,當args()函數入參為參數名時,共包括兩方面的信息:
-
連接點匹配規則信息:連接點方法第一個入參是String類型,第二個入參是int類型;
-
連接點方法入參和增強方法入參的綁定信息:連接點方法的第一個入參綁定到增強方法的name參數上,第二個入參綁定到增強方法的number入參上。
切點匹配和參數綁定的過程是這樣的:
-
首先args()根據參數名稱在增強方法中查到名稱相同的入參并獲知對應的類型,這樣就知道匹配連接點方法的入參類型。
-
其次連接點方法入參類型所在的位置則由參數名在args()函數中聲明的位置決定。
args(name,number)只匹配第一個入參是String第二個入參是int的目標類方法,如LogicBindService.dealLogic(String bussiness, int number)而不匹配LogicBindService.dealLogic( int number,String bussiness)
切點匹配和參數綁定過程:
和args()一樣,其它可以綁定連接點參數的切點函數(如@args()和target()等),當指定參數名時,就同時具有匹配切點和綁定參數的雙重功能
將業務Bean和切面配置到配置文件中
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- (1)聲明Context命名空間以及Schema文件 (2)掃描類包以及應用注解定義的bean --> <context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJAdvance.bindJointParameter"/><!-- 基于@AspectJ切面的驅動器 --> <aop:aspectj-autoproxy proxy-target-class="true"/><!-- 使用了@AspectJ注解的切面類 --> <bean class="com.xgj.aop.spring.advisor.aspectJAdvance.bindJointParameter.BindJointPointParameterAspect"/></beans>測試類
package com.xgj.aop.spring.advisor.aspectJAdvance.bindJointParameter;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class BindJointPointParameterAspectTest {@Testpublic void test() {ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/aop/spring/advisor/aspectJAdvance/bindJointParameter/conf-joinPoint.xml");LogicBindService logicBindService = ctx.getBean("logicBindService",LogicBindService.class);logicBindService.dealLogic("PROGRAMMING", 5);} }運行結果
2017-09-12 02:05:35,991 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@24177697: startup date [Tue Sep 12 02:05:35 BOT 2017]; root of context hierarchy 2017-09-12 02:05:36,098 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJAdvance/bindJointParameter/conf-joinPoint.xml] ----bindJoinPointParams()---- name:PROGRAMMING number:5 ----bindJoinPointParams()---- deal Logic:PROGRAMMING, number:5可見,增強方法按預期綁定了LogicBindService.dealLogic(String bussiness, int number)方法的運行期入參。
Note: 為了保證實例能成功執行,必須啟用CGLib動態代理:<aop:aspectj-autoproxy proxy-target-class="true" />,因為該實例需要對NaiveWaiter類進行代理(因為NaiveWaiter#simle()方法不是Waiter接口的方法),所以必須使用CGLib生成子類的代理方法。
提示 :為了保證實例能成功執行,必須啟用CGLib動態代理:<aop:aspectj-autoproxy proxy-target-class="true" />,因為該實例不涉及到接口,所以必須使用CGLib生成子類的代理方法。 當然了,即使不設置(默認為jdk反向代理),當涉及的業務類沒有接口時,spring會自動使用cglib代理
總結
以上是生活随笔為你收集整理的Spring-AOP @AspectJ进阶之绑定连接点方法入参的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring-AOP @AspectJ进
- 下一篇: Spring-AOP @AspectJ进