溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Spring-AOP 靜態(tài)普通方法名匹配切面的方法

發(fā)布時(shí)間:2021-07-19 08:25:26 來(lái)源:億速云 閱讀:229 作者:chen 欄目:開(kāi)發(fā)技術(shù)

本篇內(nèi)容主要講解“Spring-AOP 靜態(tài)普通方法名匹配切面的方法”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Spring-AOP 靜態(tài)普通方法名匹配切面的方法”吧!

概述

StaticMethodMatcherPointcutAdvisor代表一個(gè)靜態(tài)方法匹配切面,它通過(guò)StaticMethodMatcherPointcut來(lái)定義切點(diǎn),并通過(guò)類(lèi)過(guò)濾和方法名來(lái)匹配所定義的切點(diǎn).

實(shí)例

代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster

我們假設(shè)我們業(yè)務(wù)類(lèi)中 Waiter和 Seller中都有同名的greetTo()方法.

業(yè)務(wù)類(lèi)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ù)類(lèi)Seller

package com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor;
public class Seller {
 /**
  * 
  * 
  * @Title: greetTo
  * 
  * @Description: 和Waiter類(lèi)中的同名的方法,目的是為了驗(yàn)證僅僅織入了Waiter類(lèi)中的greetTo方法
  * 
  * @param name
  * 
  * @return: void
  */
 public void greetTo(String name) {
  System.out.println("Seller Greet to " + name);
 }
}

現(xiàn)在我們希望通過(guò)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: 切面類(lèi)
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年8月18日 下午8:27:52
 */
public class GreetingAdvisor extends StaticMethodMatcherPointcutAdvisor {
	private static final long serialVersionUID = 1L;
	/**
	 * 重寫(xiě)matches方法,切點(diǎn)方法匹配規(guī)則:方法名為greetTo
	 */
	@Override
	public boolean matches(Method method, Class<?> targetClass) {
		return "greetTo".equals(method.getName());
	}
	/**
	 * 默認(rèn)情況下,匹配所有的類(lèi),重寫(xiě)getClassFilter,定義匹配規(guī)則 切點(diǎn)類(lèi)型匹配規(guī)則,為Waiter的類(lèi)或者之類(lèi)
	 */
	public ClassFilter getClassFilter() {
		return new ClassFilter() {
			@Override
			public boolean matches(Class<?> clazz) {
				return Waiter.class.isAssignableFrom(clazz);
			}
		};
	}
}

StaticMethodMatcherPointcutAdvisor 抽象類(lèi)唯一需要定義的是matches()方法,在默認(rèn)情況下,該切面匹配所有的類(lèi),這里通過(guò)覆蓋getClassFilter()方法,讓它僅匹配Waiter類(lèi)及其子類(lèi)。

當(dāng)然,Advisor還需要一個(gè)增強(qiáng)類(lèi)的配合 .

我們來(lái)定義一個(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配置來(lái)定義切面等信息

<?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)類(lèi) -->
	<bean id="waiterTarget" class="com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor.Waiter"/>
	<!-- Seller目標(biāo)類(lèi) -->
	<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) -->
		
	<!-- 通過(guò)父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>

單元測(cè)試類(lèi)

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: 測(cè)試類(lèi)
 * 
 * @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é)果:

Spring-AOP 靜態(tài)普通方法名匹配切面的方法

我們可以看到切面僅僅織入了Wwaiter.greetTo()方法調(diào)用前的連接點(diǎn)上, Waiter.serverTo()和Seller.greetTo()方法并沒(méi)有織入切面。

到此,相信大家對(duì)“Spring-AOP 靜態(tài)普通方法名匹配切面的方法”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI