Spring-AOP 靜態(tài)普通方法名匹配切面操作
概述
StaticMethodMatcherPointcutAdvisor代表一個(gè)靜態(tài)方法匹配切面,它通過StaticMethodMatcherPointcut來定義切點(diǎn),并通過類過濾和方法名來匹配所定義的切點(diǎn).
實(shí)例
代碼已托管到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類中的同名的方法,目的是為了驗(yàn)證僅僅織入了Waiter類中的greetTo方法
*
* @param name
*
* @return: void
*/
public void greetTo(String name) {
System.out.println("Seller Greet to " + name);
}
}
現(xiàn)在我們希望通過StaticMethodMatcherPointcutAdvisor定義一個(gè)切面,在Waiter#greetTo()方法調(diào)用前織入一個(gè)增強(qiáng),即連接點(diǎn)為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方法,切點(diǎn)方法匹配規(guī)則:方法名為greetTo
*/
@Override
public boolean matches(Method method, Class<?> targetClass) {
return "greetTo".equals(method.getName());
}
/**
* 默認(rèn)情況下,匹配所有的類,重寫getClassFilter,定義匹配規(guī)則 切點(diǎn)類型匹配規(guī)則,為Waiter的類或者之類
*/
public ClassFilter getClassFilter() {
return new ClassFilter() {
@Override
public boolean matches(Class<?> clazz) {
return Waiter.class.isAssignableFrom(clazz);
}
};
}
}
StaticMethodMatcherPointcutAdvisor 抽象類唯一需要定義的是matches()方法,在默認(rèn)情況下,該切面匹配所有的類,這里通過覆蓋getClassFilter()方法,讓它僅匹配Waiter類及其子類。
當(dāng)然,Advisor還需要一個(gè)增強(qiáng)類的配合 .
我們來定義一個(gè)前置增強(qiáng)
package com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
/**
*
*
* @ClassName: GreetBeforeAdivce
*
* @Description: 前置增強(qiáng)
*
* @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 {
// 輸出切點(diǎn)
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"/> <!-- 前置增強(qiáng) --> <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"/> <!-- 向切面注入一個(gè)前置增強(qiáng) --> <!-- 通過父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() {
// 加載配置文件,啟動(dòng)容器
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");
}
}
運(yùn)行結(jié)果:

我們可以看到切面僅僅織入了Wwaiter.greetTo()方法調(diào)用前的連接點(diǎn)上, Waiter.serverTo()和Seller.greetTo()方法并沒有織入切面。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Lombok使用@Tolerate實(shí)現(xiàn)沖突兼容問題
這篇文章主要介紹了Lombok使用@Tolerate實(shí)現(xiàn)沖突兼容問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
java并發(fā)編程JUC CountDownLatch線程同步
這篇文章主要介紹CountDownLatch是什么、CountDownLatch 如何工作、CountDownLatch 的代碼例子來展開對(duì)java并發(fā)編程JUC CountDownLatch線程同步,需要的朋友可以參考下面文章內(nèi)容2021-09-09
Springboot?配置線程池創(chuàng)建線程及配置?@Async?異步操作線程池詳解
這篇文章主要介紹了Springboot?配置線程池創(chuàng)建線程及配置?@Async?異步操作線程池詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
Java循環(huán)對(duì)bean的屬性進(jìn)行賦值的實(shí)現(xiàn)
本文主要介紹了Java循環(huán)對(duì)bean的屬性進(jìn)行賦值,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
解決springboot配置文件組解決自動(dòng)配置屬性無法注入問題
在使用Spring Boot時(shí),可能會(huì)遇到配置文件屬性注入失敗的問題,本文描述了一個(gè)案例,其中嘗試使用profile文件組指定不同環(huán)境下的配置文件,但遇到了屬性無法成功注入的情況,提供的解決辦法是將Spring Boot的版本號(hào)從2.2.0.RELEASE升級(jí)到2.4.02024-09-09

