spring aop之链式调用的实现
作者:niocoder 发布时间:2022-10-30 05:56:50
概述
AOP
(Aspect Orient Programming
),我们一般称为面向方面(切面)编程,作为面向对象的一种补充,用于处理系统中分布于各个模块的横切关注点,比如事务管理、日志、缓存等等。 Spring
AOP
采用的是 * ,在运行期间对业务方法进行增强,所以不会生成新类,Spring
AOP
提供了对JDK
* 的支持以及CGLib的支持。本章我们不关注aop
代理类的实现,我简单实现一个指定次序的链式调用。
实现链式调用的
MethodInterceptor
定义 * 链,MethodInvocation
递归进入下一个 * 链中。类图如下:
MethodInterceptor
public interface MethodInterceptor {
Object invoke(MethodInvocation invocation) throws Throwable;
}
MethodInvocation
public interface MethodInvocation {
Object proceed() throws Throwable;
}
AbstractAspectJAdvice
抽象类,实现MethodInterceptor
public abstract class AbstractAspectJAdvice implements MethodInterceptor{
private Method adviceMethod;
private Object adviceObject;
public AbstractAspectJAdvice(Method adviceMethod, Object adviceObject) {
this.adviceMethod = adviceMethod;
this.adviceObject = adviceObject;
}
public Method getAdviceMethod() {
return this.adviceMethod;
}
public void invokeAdviceMethod() throws Throwable {
adviceMethod.invoke(adviceObject);
}
}
AspectJBeforeAdvice
前置通知
public class AspectJBeforeAdvice extends AbstractAspectJAdvice {
public AspectJBeforeAdvice(Method method, Object adviceObject) {
super(method, adviceObject);
}
@Override
public Object invoke(MethodInvocation invocation) throws Throwable{
this.invokeAdviceMethod();
Object o = invocation.proceed();
return o;
}
}
AspectJAfterReturningAdvice
后置通知
public class AspectJAfterReturningAdvice extends AbstractAspectJAdvice {
public AspectJAfterReturningAdvice(Method method, Object adviceObject) {
super(method, adviceObject);
}
@Override
public Object invoke(MethodInvocation invocation) throws Throwable{
Object o = invocation.proceed();
this.invokeAdviceMethod();
return o;
}
}
ReflectiveMethodInvocation
实现MethodInvocation
,proceed()
方法递归实现链式调用。
public class ReflectiveMethodInvocation implements MethodInvocation {
private final Object targetObject;
private final Method targetMethod;
private final List<MethodInterceptor> interceptorList;
private int currentInterceptorIndex = -1;
public ReflectiveMethodInvocation(Object targetObject, Method targetMethod, List<MethodInterceptor> interceptorList) {
this.targetObject = targetObject;
this.targetMethod = targetMethod;
this.interceptorList = interceptorList;
}
@Override
public Object proceed() throws Throwable {
if (this.currentInterceptorIndex == this.interceptorList.size() - 1) {
return invokeJoinPoint();
}
this.currentInterceptorIndex++;
MethodInterceptor interceptor =
this.interceptorList.get(this.currentInterceptorIndex);
return interceptor.invoke(this);
}
private Object invokeJoinPoint() throws Throwable {
return this.targetMethod.invoke(this.targetObject);
}
}
NioCoderService
模拟service
类
public class NioCoderService {
public void testAop() {
System.out.println("http://niocoder.com/");
}
}
TransactionManager
模拟通知类
public class TransactionManager {
public void start() {
System.out.println("start tx");
}
public void commit() {
System.out.println("commit tx");
}
public void rollback() {
System.out.println("rollback tx");
}
}
ReflectiveMethodInvocationTest
beforeAdvice->afterReturningAdvice
测试类,测试通知
public class ReflectiveMethodInvocationTest {
private AspectJBeforeAdvice beforeAdvice = null;
private AspectJAfterReturningAdvice afterReturningAdvice = null;
private NioCoderService nioCoderService;
private TransactionManager tx;
public void setUp() throws Exception {
nioCoderService = new NioCoderService();
tx = new TransactionManager();
beforeAdvice = new AspectJBeforeAdvice(TransactionManager.class.getMethod("start"), tx);
afterReturningAdvice = new AspectJAfterReturningAdvice(TransactionManager.class.getMethod("commit"), tx);
}
public void testMethodInvocation() throws Throwable {
Method method = NioCoderService.class.getMethod("testAop");
List<MethodInterceptor> interceptorList = new ArrayList<>();
interceptorList.add(beforeAdvice);
interceptorList.add(afterReturningAdvice);
ReflectiveMethodInvocation mi = new ReflectiveMethodInvocation(nioCoderService, method, interceptorList);
mi.proceed();
}
public static void main(String[] args) throws Throwable {
ReflectiveMethodInvocationTest reflectiveMethodInvocationTest = new ReflectiveMethodInvocationTest();
reflectiveMethodInvocationTest.setUp();
reflectiveMethodInvocationTest.testMethodInvocation();
}
}
输出:
start tx
http://niocoder.com/
commit tx
时序图 beforeAdvice->afterReturningAdvice
afterReturningAdvice->beforeAdvice
修改interceptorList
的顺序
public void testMethodInvocation() throws Throwable {
Method method = NioCoderService.class.getMethod("testAop");
List<MethodInterceptor> interceptorList = new ArrayList<>();
interceptorList.add(afterReturningAdvice);
interceptorList.add(beforeAdvice);
ReflectiveMethodInvocation mi = new ReflectiveMethodInvocation(nioCoderService, method, interceptorList);
mi.proceed();
}
输出:
start tx
http://niocoder.com/
commit tx
时序图 afterReturningAdvice->beforeAdvice
代码下载
github:https://github.com/longfeizheng/data-structure-java/blob/master/src/main/java/cn/merryyou/aop
代码下载
github:https://github.com/longfeizheng/data-structure-java
来源:https://segmentfault.com/a/1190000018206756


