死磕Spring AOP系列1:编程式实现AOP

简介:

这个系列是AOP源码分析级别的文章。由于现在AOP已经不是什么高深的技术,网上的例子也比比皆是,不论是xml schema,还是annotation声明式。相信用过Spring的朋友,都可以信手拈来。

本系列文章的原则

  1. 如何配置AOP不是重点

  2. AOP相关概念讲解不是重点

  3. AOP 底层代码设计才是重点

本篇的主要内容

  1. 认识ProxyFactory,并通过该工厂类,将“日志”和“安全校验”代码切入到业务逻辑中

  2. 分析代理对象的实现流程。

  3. 认识ProxyFactory相关对象



1.使用ProxyFactory,实现"AOP"效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
  *模拟业务接口
  */
public  interface  UserService {
     public  void  updateUser();
}
 
/**
  *模拟具体业务
  */
public  class  UserServiceImpl  implements  UserService{
 
     @Override
     public  void  updateUser() {
         System.out.println( "$$$$$$执行业务逻辑$$$$$" );
     }
}
 
/**
  * 模拟切面1
  */
public  class  SecurityInterceptor  implements  MethodInterceptor {
     @Override
     public  Object invoke(MethodInvocation methodInvocation)  throws  Throwable {
         System.out.println( "==========执行安全校验====================" );
         return  methodInvocation.proceed();
     }
}
 
/**
  * 模拟切面2
  */
public  class  LoggerBeforeAdvice  implements  MethodBeforeAdvice {
     @Override
     public  void  before(Method method, Object[] args, Object target)  throws  Throwable {
         System.out.println( "=======保存更新日志=========" );
     }
}


Main类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public  static  void  main(String[] args) {
     //1.初始化源对象(一定要实现接口)
     UserService target = new  UserServiceImpl();
     //2.AOP 代理工厂
     ProxyFactory pf =  new  ProxyFactory(target);
     //3.装配Advice
     pf.addAdvice( new  SecurityInterceptor());
     //pf.addAdvice(new LoggerBeforeAdvice());
     pf.addAdvisor( new  DefaultPointcutAdvisor( new  LoggerBeforeAdvice()));
     ////4.获取代理对象
     UserService proxy =(UserService)pf.getProxy();
     //5.调用业务
     proxy.updateUser();
}

输出结果

==========执行安全校验====================
=======保存更新日志=========
$$$$$$执行业务逻辑$$$$$


通过结果确认,通过编程的方式实现了AOP切入的效果。逻辑很简单,无需过多解释。把重点放到ProxyFactory对象上。下面,重点分析下ProxyFactory。

2.分析ProxyFactory

wKioL1dQR2HAcFXEAABux8L80Z4236.png


ProxyFactory继承了ProxyCreatorSupport类,持有了AopProxyFactory,AdvisorChainFactory,

List<Advisor>。等



作用
AopProxyFactory 基于 AdvisedSupport 配置信息,生成AOP 代理对象.默认为“DefaultAopProxyFactory
AdvisorChainFactory advisor链。默认实现为DefaultAdvisorChainFactory,只有一个方法,获取Advised满足条件的MethodInterceptor列表或DynamicInterceptionAdvice列表
Advisor 持有AOP advice和filter
ProxyConfig
生成代理对象的配置元信息

3.AOP重点接口大杂烩

3.1Advice&Interceptor

wKiom1dQSz7D--fAAAAy0HpjvGI119.png

Interceptor VS Advice

Advice是AOP编程中某一个方面(Aspect)在某个连接点(JoinPoint)所执行的特定动作,这个连接点(JoinPoint)可以是自 定义的;而Spring中的Interceptor更多关注程序运行时某些特定连接点(属性访问,对象构造,方法调用)时的动作。确切的 说,Interceptor的范围更窄一些。

3.2 PointCut&Advisor

wKiom1dQTUuwsTKjAABGdxR8uqc708.png

概念列表

