Spring AOP 的底层实现机制
2. Spring AOP 中的 Pointcut
6. 扩展 Pointcut
如何前面的 Pointcut 类型都无法满足要求,这种情况下可以扩展 Spring AOP 的 Pointcut ,给出自定义的 Pointcut。
要自定义 Pointcut ,Spring AOP 已经提供了相应的扩展抽象支持,我们只需要继承相应的抽象父类,然后实现或者覆写
方法逻辑即可。
Spring AOP 的 Pointcut 类型可以划分为 StaticMethodMatcherPointcut 和 DynamicMethodMatcherPointcut
自定义 Pointcut 只需要在这两个抽象类的基础上实现相应子类即可。
a. StaticMethodMatcherPointcut
package org.springframework.aop.support;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;
/**
* 因为是 StaticMethodMatcher,所以其 MethodMatcher 的 isRuntime 方法返回 false,
* 同时三个参数的 matches 方法抛出 UnsupportedOperationException 异常,
* 以表示该方法不应该被调用到。(该部分在 抽象父类 StaticMethodMatcher 中实现)
*
*/
public abstract class StaticMethodMatcherPointcut extends StaticMethodMatcher implements Pointcut {
private ClassFilter classFilter = ClassFilter.TRUE;
/**
* 默认子类的 ClassFilter 均为 ClassFilter.TRUE,即忽略类的类型匹配。
*/
public ClassFilter getClassFilter() {
return this.classFilter;
}
/**
* 如果子类需要对目标对象的类型做进一步的限制,可以通过该方法设置相应的 ClassFilter 实现
* @param classFilter
*/
public void setClassFilter(ClassFilter classFilter) {
this.classFilter = classFilter;
}
public final MethodMatcher getMethodMatcher() {
return this;
}
}
最终实现 自定义的StaticMethodMatcherPointcut 只需要实现两个参数的 matches 方法即可。
例如:提供一个 Pointcut , 用来扑捉系统中数据访问对象中的查询方法。
package prx.aop.pointcut;
import java.lang.reflect.Method;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
public class QueryMethodPointcut extends StaticMethodMatcherPointcut {
public boolean matches(Method method, Class<?> targetClass) {
return method.getName().startsWith("query")
&& targetClass.getPackage().getName().contains("dao");
}
}
b. DynamicMethodMatcherPointcut
package org.springframework.aop.support;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;
/**
* 因为是 DynamicMethodMatcher,所以其 MethodMatcher 的 isRuntime 方法返回 true,
* 同时两个参数的 matches 也返回 true,以便三个参数的方法顺利执行
* (该部分在 抽象父类 DynamicMethodMatcher 中实现)
*/
public abstract class DynamicMethodMatcherPointcut extends DynamicMethodMatcher implements Pointcut {
/**
* 默认子类的 ClassFilter 均为 ClassFilter.TRUE,即忽略类的类型匹配。
* 如果需要特定的目标对象类型限定,需要覆盖这个方法。
*/
public ClassFilter getClassFilter() {
return ClassFilter.TRUE;
}
public final MethodMatcher getMethodMatcher() {
return this;
}
}
最终实现自定义的 DynamicMethodMatcherPointcut 只需要实现三个参数的 matches 方法即可。
例如:有个查询只有 Boss 才能访问。
package prx.aop.pointcut;
import java.lang.reflect.Method;
import org.springframework.aop.support.DynamicMethodMatcherPointcut;
public class BossQueryMethodPointcut extends DynamicMethodMatcherPointcut {
public boolean matches(Method method, Class<?> targetClass, Object[] args) {
if(method.getName().startsWith("query")
&& targetClass.getPackage().getName().contains("dao")) {
if(args != null && args.length > 1) {
return "Boss".equals(args[0]);
}
}
return false;
}
}
如果愿意,也可以覆盖 两个参数的 matches 方法,这样,不用每次都得到三个参数的 matches 方法执行的时候才检查
所有的条件。
将 Pointcut 加入 IoC 容器中
选择好了 Pointcut ,剩下就是 将它们加入Spring IoC 容器中,以便 Spring 管理。 Spring 中的 Pointcut 实现都是
普通的 Java 对象, 所以想普通的 POJO 那样配置注入就可以了。
<bean id="nameMatchPointcut" class="org.springframework.aop.support.NameMatchMethodPointcut">
<property name="mappedNames">
<list>
<value>methodName1</value>
<value>methodName2</value>
</list>
</property>
</bean>
- 大小: 28.2 KB
分享到:
相关推荐
AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析...
springAOP核心源码流程分析,请基于5.0.6版本进行代码跟进。
spring aop框架源码级分析 结构图
Spring 框架之 AOP 原理剖析.html 抓下来打包成了HTML文件, 方便离线观看
有关于Spring,我们最常用的两个功能就是IOC和AOP,前几篇文章从源码级别介绍了Spring容器如何为我们生成bean及bean之间的依赖关系 下面我们接着来看AOP的源码实现。 有关于AOP,我们在面试中也被无数次问到...
springAOP核心源码流程分析,请基于5.0.6版本进行代码跟进。
Spring AOP(面向切面编程)作为Spring框架的一个重要部分,为Java开发者提供了一个...通过对Spring AOP源码的深入分析,我们可以更好地理解其内部工作原理,从而在实际开发中更加灵活和高效地使用这一强大的编程范式。
NULL 博文链接:https://ylxy3058.iteye.com/blog/2224244
Spring AOP 业务代码
本文深入分析了Spring AOP的实现机制,让读者能够更好地理解和应用这一强大的编程范式。 Spring AOP是基于代理模式实现的,主要包括动态代理、通知(Advice)、切点(Pointcut)、切面(Aspect)和连接点(Join ...
Java进阶之SpringAOP源码深度剖析共17页.pdf.zip
面试必考之HashMap源码分析与实现 探索JVM底层奥秘ClassLoader源码分析与案例讲解 面试必备技能之Dubbo企业实战 ...互联网系统垂直架构之Session解决方案 分库分表之后分布式下如何...无处不在的Spring AOP事务及踩过的坑
SpringAOP入门和原理分析,动态代理
元注解方式结合AOP,灵活记录操作日志 能够记录详细错误日志为运维提供支持 日志记录尽可能减少性能影响
Spring源代码分析(三):Spring JDBC Spring源代码解析(四):Spring MVC Spring源代码解析(五):Spring AOP获取Proxy Spring源代码解析(六):Spring声明式事务处理 Spring源代码解析(七):Spring AOP中对拦截器...
Spring AOP源码深度剖析开源架构源码2021.pdf
AOP是Spring提供的关键特性之一。 AOP即面向切面编程,是OOP编程(面向对象编程)的有效补充。
Java流行框架源码分析:Spring源码、SpringBoot源码、SpringAOP源码、SpringSecurity源码、SpringSecurity OAuth2源码、JDK源码、Netty源码
aop分析笔记 个人总结所得 org.springframework.aop.framework.autoproxy
主要介绍了Spring AOP中使用args表达式的方法,结合实例形式分析了spring面向切面AOP中使用args表达式具体步骤、相关实现技巧与操作注意事项,需要的朋友可以参考下