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

编程式的Pointcut

除了可以通过注解和Xml配置定义Pointcut之外,其实我们还可以通过程序来定义Pointcut。Spring Aop的切入点(Pointcut)对应于它的一个Pointcut接口,全称是org.springframework.aop.Pointcut。该接口的定义如下:

public interface Pointcut {

	ClassFilter getClassFilter();

	MethodMatcher getMethodMatcher();

	Pointcut TRUE = TruePointcut.INSTANCE;

}

该接口一共定义了两个核心方法,一个用于获取该Pointcut对应的过滤Class的ClassFilter对象,一个用于获取过滤Method的MethodMatcher对象。
ClassFilter接口的定义如下:

public interface ClassFilter {

	boolean matches(Class<?> clazz);

	ClassFilter TRUE = TrueClassFilter.INSTANCE;

}

该接口只定义了一个matches方法,用于判断指定的Class对象是否匹配当前的过滤规则。
MethodMatcher接口定义如下:

public interface MethodMatcher {

	boolean matches(Method method, Class<?> targetClass);

	boolean isRuntime();

	boolean matches(Method method, Class<?> targetClass, Object[] args);

	MethodMatcher TRUE = TrueMethodMatcher.INSTANCE;

}

该接口中一共定义了三个方法,两个matches方法,一个包含方法参数一个不包含。不包含方法参数的matches方法用于判断非运行时的方法匹配,比如只需要匹配方法名、方法参数定义的;包含方法参数值的matches方法用于运行时判断方法是否匹配,应用于需要根据方法传参来判断是否匹配的情况,但是该方法一般会在不包含方法参数的matches方法返回true和isRuntime()方法true的情形下才会调用。isRuntime()方法用于指定该Pointcut是否需要在运行时才能判断对应的方法是否匹配。

自定义Pointcut

以下是一个自定义Pointcut的代码,其将匹配所有的名称Service结尾的Class对应的名称以find开始的方法调用:

import java.lang.reflect.Method;

import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut; /**
* 自定义Pointcut
* @author Elim
* 2017年5月8日
*/
public class MyCustomPointcut implements Pointcut { @Override
public ClassFilter getClassFilter() {
return new MyCustomClassFilter();
} @Override
public MethodMatcher getMethodMatcher() {
return new MyCustomMethodMatcher();
} private static class MyCustomClassFilter implements ClassFilter { @Override
public boolean matches(Class<?> clazz) {
//实现自己的判断逻辑,这里简单的判断对应Class的名称是以Service结尾的就表示匹配
return clazz.getName().endsWith("Service");
} } private static class MyCustomMethodMatcher implements MethodMatcher { @Override
public boolean matches(Method method, Class<?> targetClass) {
//实现方法匹配逻辑
return method.getName().startsWith("find");
} @Override
public boolean isRuntime() {
return false;
} @Override
public boolean matches(Method method, Class<?> targetClass, Object[] args) {
return false;
} } }

然后我们可以定义该自定义Pointcut对应的bean,再定义一个Advisor将使用该Pointcut。如下示例中我们就指定了将在MyCustomPointcut对应的切入点处采用LogAroundAdvice。

 	<aop:config>
<aop:advisor advice-ref="logAroundAdvice" pointcut-ref="myCustomPointcut"/>
</aop:config>
<bean id="logAroundAdvice" class="com.elim.learn.spring.aop.advice.LogAroundAdvice"/>
<bean id="myCustomPointcut" class="com.elim.learn.spring.aop.pointcut.MyCustomPointcut"/>

继承自现有的Pointcut

除了可以完全实现Pointcut接口外,我们还可以直接使用Spring自带的Pointcut。比如基于固定方法的StaticMethodMatcherPointcut。该Pointcut是一个抽象类,在使用该Pointcut时只需要实现一个抽象方法matches(Method method, Class<?> targetClass),以下是一个继承自StaticMethodMatcherPointcut的示例类定义,该Pointcut将匹配所有Class中定义的方法名以find开头的方法。

public class FindMethodMatcherPointcut extends StaticMethodMatcherPointcut {

	@Override
public boolean matches(Method method, Class<?> targetClass) {
return method.getName().startsWith("find");
} }