Terms Description
Aspect
A module which has a set of APIs providing cross-cutting requirements. For example, a logging module would be called AOP aspect for logging. An application can have any number of aspects depending on the requirement.
Join point This represents a point in your application where you can plug-in AOP aspect. You can also say, it is the actual place in the application where an action will be taken using Spring AOP framework.
Advice This is the actual action to be taken either before or after the method execution. This is actual piece of code that is invoked during program execution by Spring AOP framework.
Pointcut This is a set of one or more joinpoints where an advice should be executed. You can specify pointcuts using expressions or patterns as we will see in our AOP examples.
Introduction An introduction allows you to add new methods or attributes to existing classes.
Target object The object being advised by one or more aspects, this object will always be a proxied object. Also referred to as the advised object.
Weaving Weaving is the process of linking aspects with other application types or objects to create an advised object. This can be done at compile time, load time, or at runtime.

3.4Proxy&ProxyFactory

wKiom1dQTsySiRwNAAAjgjDsxmo266.png

4. ProxyFactory底层实现设计

弄清楚ProxyFactory的底层实现,也就基本上弄清了AOP的实现原理,不管使用XML Schema还是Annotation方式,归根结底核心就在这儿。主要差别,仅仅是使用PointCut差别而已。


Advisor是设计的核心元素,起到承上启下的作用,连接着Advice和Pointcut。默认实现是:DefaultPointCutAdvisor。需要知道2个接口ClassFilter,MethodMatcher,通过方法名,就可以判定他们的作用了,主要用在"匹配"上。

wKiom1dQT87z01lAAACpJxJJ9j4702.png


序列图

wKiom1dQT_uhGJ82AADfJFuvcQQ570.png

通过该序列图,可以清楚了解,代理的调用过程。整个过程就是围绕一个核心,构造匹配元素+反射调用。如果想搞清楚底层代理调用实现,可以看org.springframework.aop.framework .ReflectiveMethodInvocation。


接下来,将分析Spring Bean工厂是如何生成代理Bean的,以DefaultAdvisorAutoProxyCreator类为例,进行分析。




本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/1785667,如需转载请自行联系原作者
相关文章
|
1月前
|
监控 Java 开发者
Spring AOP动态代理
Spring AOP动态代理
40 1
|
1月前
|
Java Spring 容器
Spring的AOP失效场景详解
Spring的AOP失效场景详解
88 0
|
1月前
|
Java Spring
【编程笔记】在 Spring 项目中使用 RestTemplate 发送网络请求
【编程笔记】在 Spring 项目中使用 RestTemplate 发送网络请求
94 0
|
23天前
|
设计模式 Java Maven
Spring Aop 底层责任链思路实现-springaopdi-ceng-ze-ren-lian-si-lu-shi-xian
Spring Aop 底层责任链思路实现-springaopdi-ceng-ze-ren-lian-si-lu-shi-xian
31 1
|
2月前
|
XML Java 数据格式
5个点轻松搞定Spring AOP底层实现原理
AOP 也是 Spring 中一个较为重要的内容,相对于传统的 OOP 模式,AOP 有很多让人难以理解的地方,本篇文章将向大家介绍 AOP 的实现方法及其底层实现,内容包括:
44 1
|
7天前
|
Java 数据库 Spring
切面编程的艺术:Spring动态代理解析与实战
切面编程的艺术:Spring动态代理解析与实战
23 0
切面编程的艺术:Spring动态代理解析与实战
|
16天前
|
XML Java Maven
Spring之Aop的注解使用
Spring之Aop的注解使用
|
22天前
|
Java Spring
Spring 如何实现 AOP
Spring 如何实现 AOP
17 0
|
30天前
|
Java 编译器 程序员
Spring AOP 和 AspectJ 的比较
Spring AOP 和 AspectJ 的比较
35 0
|
1月前
|
Java Spring
【spring(三)】AOP总结
【spring(三)】AOP总结