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请求,以及请求和响应的信息,但是过 ...
随机推荐
- 2018/7/31-zznuoj-问题 A: A + B 普拉斯【二维字符串+暴力模拟+考虑瑕疵的题意-0的特例】
问题 A: A + B 普拉斯 在计算机中,数字是通过像01像素矩阵来显示的,最终的显示效果如下: 现在我们用01来构成这些数字 当宝儿姐输入A + B 时(log10(A)<50,log10 ...
- Python网络爬虫四大选择器(正则表达式、BS4、Xpath、CSS)总结
一.正则表达式 正则表达式为我们提供了抓取数据的快捷方式.虽然该正则表达式更容易适应未来变化,但又存在难以构造.可读性差的问题.当在爬京东网的时候,正则表达式如下图所示: 此外 ,我们都知道,网页时常 ...
- jQuery——jQuery对象与DOM对象
1.jQuery对象与DOM对象的区别 通过jQuery方法包装后的对象,是一个类数组对象.它与DOM对象完全不同,唯一相似的是它们都能操作DOM. 通过jQuery处理DOM的操作,可以让开发者更专 ...
- C# IFormattable 接口重写
public class Racer : IComparable<Racer>, IFormattable { public int Id { get; private set; } pu ...
- 【专题】Spring Boot 2.x 面试题
1.Spring Boot.Spring MVC 和 Spring 有什么区别? SpringFramework 最重要的特征是依赖注入.所有 SpringModules 不是依赖注入就是 IOC 控 ...
- Vivado RAM使用
RAM使用的几点说明: 1,RAM的读写位宽可以不同,举例:写的位宽为8(1Byte),读的位宽为1(1bit),那么读的地址就变成了写地址的8倍,即位宽增加3bit.
- python3.5+win7 安装 numpy 和scipy的总结
1.安装numpy. 官网下载numpy.下载地址为https://pypi.Python.org/pypi/numpy.选择与Python版本相对应的numpy版本.之后在python35 ...
- mfc编程之发送wm_paint消息时绘图界面只出现一瞬间
实现的功能是在打开一个文件然后把文件的图形信息绘制在picture控件上. 问题描述:我把绘制的操作放在了窗口中onpaint()函数里,打开文件后发送一个wm_paint的消息,发现picture控 ...
- array_merge与array+array的区别
结果:
- 观察者模式在android网络监控下的运用
github:https://github.com/shonegg/NetMonitor 一.对观察者模式的理解: 1.观察者模式,又叫发布-订阅(Publish/Subscribe)模式,定义的是对 ...