转发地址:https://www.iteye.com/blog/elim-2396525

基于正则表达式的Pointcut

JdkRegexpMethodPointcut

Spring官方为我们提供了一个基于正则表达式来匹配方法名的Pointcut,JdkRegexpMethodPointcut。该Pointcut是继承自StaticMethodMatcherPointcut的。我们在定义JdkRegexpMethodPointcut时可以通过patternsexcludedPatterns来注入需要满足和排除的正则表达式,它们对应的都是一个String[]。比如我们想匹配所有的方法名以find开头的方法,我们可以如下定义:

  1. <bean id="regexPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
  2. <property name="patterns">
  3. <list>
  4. <value>find.*</value><!-- 所有方法名以find开始的方法 -->
  5. </list>
  6. </property>
  7. </bean>

如果我们需要匹配或需要排除的正则表达式只是单一的一个正则表达式,那么我们也可以通过patternexcludedPattern来指定单一的需要匹配和排除的正则表达式。需要注意的是patternspattern不能同时使用,excludedPatternexcludedPatterns也是一样的。当我们同时指定了patternsexcludedPatterns时,该Pointcut将先匹配patterns,对于能够匹配patterns的将再判断其是否在excludedPatterns中,如果存在也将不匹配。以下是该匹配逻辑的核心代码。

  1. protected boolean matchesPattern(String signatureString) {
  2. for (int i = 0; i < this.patterns.length; i++) {
  3. boolean matched = matches(signatureString, i);
  4. if (matched) {
  5. for (int j = 0; j < this.excludedPatterns.length; j++) {
  6. boolean excluded = matchesExclusion(signatureString, j);
  7. if (excluded) {
  8. return false;
  9. }
  10. }
  11. return true;
  12. }
  13. }
  14. return false;
  15. }

需要说明的是在上面的匹配逻辑中传递的参数signatureString是对应方法的全路径名称,即包含该方法的类的全路径及该方法的名称,如“org.springframework.aop.support.JdkRegexpMethodPointcut.matches”这种,所以如果我们需要在使用正则表达式定义Pointcut时,也可以匹配某某类的某某方法这种形式。

RegexpMethodPointcutAdvisor

使用了JdkRegexpMethodPointcut后,我们在使用的时候通常会进行如下配置。

  1. <aop:config>
  2. <aop:advisor advice-ref="logBeforeAdvice" pointcut-ref="regexPointcut"/>
  3. </aop:config>
  4. <bean id="logBeforeAdvice" class="com.elim.learn.spring.aop.advice.LogBeforeAdvice" />
  5. <bean id="regexPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
  6. <property name="patterns">
  7. <list>
  8. <value>find.*</value><!-- 所有方法名以find开始的方法 -->
  9. </list>
  10. </property>
  11. </bean>

其实针对于JdkRegexpMethodPointcut,Spring为我们提供了一个简便的Advisor定义,可以让我们同时指定一个JdkRegexpMethodPointcut和其需要对应的Advice,那就是RegexpMethodPointcutAdvisor,我们可以给它注入一个Advice和对应需要匹配的正则表达式(pattern或patterns注入)。

  1. <bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
  2. <property name="advice" ref="logBeforeAdvice"/>
  3. <property name="pattern" value="find.*"/>
  4. </bean>

需要注意的是RegexpMethodPointcutAdvisor没有提供不匹配的正则表达式注入方法,即没有excludedPattern和excludedPatterns注入,如果需要该功能请还是使用JdkRegexpMethodPointcut

(本文是基于Spring4.1.0所写,Elim写于2017年5月8日)

