Spring-AOP 靜態(tài)普通方法名匹配切面操作
概述
StaticMethodMatcherPointcutAdvisor代表一個靜態(tài)方法匹配切面,它通過StaticMethodMatcherPointcut來定義切點,并通過類過濾和方法名來匹配所定義的切點.
實例
代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster
我們假設(shè)我們業(yè)務(wù)類中 Waiter和 Seller中都有同名的greetTo()方法.
業(yè)務(wù)類Waiter
package com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor; 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); } }
業(yè)務(wù)類Seller
package com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor; public class Seller { /** * * * @Title: greetTo * * @Description: 和Waiter類中的同名的方法,目的是為了驗證僅僅織入了Waiter類中的greetTo方法 * * @param name * * @return: void */ public void greetTo(String name) { System.out.println("Seller Greet to " + name); } }
現(xiàn)在我們希望通過StaticMethodMatcherPointcutAdvisor定義一個切面,在Waiter#greetTo()方法調(diào)用前織入一個增強,即連接點為Waiter#greetTo()方法調(diào)用前的位置。
切面代碼
package com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor; import java.lang.reflect.Method; import org.springframework.aop.ClassFilter; import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor; /** * * * @ClassName: GreetingAdvisor * * @Description: 切面類 * * @author: Mr.Yang * * @date: 2017年8月18日 下午8:27:52 */ public class GreetingAdvisor extends StaticMethodMatcherPointcutAdvisor { private static final long serialVersionUID = 1L; /** * 重寫matches方法,切點方法匹配規(guī)則:方法名為greetTo */ @Override public boolean matches(Method method, Class<?> targetClass) { return "greetTo".equals(method.getName()); } /** * 默認情況下,匹配所有的類,重寫getClassFilter,定義匹配規(guī)則 切點類型匹配規(guī)則,為Waiter的類或者之類 */ public ClassFilter getClassFilter() { return new ClassFilter() { @Override public boolean matches(Class<?> clazz) { return Waiter.class.isAssignableFrom(clazz); } }; } }
StaticMethodMatcherPointcutAdvisor 抽象類唯一需要定義的是matches()方法,在默認情況下,該切面匹配所有的類,這里通過覆蓋getClassFilter()方法,讓它僅匹配Waiter類及其子類。
當(dāng)然,Advisor還需要一個增強類的配合 .
我們來定義一個前置增強
package com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; /** * * * @ClassName: GreetBeforeAdivce * * @Description: 前置增強 * * @author: Mr.Yang * * @date: 2017年8月18日 下午8:27:40 */ public class GreetBeforeAdivce implements MethodBeforeAdvice { @Override public void before(Method method, Object[] args, Object target) throws Throwable { // 輸出切點 System.out.println("Pointcut:" + target.getClass().getName() + "." + method.getName()); String clientName = (String) args[0]; System.out.println("How are you " + clientName + " ?"); } }
我們使用Spring配置來定義切面等信息
<?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)方法匹配切面 --> <!-- Waiter目標(biāo)類 --> <bean id="waiterTarget" class="com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor.Waiter"/> <!-- Seller目標(biāo)類 --> <bean id="sellerTarget" class="com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor.Seller"/> <!-- 前置增強 --> <bean id="greetBeforeAdvice" class="com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor.GreetBeforeAdivce"/> <!-- 切面 --> <bean id="greetAdvicesor" class="com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor.GreetingAdvisor" p:advice-ref="greetBeforeAdvice"/> <!-- 向切面注入一個前置增強 --> <!-- 通過父bean,配置公共的信息 --> <bean id="parent" abstract="true" class="org.springframework.aop.framework.ProxyFactoryBean" p:interceptorNames="greetAdvicesor" 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.StaticMethodMatcherPointcutAdvisor; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * * * @ClassName: StaticMethodMatcherPointcutAdvisorTest * * @Description: 測試類 * * @author: Mr.Yang * * @date: 2017年8月18日 下午8:29:28 */ public class StaticMethodMatcherPointcutAdvisorTest { @Test public void test() { // 加載配置文件,啟動容器 ApplicationContext ctx = new ClassPathXmlApplicationContext( "classpath:com/xgj/aop/spring/advisor/StaticMethodMatcherPointcutAdvisor/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"); } }
運行結(jié)果:
我們可以看到切面僅僅織入了Wwaiter.greetTo()方法調(diào)用前的連接點上, Waiter.serverTo()和Seller.greetTo()方法并沒有織入切面。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Lombok使用@Tolerate實現(xiàn)沖突兼容問題
這篇文章主要介紹了Lombok使用@Tolerate實現(xiàn)沖突兼容問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08java并發(fā)編程JUC CountDownLatch線程同步
這篇文章主要介紹CountDownLatch是什么、CountDownLatch 如何工作、CountDownLatch 的代碼例子來展開對java并發(fā)編程JUC CountDownLatch線程同步,需要的朋友可以參考下面文章內(nèi)容2021-09-09Springboot?配置線程池創(chuàng)建線程及配置?@Async?異步操作線程池詳解
這篇文章主要介紹了Springboot?配置線程池創(chuàng)建線程及配置?@Async?異步操作線程池詳解,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09Java循環(huán)對bean的屬性進行賦值的實現(xiàn)
本文主要介紹了Java循環(huán)對bean的屬性進行賦值,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08解決springboot配置文件組解決自動配置屬性無法注入問題
在使用Spring Boot時,可能會遇到配置文件屬性注入失敗的問題,本文描述了一個案例,其中嘗試使用profile文件組指定不同環(huán)境下的配置文件,但遇到了屬性無法成功注入的情況,提供的解決辦法是將Spring Boot的版本號從2.2.0.RELEASE升級到2.4.02024-09-09