欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2021年07月17日 17:01:02   作者:小小工匠  
這篇文章主要介紹了Spring-AOP 靜態(tài)普通方法名匹配切面操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

概述

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é)果:

這里寫(xiě)圖片描述

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

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Lombok使用@Tolerate實(shí)現(xiàn)沖突兼容問(wèn)題

    Lombok使用@Tolerate實(shí)現(xiàn)沖突兼容問(wèn)題

    這篇文章主要介紹了Lombok使用@Tolerate實(shí)現(xiàn)沖突兼容問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Java 爬蟲(chóng)工具Jsoup詳解

    Java 爬蟲(chóng)工具Jsoup詳解

    這篇文章主要介紹了 Java 爬蟲(chóng)工具Jsoup詳解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • 淺談JAVA并發(fā)之ReentrantLock

    淺談JAVA并發(fā)之ReentrantLock

    本文主要介紹了基于AQS實(shí)現(xiàn)的ReentrantLock(重入鎖),感興趣的同學(xué),可以參考下。
    2021-06-06
  • java并發(fā)編程JUC CountDownLatch線程同步

    java并發(fā)編程JUC CountDownLatch線程同步

    這篇文章主要介紹CountDownLatch是什么、CountDownLatch 如何工作、CountDownLatch 的代碼例子來(lái)展開(kāi)對(duì)java并發(fā)編程JUC CountDownLatch線程同步,需要的朋友可以參考下面文章內(nèi)容
    2021-09-09
  • Springboot?配置線程池創(chuàng)建線程及配置?@Async?異步操作線程池詳解

    Springboot?配置線程池創(chuàng)建線程及配置?@Async?異步操作線程池詳解

    這篇文章主要介紹了Springboot?配置線程池創(chuàng)建線程及配置?@Async?異步操作線程池詳解,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • jdbc和mybatis的流式查詢(xún)使用方法

    jdbc和mybatis的流式查詢(xún)使用方法

    有些時(shí)候我們所需要查詢(xún)的數(shù)據(jù)量比較大,但是jvm內(nèi)存又是有限制的,數(shù)據(jù)量過(guò)大會(huì)導(dǎo)致內(nèi)存溢出。這個(gè)時(shí)候就可以使用流式查詢(xún),本文就主要介紹了jdbc和mybatis的流式查詢(xún),感興趣的可以了解一下
    2021-11-11
  • Java循環(huán)對(duì)bean的屬性進(jìn)行賦值的實(shí)現(xiàn)

    Java循環(huán)對(duì)bean的屬性進(jìn)行賦值的實(shí)現(xiàn)

    本文主要介紹了Java循環(huán)對(duì)bean的屬性進(jìn)行賦值,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 解決springboot配置文件組解決自動(dòng)配置屬性無(wú)法注入問(wèn)題

    解決springboot配置文件組解決自動(dòng)配置屬性無(wú)法注入問(wèn)題

    在使用Spring Boot時(shí),可能會(huì)遇到配置文件屬性注入失敗的問(wèn)題,本文描述了一個(gè)案例,其中嘗試使用profile文件組指定不同環(huán)境下的配置文件,但遇到了屬性無(wú)法成功注入的情況,提供的解決辦法是將Spring Boot的版本號(hào)從2.2.0.RELEASE升級(jí)到2.4.0
    2024-09-09
  • Java可以寫(xiě)android的應(yīng)用程序嗎

    Java可以寫(xiě)android的應(yīng)用程序嗎

    在本篇文章里小編給大家整理的是一篇關(guān)于Java可以寫(xiě)android的應(yīng)用程序嗎的相關(guān)基礎(chǔ)文章,有興趣的朋友們可以學(xué)習(xí)下。
    2020-11-11
  • Java實(shí)現(xiàn)圖片比對(duì)算法

    Java實(shí)現(xiàn)圖片比對(duì)算法

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)圖片比對(duì)算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04

最新評(píng)論