关于更多Spring官方已经提供的其它Pointcut定义请参考Spring的API文档。

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

Spring Aop(十)——编程式的Pointcut的更多相关文章

  1. spring事务管理——编程式事务、声明式事务

    本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本教程假定您已经掌握了 ...

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

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

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

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

  4. Spring Aop(十六)——编程式的自定义Advisor

    转发:https://www.iteye.com/blog/elim-2399437 https://www.iteye.com/blogs/subjects/springaop 编程式的自定义Adv ...

  5. Spring框架——事务处理(编程式和声明式)

     一. 事务概述 ●在JavaEE企业级开发的应用领域,为了保证数据的完整性和一致性,必须引入数据库事务的概念,所以事务管理是企业级应用程序开发中必不可少的技术. ●事务就是一组由于逻辑上紧密关联而合 ...

  6. Spring aop 原始的工作原理的理解

    理解完aop的名词解释,继续学习spring aop的工作原理. 首先明确aop到底是什么东西?又如何不违单一原则并实现交叉处理呢? 如果对它的认识只停留在面向切面编程,那就脏了.从oop(Objec ...

  7. spring aop 样例

    基于注解的AOP 方式 1.加入jar包 com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver- ...

  8. Spring AOP 知识整理

    通过一个多月的 Spring AOP 的学习,掌握了 Spring AOP 的基本概念.AOP 是面向切面的编程(Aspect-Oriented Programming),是基于 OOP(面向对象的编 ...

  9. 深入理解TransactionTemplate编程式事务

    Spring可以支持编程式事务和声明式事务. Spring提供的最原始的事务管理方式是基于TransactionDefinition.PlatformTransactionManager.Transa ...

随机推荐

  1. Anton and Chess(模拟+思维)

    http://codeforces.com/group/1EzrFFyOc0/contest/734/problem/D 题意:就是给你一个很大的棋盘,给你一个白棋的位置还有n个黑棋的位置,问你黑棋能 ...

  2. HDU 6568 Math

    Math \(f_i\)为从\(i\)到\(i+1\)的期望步数. \(f_i = 1-p + p(f_i + 2((1-q)^{n-i}(n-i) + q\sum_{j=0}^{n-i-1}(1-q ...

  3. JAVA遇见HTML——Servlet篇:应用MVC架构实现项目

    java关键字“this”只能用在方法方法体内.当一个对象创建之后,java虚拟机就会给这个对象分配一个引用自身的指针,这个指针的名字就是this.只能在非静态方法中使用 package servle ...

  4. JAVA遇见HTML——JSP篇(1、JAVA WEB简介)

    比如淘宝.新浪.搜狐.网易就是Web应用程序

  5. 2018 开始认真学习点python

    2018 伊始,又是春暖花开.俗语,“一年之计在于春”.又是一年立志时. 决定认真学习一些web. 本来倾向与学习NodeJS的.可是之前买的python的书太多了.就先紧手头的资源看了再说吧. 今天 ...

  6. ODBC连接到400

    1.首先iSeries Client安装的时候要勾选ODBC , 这样才能找到Driver 2.某个Application是32位上,要用32位路径下的ODBC Administration打开,添加 ...

  7. AT3913 XOR Tree(巧妙转换+状压dp)

    Step1:首先定义一个点的权值为与其相连边的异或和.那么修改一条路径,权值改变的只有两个端点.边权都为0和点权都为0实质相同. Step2:那么现在和树的结构就没有什么关系了.每次选两个点,然后同时 ...

  8. windows游戏编程地址

    本系列文章由jadeshu编写,转载请注明出处.http://blog.csdn.net/jadeshu/article/details/22309325 作者:jadeshu   邮箱: jades ...

  9. 进入docker 容器命令行

    #!/bin/bash CNAME=$1 CPID=$(docker inspect --format "{{.State.Pid}}" $CNAME) nsenter --tar ...

  10. List对象遍历时null判断逻辑梳理

          凡是对集合list,set,map,数组等进行循环一定要判断是否为null,增强代码的健壮性.下面以list为例, 使用for循环遍历list对象,处理其中的元素时,需要对null值判断: ...