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请求,以及请求和响应的信息,但是过 ...
随机推荐
- jpa 如果返回java对象,会自动更新对象值(坑!!)
//上一段示例代码List<Member> memberList = memberDao.findByLoginNameIn(names);for (Member m : memberLi ...
- 09—mybatis注解配置join查询
今天来聊mybatis的join查询,怎么说呢,有的时候,join查询确实能提升查询效率,今天举个left join的例子,来看看mybatis的join查询. 就不写的很细了,把主要代码贴出来了. ...
- Java集合--Map架构
概要 前面,我们已经系统的对List进行了学习.接下来,我们先学习Map,然后再学习Set:因为Set的实现类都是基于Map来实现的(如,HashSet是通过HashMap实现的,TreeSet是通过 ...
- Python sleep()函数用法:线程睡眠
如果需要让当前正在执行的线程暂停一段时间,并进入阻塞状态,则可以通过调用 time 模块的 sleep(secs) 函数来实现.该函数可指定一个 secs 参数,用于指定线程阻塞多少秒. 当前线程调用 ...
- linux 优化
如何优化Linux系统? 1)不用root超级用户登录,添加普通用户,通过sudo授权管理:/etc/sudoers 2)更改默认的远程连接ssh服务端口号,禁止root用户远程登录到服务器:/etc ...
- 矩阵库Numpy基本操作
NumPy是一个关于矩阵运算的库,熟悉Matlab的都应该清楚,这个库就是让python能够进行矩阵话的操作,而不用去写循环操作. 下面对numpy中的操作进行总结. numpy包含两种基本的数据类型 ...
- Google-Guava Concurrent包里的Service框架浅析
原文地址 译文地址 译者:何一昕 校对:方腾飞 概述 Guava包里的Service接口用于封装一个服务对象的运行状态.包括start和stop等方法.例如web服务器,RPC服务器.计时器等可以实 ...
- Node多国语言包
Via:https://github.com/caouecs/Laravel-lang 1.下载:https://github.com/caouecs/laravel-lang/archive/mas ...
- Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) A题
A. Optimal Currency ExchangeAndrew was very excited to participate in Olympiad of Metropolises. Days ...
- 离线语音Snowboy热词唤醒+ 树莓派语音交互实现开关灯
离线语音Snowboy热词唤醒 语音识别现在有非常广泛的应用场景,如手机的语音助手,智能音响(小爱,叮咚,天猫精灵...)等. 语音识别一般包含三个阶段:热词唤醒,语音录入,识别和逻辑控制阶段. 热词 ...