javascript
Spring-AOP @AspectJ切点函数之execution()
文章目錄
- 概述
- 通過execution()定義切點的不同方式
- 通過方法簽名定義切點
- 通過類定義切點
- 通過類包定義切點
- 通過方法入參定義切點
- 實例
概述
execution()是最常用的切點函數,語法如下
execution(<修飾符模式>?<返回類型模式><方法名模式>(<參數模式>)<異常模式>?)其中:返回類型模式、方法名模式、參數模式是必選項。
通過execution()定義切點的不同方式
下面我們通過各種實例來理解如何使用execution()
通過方法簽名定義切點
-
execution(public * *(..)) 匹配所有目標類的public方法。 第一個*代表返回類型,第二個*代表方法名,而..代表任意入參的方法
-
execution(* *To(..)) 匹配目標類所有以To為后綴的方法。 第一個*代表返回類型,而*To代表任意以To為后綴的方法。
通過類定義切點
-
execution(* com.xgj.aop.spring.advisor.aspectJ.function.execution.classpoint.Cleaner.*(..))匹配Cleaner接口的所有方法(包括實現類中覆寫的方法), 第一個 * 代表返回任意類型 ,...Cleaner.*代表Cleaner接口中的所有方法
-
execution(* com.xgj.aop.spring.advisor.aspectJ.function.execution.classpoint.Cleaner.*(..))匹配Cleaner接口及其所有實現類的方法,不但匹配實現類中覆寫的方法,也包括實現類中不在接口中定義的方法
通過類包定義切點
在類名模式串中,.*表示包下的所有類,..*表示包、子孫包下的所有類
-
execution(* com.xgj.*(..))匹配com.xgj包下所有類的所有方法
-
execution(* com.xgj..*(..))匹配com.xgj包、子孫包下所有類的所有方法.比如 com.xgj.dao ,com.xgj.service,com.xgj.dao.user包下所有類的所有方法都匹配。 當 ..出現在類名中時,必須后面跟*表示子孫包下的所有類。
-
execution(* com..*Dao.find*(..))匹配包名前綴為com的任何包下類名后綴為Dao的方法,方法名必須以find為前綴, 比如com.xgj.UserDao#findUserById()方法都是匹配切點。
通過方法入參定義切點
切點表達式中的方法入參部分比較復雜,可以使用*和..通配符。 其中 *表示任意參數類型的參數, 而..表示任意類型的參數且參數個數不限。
-
execution(* joke(String,int))匹配joke(String,int)方法,且joke方法的第一個入參是String,第二個入參是int。 比如 匹配 SmartSeller#joke(String ,int)方法。 如果方法中的入參類型是java.lang包下的,這可以直接使用類名,否則必須使用全限定類名,比如 joke(java.util.List,int)
-
execution(* joke(String,*))匹配目標類中的joke()方法,該方法第一個入參為String,第二個入參為任意類型。 比如 joke(String s1, String s2)和joke(String s1,double d)都匹配,但是 joke(String s1, String s2,double d3)不匹配
-
execution(* joke(String,..))匹配目標類中的joke方法,該方法的第一個入參為String,后面可以有任意個入參且入參類型不限。 比如 joke(String s1),joke(String s1,String s2)和joke(String s1,double d2,String s3)都匹配。
-
execution(* joke(Object+))匹配目標類中的joke()方法,方法擁有一個入參,且入參是Object類型或該類的子類。 它匹配joke(String s1) 和joke(Client c) . 如果定義的切點是execution(* joke(Object)) ,則只匹配joke(Object object)而不匹配joke(String s1) 或者joke(Client c)
實例
代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster
僅以通過方法簽名定義切點為例子,其余場景請參考https://github.com/yangshangwei/SpringMaster, 親測通過。
execution(* com.xgj.aop.spring.advisor.aspectJ.function.execution.classpoint.Cleaner.*(..))
package com.xgj.aop.spring.advisor.aspectJ.function.execution;public class NaiveWaiter {/*** public方法,演示execution(public * *(..)),能匹配到*/public void greetTo(String clientName) {System.out.println("NaiveWaiter greet to " + clientName);}} package com.xgj.aop.spring.advisor.aspectJ.function.execution;public class SmartSeller {/*** public方法,演示execution(public * *(..)),能匹配到*/public void sell(String goods) {System.out.println("SmartSeller sells " + goods);}/*** * * @Title: smileTo* * @Description: 非public方法,演示execution(public * *(..)),不能匹配到* * @param clientName* * @return: void*/protected void smileTo(String clientName) {System.out.println("SmartSeller simles to " + clientName);}}切面
package com.xgj.aop.spring.advisor.aspectJ.function.execution;import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before;/*** * * @ClassName: ExecutionPublicAspect* * @Description: TODO* * @author: Mr.Yang* * @date: 2017年8月27日 下午1:47:55*/@Aspect public class ExecutionPublicAspect {@Before("execution(public * *(..))")public void crossCuttingLogic() {System.out.println("織入前置增強,橫切邏輯code");}}配置文件
<?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"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.xsd"><!-- 使用基于Schema的aop命名空間進行配置 --><!-- 基于@AspectJ切面的驅動器 --> <aop:aspectj-autoproxy/><!-- 目標Bean --> <bean id="smartSeller" class="com.xgj.aop.spring.advisor.aspectJ.function.execution.SmartSeller"/> <bean id="naiveWaiter" class="com.xgj.aop.spring.advisor.aspectJ.function.execution.NaiveWaiter"/> <!-- 使用了@AspectJ注解的切面類 --> <bean class="com.xgj.aop.spring.advisor.aspectJ.function.execution.ExecutionPublicAspect"/></beans>測試類
package com.xgj.aop.spring.advisor.aspectJ.function.execution;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;/*** * * @ClassName: ExecutionPublicAspectTest* * @Description: execution(public * *(..)) 測試類* * @author: Mr.Yang* * @date: 2017年8月27日 下午1:52:25*/ public class ExecutionPublicAspectTest {@Testpublic void test() {ApplicationContext ctx = new ClassPathXmlApplicationContext("com/xgj/aop/spring/advisor/aspectJ/function/execution/conf-execution.xml");SmartSeller smartSeller = (SmartSeller) ctx.getBean("smartSeller");// sell方法是public,會織入前置增強中的橫切邏輯smartSeller.sell("bread");// smileTo方法是protec,不會織入前置增強中的橫切邏輯smartSeller.smileTo("XiaoGongJiang");NaiveWaiter naiveWaiter = (NaiveWaiter) ctx.getBean("naiveWaiter");// greetTo方法是public,會織入前置增強中的橫切邏輯naiveWaiter.greetTo("XiaoGongJiang");}}運行結果
2017-08-29 00:00:39,395 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@24b9371e: startup date [Tue Aug 29 00:00:39 BOT 2017]; root of context hierarchy 2017-08-29 00:00:39,514 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/function/execution/conf-execution.xml] 織入前置增強,橫切邏輯code SmartSeller sells bread SmartSeller simles to XiaoGongJiang 織入前置增強,橫切邏輯code NaiveWaiter greet to XiaoGongJiang總結
以上是生活随笔為你收集整理的Spring-AOP @AspectJ切点函数之execution()的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring-AOP @AspectJ切
- 下一篇: Java-工具类之发送邮件