编程式的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面向方面编程原理Spring AOP面向方面编程原理Spring AOP面向方面编程原理Spring AOP面向方面编程原理Spring AOP面向方面编程原理Spring AOP面向方面编程原理Spring AOP面向方面编程原理
spring AOP切面编程 运行环境:eclipse 构建工具:maven 不提供maven构建,maven用来解决jar包的依赖
Spring AOP面向方面编程原理:AOP概念,主要介绍面向对象的概念及原理,及作者的一些理解。
学习Spring开发的AOP面向切面编程时所需要的jar包,包括com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
死磕Spring之AOP篇 - Spring AOP两种代理对象的拦截处理(csdn)————程序
spring aop jar 包
NULL 博文链接:https://ylxy3058.iteye.com/blog/2224244
java Spring aop面向方面编程.rar
spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop
描述一下Spring AOP? 在Spring AOP中关注点(concern)和横切关注点(cross-cutting concern)有什么不同? AOP有哪些可用的实现? Spring中有哪些不同的通知类型(advice types)? Spring AOP 代理是什么? 引介...
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
一、适合人群 1、具备一定Java编程基础,初级开发者 2、对springboot,mybatis,mysql有基本认识 3、对spring aop认识模糊的,不清楚如何实现Java 自定义注解的 ...4、spring boot,mybatis,druid,spring aop的使用
小马哥讲 Spring AOP 编程思想 - API 线索图.pdf
Spring框架的关键组件之一是面向方面编程(AOP)框架。 面向方面的编程需要将程序逻辑分解成不同的部分。 此教程将通过简单实用的方法来学习Spring框架提供的AOP/面向方面编程。
基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop
Spring AOP 是基于 AOP 编程模式的一个框架,它的使用有效减少了系统间的重复代码,达到了模块间的松耦合目的。AOP 的全称是“Aspect Oriented Programming”,即面向切面编程,它将业务逻辑的各个部分进行隔离,使...
最近公司要求和别的公司对接,但是不想破坏原有的架构,就只有用切面了,百度了下都不全面,就自己写了个小demo,只要导入spring4.2.3的jar就直接可以用,注解实现spingaop机制,1分意思下
Spring 动态代理和aop切面编程例子,自己写的例子!!!!!!!
配套教材,里面的代码测试过都是可运行的,配套学习很方便
NULL 博文链接:https://sunzone.iteye.com/blog/1967543