Spring Aop(九)——基于正则表达式的Pointcut的更多相关文章

  1. Spring AOP中定义切点(PointCut)和通知(Advice)

    如果你还不熟悉AOP,请先看AOP基本原理,本文的例子也沿用了AOP基本原理中的例子.切点表达式 切点的功能是指出切面的通知应该从哪里织入应用的执行流.切面只能织入公共方法.在Spring AOP中, ...

  2. Spring AOP表达式报错:Pointcut is not well-formed: expecting 'name pattern' at character position

    问题现象: java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test ...

  3. SSM框架—Spring AOP之基于注解的声明式AspectJ(Demo)

    项目结构 XML <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http ...

  4. 基于注解的Spring AOP的配置和使用

    摘要: 基于注解的Spring AOP的配置和使用 AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不 ...

  5. 基于注解的Spring AOP的配置和使用--转载

    AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...

  6. Spring Aop(七)——基于XML配置的Spring Aop

    转发:https://www.iteye.com/blog/elim-2396043 7 基于XML配置的Spring AOP 基于XML配置的Spring AOP需要引入AOP配置的Schema,然 ...

  7. Spring Aop(三)——Pointcut表达式介绍

    转发地址:https://www.iteye.com/blog/elim-2395255 3 Pointcut表达式介绍 3.1 表达式类型 标准的Aspectj Aop的pointcut的表达式类型 ...

  8. Spring Aop(二)——基于Aspectj注解的Spring Aop简单实现

    转发地址:https://www.iteye.com/blog/elim-2394762 2 基于Aspectj注解的Spring Aop简单实现 Spring Aop是基于Aop框架Aspectj实 ...

  9. spring AOP AspectJ 定义切面实现拦截

    总结记录一下AOP常用的应用场景及使用方式,如有错误,请留言. 1.  讲AOP之前,先来总结web项目的几种拦截方式    A:  过滤器 使用过滤器可以过滤URL请求,以及请求和响应的信息,但是过 ...

随机推荐

  1. go deep copy map

    func deepCopyJSON(src map[string]interface{}, dest map[string]interface{}) error { if src == nil { r ...

  2. Pycharm----破解码的获取

    网站:http://idea.lanyus.com/ 复制后,粘贴到pycharm中的激活即可

  3. 25-SQLServer中的DMV和DMF的使用

    一.总结 1.什么事DMV和DMFDMV(Dynamic Management View):动态管理视图DMF(Dynamic Management Function):动态管理函数 二.操作步骤 1 ...

  4. android studio调试报错:java.lang.RuntimeException: Unable to start activity ComponentInfo

    报错信息: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pro_u_loc/com.e ...

  5. P4149 [IOI2011]Race 点分治

    思路: 点分治 提交:5次 题解: 刚开始用排序+双指针写的,但是调了一晚上,总是有两个点过不了,第二天发现原因是排序时的\(cmp\)函数写错了:如果对于路径长度相同的,我们从小往大按边数排序,当双 ...

  6. Luogu P2151 [SDOI2009]HH去散步 矩乘加速DP

    思路:矩乘优化DP 提交:3次(用了一个奇怪的东西导致常数过大) 题解: 如果可以走完正向边后又走反向边那就显然了,但是不能走,所以我们要将正反向边分别编号,区分正反向边. 所以这道题的矩阵是以边的编 ...

  7. PHP mysqli_connect_errno() 函数

    返回上一次连接错误的错误代码: <?php $con=mysqli_connect("localhost","wrong_user","my_p ...

  8. 对象(Object)相关

    详情参考 1.对象的表示方法 js原生提供Object构造函数.js中所有的对象都是Object的实例. 定义一个对象最简单的就是var obj = {}; ES6属性和方法允许简写.对象的super ...

  9. vue编辑、新增弹框(引用外部页面)

    vue编辑.新增弹框(引用外部页面) 2018年06月15日 09:37:20 会飞的猪biubiu 阅读数 10265    版权声明:本文为博主原创文章,未经博主允许不得转载. https://b ...

  10. web批量下载文件到本地

    JavaWeb 文件下载功能 文件下载的实质就是文件拷贝,将文件从服务器端拷贝到浏览器端,所以文件下载需要IO技术将服务器端的文件读取到,然后写到response缓冲区中,然后再下载到个人客户端. 1 ...