Spring Aop(九)——基于正则表达式的Pointcut
转发地址:https://www.iteye.com/blog/elim-2396525
基于正则表达式的Pointcut
JdkRegexpMethodPointcut
Spring官方为我们提供了一个基于正则表达式来匹配方法名的Pointcut,JdkRegexpMethodPointcut。该Pointcut是继承自StaticMethodMatcherPointcut的。我们在定义JdkRegexpMethodPointcut时可以通过patterns和excludedPatterns来注入需要满足和排除的正则表达式,它们对应的都是一个String[]。比如我们想匹配所有的方法名以find开头的方法,我们可以如下定义:
<bean id="regexPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="patterns">
<list>
<value>find.*</value><!-- 所有方法名以find开始的方法 -->
</list>
</property>
</bean>
如果我们需要匹配或需要排除的正则表达式只是单一的一个正则表达式,那么我们也可以通过pattern和excludedPattern来指定单一的需要匹配和排除的正则表达式。需要注意的是patterns和pattern不能同时使用,excludedPattern和excludedPatterns也是一样的。当我们同时指定了patterns和excludedPatterns时,该Pointcut将先匹配patterns,对于能够匹配patterns的将再判断其是否在excludedPatterns中,如果存在也将不匹配。以下是该匹配逻辑的核心代码。
	protected boolean matchesPattern(String signatureString) {
		for (int i = 0; i < this.patterns.length; i++) {
			boolean matched = matches(signatureString, i);
			if (matched) {
				for (int j = 0; j < this.excludedPatterns.length; j++) {
					boolean excluded = matchesExclusion(signatureString, j);
					if (excluded) {
						return false;
					}
				}
				return true;
			}
		}
		return false;
	}
需要说明的是在上面的匹配逻辑中传递的参数signatureString是对应方法的全路径名称,即包含该方法的类的全路径及该方法的名称,如“org.springframework.aop.support.JdkRegexpMethodPointcut.matches”这种,所以如果我们需要在使用正则表达式定义Pointcut时,也可以匹配某某类的某某方法这种形式。
RegexpMethodPointcutAdvisor
使用了JdkRegexpMethodPointcut后,我们在使用的时候通常会进行如下配置。
<aop:config>
<aop:advisor advice-ref="logBeforeAdvice" pointcut-ref="regexPointcut"/>
</aop:config>
<bean id="logBeforeAdvice" class="com.elim.learn.spring.aop.advice.LogBeforeAdvice" />
<bean id="regexPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="patterns">
<list>
<value>find.*</value><!-- 所有方法名以find开始的方法 -->
</list>
</property>
</bean>
其实针对于JdkRegexpMethodPointcut,Spring为我们提供了一个简便的Advisor定义,可以让我们同时指定一个JdkRegexpMethodPointcut和其需要对应的Advice,那就是RegexpMethodPointcutAdvisor,我们可以给它注入一个Advice和对应需要匹配的正则表达式(pattern或patterns注入)。
<bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="logBeforeAdvice"/>
<property name="pattern" value="find.*"/>
</bean>
需要注意的是
RegexpMethodPointcutAdvisor没有提供不匹配的正则表达式注入方法,即没有excludedPattern和excludedPatterns注入,如果需要该功能请还是使用JdkRegexpMethodPointcut。
(本文是基于Spring4.1.0所写,Elim写于2017年5月8日)
Spring Aop(九)——基于正则表达式的Pointcut的更多相关文章
- Spring AOP中定义切点(PointCut)和通知(Advice)
		如果你还不熟悉AOP,请先看AOP基本原理,本文的例子也沿用了AOP基本原理中的例子.切点表达式 切点的功能是指出切面的通知应该从哪里织入应用的执行流.切面只能织入公共方法.在Spring AOP中, ... 
- Spring AOP表达式报错:Pointcut is not well-formed: expecting 'name pattern' at character position
		问题现象: java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test ... 
- SSM框架—Spring AOP之基于注解的声明式AspectJ(Demo)
		项目结构 XML <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http ... 
- 基于注解的Spring AOP的配置和使用
		摘要: 基于注解的Spring AOP的配置和使用 AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不 ... 
- 基于注解的Spring AOP的配置和使用--转载
		AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ... 
- Spring Aop(七)——基于XML配置的Spring Aop
		转发:https://www.iteye.com/blog/elim-2396043 7 基于XML配置的Spring AOP 基于XML配置的Spring AOP需要引入AOP配置的Schema,然 ... 
- Spring Aop(三)——Pointcut表达式介绍
		转发地址:https://www.iteye.com/blog/elim-2395255 3 Pointcut表达式介绍 3.1 表达式类型 标准的Aspectj Aop的pointcut的表达式类型 ... 
- Spring Aop(二)——基于Aspectj注解的Spring Aop简单实现
		转发地址:https://www.iteye.com/blog/elim-2394762 2 基于Aspectj注解的Spring Aop简单实现 Spring Aop是基于Aop框架Aspectj实 ... 
- spring AOP  AspectJ 定义切面实现拦截
		总结记录一下AOP常用的应用场景及使用方式,如有错误,请留言. 1. 讲AOP之前,先来总结web项目的几种拦截方式 A: 过滤器 使用过滤器可以过滤URL请求,以及请求和响应的信息,但是过 ... 
随机推荐
- Hibernate初探之单表映射——第二章:Hibernate进阶
			第二章:Hibernate进阶 1.hibernate.cfg.xml常用配置 2.session 简介 3.transaction简介 4.session详解 5.对象关系映射常用配置 1.hibe ... 
- 2019牛客多校E Androgynos——自补图&&构造
			题目 给出一个 $n$,判断是否存在 $n$ 个顶点的自补图,如果存在,输出边和映射. 分析 一个无向图若同构于它的补图,则称该图为自补图. 定理:一个自补图一定存在 $4k$ 或 $4k+1$ 个顶 ... 
- FastDFS+Nginx+Module
			1.安装libevent wget https://cloud.github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.g ... 
- [Flutter] Stack Layout
			Normally if you place three the same size icons in a stack, they will stands on top of each other, t ... 
- [Functional Programming] Using ComposeK for both get State and modify State
			We have State like this: const state = { cards: [ { id: "green-square", color: "green ... 
- Appium自动化测试教程-自学网-安卓模拟器
			安卓模拟器: 夜神模拟器安装配置 下载地址:https://www.yeshen.com 开启VT VT是什么?为什么要开启VT? VT,全称是Virtualization Technology,即是 ... 
- learning armbian steps(5)   ----- armbian 构建arm rootfs
			基于learning armbian step(4) 的总结,我们来实践一下,接下来的会把整个构建的log都贴出来: vmuser@vmuser-virtual-machine:~/qemu-arm$ ... 
- poj 2762
			Tarjan + TopsortTarjan 缩点Topsort 判断 Topsort 判断:在DAG中若初始状态下存在多于1个入度为0的点则说明这些 入度为0的点之间不会有路径可达若不存在入度为0的 ... 
- [Luogu] 均分数据
			题面:https://www.luogu.org/problemnew/show/P2503 题解:https://www.zybuluo.com/wsndy-xx/note/1141736 
- Gym - 102307G Graduation 拓扑排序
			Gym - 102307G Graduation 题意:xjl得修够n门课才能毕业,其中有些课是某门课的先行课,并且他精力有限,每学期最多只能修k门课,问xjl最少需要多少学期才能毕业. 首先,正向 ... 
