Spring-AOP 靜態(tài)正則表達(dá)式方法如何匹配切面
概述
在Spring-AOP 靜態(tài)普通方法名匹配切面案例中 StaticMethodMatcherPointcutAdvisor中,僅能通過方法名定義切點(diǎn),這種描述方式不夠靈活,假設(shè)目標(biāo)類中有多個方法,切滿足一定的命名規(guī)范,使用正則表達(dá)式進(jìn)行匹配就靈活多了。
RegexpMethodPointcutAdvisor是正則表達(dá)式方法匹配的切面實(shí)現(xiàn)類,該類已經(jīng)是功能齊全的實(shí)現(xiàn)類,一般情況下無需擴(kuò)展該類。
實(shí)例
代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster
業(yè)務(wù)類 Waiter和Seller
package com.xgj.aop.spring.advisor.RegexpMethodPointcutAdvisor; public class Waiter { /** * * * @Title: greetTo * * @Description: * * @param name * * @return: void */ public void greetTo(String name) { System.out.println("Waiter Greet to " + name); } /** * * * @Title: serverTo * * @Description: * * @param name * * @return: void */ public void serverTo(String name) { System.out.println("Waiter Server to " + name); } }
package com.xgj.aop.spring.advisor.RegexpMethodPointcutAdvisor; public class Seller { /** * * * @Title: greetTo * * @Description: 和Waiter類中的同名的方法,目的是為了驗(yàn)證僅僅織入了Waiter類中的greetTo方法 * * @param name * * @return: void */ public void greetTo(String name) { System.out.println("Seller Greet to " + name); } }
前置增強(qiáng)
package com.xgj.aop.spring.advisor.RegexpMethodPointcutAdvisor; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; /** * * * @ClassName: GreetBeforeAdivce * * @Description: * * @author: Mr.Yang * * @date: 2017年8月18日 下午11:29:46 */ public class GreetBeforeAdivce implements MethodBeforeAdvice { @Override public void before(Method method, Object[] args, Object target) throws Throwable { // 輸出切點(diǎn) System.out.println("Pointcut:" + target.getClass().getName() + "." + method.getName()); String clientName = (String) args[0]; System.out.println("How are you " + clientName + " ?"); } }
配置文件
RegexpMethodPointcutAdvisor是正則表達(dá)式方法匹配的切面實(shí)現(xiàn)類,該類已經(jīng)是功能齊全的實(shí)現(xiàn)類,一般情況下無需擴(kuò)展該類。
直接使用RegexpMethodPointcutAdvisor,通過配置的方式為目標(biāo)類定義一個切面
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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.xsd"> <!-- 配置切面:靜態(tài)正則表達(dá)式方法匹配切面 --> <!-- Waiter目標(biāo)類 --> <bean id="waiterTarget" class="com.xgj.aop.spring.advisor.RegexpMethodPointcutAdvisor.Waiter"/> <!-- Seller目標(biāo)類 --> <bean id="sellerTarget" class="com.xgj.aop.spring.advisor.RegexpMethodPointcutAdvisor.Seller"/> <!-- 前置增強(qiáng) --> <bean id="greetBeforeAdvice" class="com.xgj.aop.spring.advisor.RegexpMethodPointcutAdvisor.GreetBeforeAdivce"/> <!-- 切面 --> <bean id="regexpAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" p:advice-ref="greetBeforeAdvice" p:pattern=".*greet.*" > <!-- 向切面注入一個前置增強(qiáng) , 用正則表達(dá)式定義目標(biāo)類全限定方法名的匹配模式串 。--> </bean> <!-- 通過父bean,配置公共的信息 --> <bean id="parent" abstract="true" class="org.springframework.aop.framework.ProxyFactoryBean" p:interceptorNames="regexpAdvisor" p:proxyTargetClass="true"/> <!-- waiter代理 --> <bean id="waiter" parent="parent" p:target-ref="waiterTarget"/> <!-- seller代理 --> <bean id="seller" parent="parent" p:target-ref="sellerTarget"/> </beans>
單元測試
package com.xgj.aop.spring.advisor.RegexpMethodPointcutAdvisor; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * * * @ClassName: RegexpMethodPointcutAdvisorTest * * @Description: 單元測試類 * * @author: Mr.Yang * * @date: 2017年8月18日 下午11:30:01 */ public class RegexpMethodPointcutAdvisorTest { @Test public void test() { // 加載配置文件,啟動容器 ApplicationContext ctx = new ClassPathXmlApplicationContext( "classpath:com/xgj/aop/spring/advisor/RegexpMethodPointcutAdvisor/conf-advisor.xml"); // 從容器中獲取Bean Waiter waiter = ctx.getBean("waiter", Waiter.class); Seller seller = ctx.getBean("seller", Seller.class); // 調(diào)用業(yè)務(wù)方法 waiter.greetTo("XiaoGongJiang"); waiter.serverTo("XiaoGongJiang"); seller.greetTo("XiaoGongJiang"); } }
運(yùn)行結(jié)果
可見,Waiter和 Seller的greetTo()都成功的織入了切面,Waiter.serverTo()沒有被織入切面。
RegexpMethodPointcutAdvisor除了pattern和advice屬性外 ,還有另外兩個屬性
patterns: 如果只有一個匹配串模式,這可以使用pattern屬性配置,如果需要定義多個匹配模式穿,就需要使用patterns屬性了, 這些匹配模式串是“或"的關(guān)系。order:切面織入時對應(yīng)的順序
正則表達(dá)式語法
正則表達(dá)式工具 RegexBuddy
可以使用RegexBuddy簡單的測下,如果匹配,下方會以黃色標(biāo)注。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
- 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á)式注入對象
- SpringBoot?AOP?@Pointcut切入點(diǎn)表達(dá)式排除某些類方式
- SpringAop切入點(diǎn)execution表達(dá)式的深入講解
- spring aop execution表達(dá)式的用法
- Spring AOP中使用args表達(dá)式的方法示例
- Spring中AOP的切點(diǎn)、通知、切點(diǎn)表達(dá)式及知識要點(diǎn)整理
相關(guān)文章
Java中線程的等待與喚醒_動力節(jié)點(diǎn)Java學(xué)院整理
在Object.java中,定義了wait(), notify()和notifyAll()等接口。wait()的作用是讓當(dāng)前線程進(jìn)入等待狀態(tài),同時,wait()也會讓當(dāng)前線程釋放它所持有的鎖。下面通過本文給大家介紹Java中線程的等待與喚醒知識,感興趣的朋友一起看看吧2017-05-05SpringMVC中MultipartFile轉(zhuǎn)File的兩種方式
在spring上傳文件中,一般都使用了MultipartFile來接收,但是有需要用到File的地方,本文主要介紹了SpringMVC中MultipartFile轉(zhuǎn)File的兩種方式,感興趣的可以了解一下2022-04-04JavaApi實(shí)現(xiàn)更新刪除及讀取節(jié)點(diǎn)
這篇文章主要介紹了JavaApi實(shí)現(xiàn)更新刪除及讀取節(jié)點(diǎn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05SpringBoot?Test的webEnvironment源碼解讀
這篇文章主要為大家介紹了SpringBoot?Test的webEnvironment源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09Java輕松實(shí)現(xiàn)批量插入或刪除Excel行列操作
在職場生活中,對Excel工作表的行和列進(jìn)行操作是非常普遍的需求,下面小編就來和大家介紹一下如何在Java中完成批量插入、刪除行和列的操作吧2023-10-10SpringBoot實(shí)現(xiàn)文件下載功能的方式分享
這篇文章主要為大家詳細(xì)介紹了SpringBoot這哪個實(shí)現(xiàn)文件下載功能的幾種方式,文中的實(shí)現(xiàn)方法簡介易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03Spring boot集成Go-FastDFS實(shí)現(xiàn)圖片上傳刪除等功能實(shí)現(xiàn)
這篇文章主要介紹了Spring boot集成Go-FastDFS實(shí)現(xiàn)圖片上傳刪除等功能實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04SpringCloud搭建netflix-eureka微服務(wù)集群的過程詳解
這篇文章主要介紹了SpringCloud搭建netflix-eureka微服務(wù)集群的過程詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04