1.前言
Aop是一个高频出现的技术点,不管在你的日常开发中还是在跳槽面试中,始终会看到它的影子。如此的高频,证明了它的重要性,既然这么重要,就没有理由不去搞清楚它。
在弄清楚Aop之前先来看看异步@Async和事务@Transactional的相关实现。
2.@Async实现
@Async在这篇文章你必须懂也可以懂的@Async原理中做过详细介绍,主要用来在应用实现异步功能。
2.1 @EnableAsync开启异步功能
若要在应用中使用@Async来实现异步功能,需要使用@EnableAsync注解来开启异步功能
2.2 注入ProxyAsyncConfiguration
@EnableAsync注解引入了异步配置选择器AsyncConfigurationSelectorbing并向IOC容器中注入了ProxyAsyncConfiguration
2.3 创建异步注解处理器AsyncAnnotationBeanPostProcessor
ProxyAsyncConfiguration中创建AsyncAnnotationBeanPostProcessorBean处理器
2.4 创建切面
2.4.1 创建切点
2.4.2 创建通知

2.5 Bean后置处理
3.@Transactional实现
3.1 @EnableAsync开启异步功能
3.2 注入ProxyTransactionManagementConfiguration配置类
3.2.1 创建切面

3.2.2 创建切点
3.2.3 创建通知
3.3 注入InfrastructureAdvisorAutoProxyCreator
InfrastructureAdvisorAutoProxyCreator实现了BeanPostProcessor接口,因此会拦截所有的Bean进行处理
4.小结
通过对@Async和@Transactional的实现进行分析后,可以发现一个共同点:使用@Enablexxx注解来引入Bean处理器和切面,指定切面对应的切点和通知;通过Bean的后置处理方法对符合切点的Bean生成代理并指明设置通知。
理解原理之后,就可以仿照着@Async和@Transactional来实现我们自己的需求,比如打印所有请求的请求参数、请求响应内容、请求耗时等。
类似功能已经可以实现了,但是实现起来还是比较复杂,首先你需要定义一个注解,紧接着要定义切面、切点、通知,然后定义Bean处理器,这么难还是放弃了吧。别着急,往下看看SpringAop的实现,你一定会爱不释手。
5.Aop实现
5.1 @EnableAspectJAutoProxy开启切面功能
5.2 注入AnnotationAwareAspectJAutoProxyCreator
AnnotationAwareAspectJAutoProxyCreator实现BeanPostProcessor接口,用作Bean后置处理
5.2.1 Bean是否含有@Aspect注解
5.2.2 创建切点

5.2.3 创建通知
5.2.4 创建切面
5.3 Aop实现
@Component
@Aspect
public class SystemLogAspect {
/**
* 定义切点
*/
@Pointcut("execution(* com.boot.example.controller.*.*(..))")
public void SystemLogAspect() {
}
/**
* 定义通知
*/
@Before("SystemLogAspect()")
public void doBefore(JoinPoint joinPoint) {
}
/**
* 定义通知
*/
@AfterReturning("SystemLogAspect()")
public void doAfterReturning() {
}
/**
* 定义通知
*/
@Around("SystemLogAspect()")
public void doAround(ProceedingJoinPoint joinPoint) {
}
/**
* 定义通知
*/
@AfterThrowing(value = "SystemLogAspect()", throwing = "ex")
public void doThrowing(JoinPoint joinPoint, Exception ex) {
}
}
6.总结
- 使用
@EnableAspectJAutoProxy注解开启Aop功能 - 定义Bean后置处理器
- 拦截所有被
@Aspect注解标注的Bean - 获取Bean中所有没有被@Pointcut标注的方法
- 方法被
@Pointcut、@ Around、Before、@After、@AfterReturning、@AfterThrowing注解标注的,使用注解上的value创建切点,使用当前方法创建通知,使用切点和通知来创建切面