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

Spring-AOP 靜態(tài)正則表達(dá)式方法如何匹配切面

 更新時間:2021年07月19日 10:45:43   作者:小小工匠  
這篇文章主要介紹了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),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中線程的等待與喚醒_動力節(jié)點(diǎn)Java學(xué)院整理

    Java中線程的等待與喚醒_動力節(jié)點(diǎn)Java學(xué)院整理

    在Object.java中,定義了wait(), notify()和notifyAll()等接口。wait()的作用是讓當(dāng)前線程進(jìn)入等待狀態(tài),同時,wait()也會讓當(dāng)前線程釋放它所持有的鎖。下面通過本文給大家介紹Java中線程的等待與喚醒知識,感興趣的朋友一起看看吧
    2017-05-05
  • SpringMVC中MultipartFile轉(zhuǎn)File的兩種方式

    SpringMVC中MultipartFile轉(zhuǎn)File的兩種方式

    在spring上傳文件中,一般都使用了MultipartFile來接收,但是有需要用到File的地方,本文主要介紹了SpringMVC中MultipartFile轉(zhuǎn)File的兩種方式,感興趣的可以了解一下
    2022-04-04
  • Mybatis常用標(biāo)簽整理

    Mybatis常用標(biāo)簽整理

    日常開發(fā)中,MyBatis中標(biāo)簽有著舉足輕重的重要性,以下是一些MyBatis框架中常見的標(biāo)簽及案例,感興趣的朋友跟隨小編一起看看吧
    2007-02-02
  • JavaApi實(shí)現(xiàn)更新刪除及讀取節(jié)點(diǎn)

    JavaApi實(shí)現(xiàn)更新刪除及讀取節(jié)點(diǎn)

    這篇文章主要介紹了JavaApi實(shí)現(xiàn)更新刪除及讀取節(jié)點(diǎn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • SpringBoot?Test的webEnvironment源碼解讀

    SpringBoot?Test的webEnvironment源碼解讀

    這篇文章主要為大家介紹了SpringBoot?Test的webEnvironment源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • Java輕松實(shí)現(xiàn)批量插入或刪除Excel行列操作

    Java輕松實(shí)現(xiàn)批量插入或刪除Excel行列操作

    在職場生活中,對Excel工作表的行和列進(jìn)行操作是非常普遍的需求,下面小編就來和大家介紹一下如何在Java中完成批量插入、刪除行和列的操作吧
    2023-10-10
  • SpringBoot實(shí)現(xiàn)文件下載功能的方式分享

    SpringBoot實(shí)現(xiàn)文件下載功能的方式分享

    這篇文章主要為大家詳細(xì)介紹了SpringBoot這哪個實(shí)現(xiàn)文件下載功能的幾種方式,文中的實(shí)現(xiàn)方法簡介易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-03-03
  • Java Jwt庫的簡介及使用詳解

    Java Jwt庫的簡介及使用詳解

    JWT 是開放的行業(yè)標(biāo)準(zhǔn)RFC7591,用來實(shí)現(xiàn)端到端安全驗(yàn)證,就是通過一些算法對加密字符串和JSON對象之間進(jìn)行加解密,下面通過本文給大家介紹Java Jwt庫的簡介及使用,感興趣的朋友一起看看吧
    2021-11-11
  • Spring boot集成Go-FastDFS實(shí)現(xiàn)圖片上傳刪除等功能實(shí)現(xiàn)

    Spring 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-04
  • SpringCloud搭建netflix-eureka微服務(wù)集群的過程詳解

    SpringCloud搭建netflix-eureka微服務(wù)集群的過程詳解

    這篇文章主要介紹了SpringCloud搭建netflix-eureka微服務(wù)集群的過程詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04

最新評論