猜你喜欢
- 抽象类什么是抽象类呢?在现实生活中,我们说“人类”,我们无法对应到具体某个人,同样的,&ld
- 使用adb or fastboot命令进入高通的9008(edl)模式已经有人写过了,下面两种是其中之一,我再加一个如题,两种方法1. ad
- 之前从他人的博文,还有一些书籍中了解到 常量是放在常量池 中,细节的内容无从得知,总觉得面前的东西是一个几乎完全的黑盒,总是觉得不舒服,于是
- 在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的。但是在通过了Ap
- 本文实例讲述了Android实现为Notification加上一个进度条的方法。分享给大家供大家参考,具体如下:package com.no
- 问题现象今天在做一个需求:将存入数据库中的数据读到后解析成list遍历分析数据格式:"[1677660600000, 167766
- 1、TCP/IP层次模型当然这里我们只讨论重要的四层01,应用层(Application):应用层是个很广泛的概念,有一些基本相同的系统级T
- 单链表:每个数据是以节点的形式存在的每个节点分为数据域和指针域数据域中保存该节点的数据指针域中保存指向下一个节点的指针实现思路:节点类Sin
- 从一个问题引入如果你以前接触过C语言,那么对下面的这段代码一定很熟悉:#include <stdio.h>int main(vo
- Synchronized的用法在多线程并发问题中,常用Synchronized锁解决问题。Synchronized锁通常用于同步示例方法,同
- final 可以适用的范围:修饰类:使用这种修饰符的类无法被继承修饰函数:被修饰的不能被重写修饰属性:1.final修饰的成员变量是常量,值
- 前言本文主要给大家介绍了关于Android模仿美团顶部滑动菜单的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。先
- 调整数组顺序使奇数位于偶数之前1. 题目描述输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的
- Math.abs()的实现源码通过三目运算符判断a是否小于0来实现/** * Returns the absolute valu
- C#处理猜拳问题的简单实例(非窗体)//猜拳,5局3胜,要求使用公用变量。namespace 结构体复习_公用变量{class Progra
- 1,背景在开发中总会遇到一个可拖拽的悬浮View,不管是在开发中,还是在线上,都时长有这样的控件,我们通常遇到这种情况,经常需要自己封装,需
- 在linux下开发,使用的是C语言。适用于需要定时的软件开发,以系统真实的时间来计算,它送出SIGALRM信号。每隔一秒定时一次c语言定时器
- 一、运算符运算符包括下面几种:算术运算符赋值运算符比较运算符逻辑运算符位运算符三目运算符最不常用的是位运算符,但也是最接近计算机底层的。1、
- 本文主要为大家分享了MyEclipse中安装使用Maven的方法,供大家参考,具体内容如下Step1:下载mavenhttp://maven
- Application对象Application对象生存期和Web应用程序生存期一样长,生存期从Web应用程序网页被访问开始,HttpApp