前言

@EnableAspectJAutoProxy 是启动aop功能的意思,那么里面是什么呢?

正文

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({AspectJAutoProxyRegistrar.class})
public @interface EnableAspectJAutoProxy {
boolean proxyTargetClass() default false; boolean exposeProxy() default false;
}

在这个接口中,又导入了AspectJAutoProxyRegistrar.class,看下这个是啥。

class AspectJAutoProxyRegistrar implements ImportBeanDefinitionRegistrar {
AspectJAutoProxyRegistrar() {
} public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);
AnnotationAttributes enableAspectJAutoProxy = AnnotationConfigUtils.attributesFor(importingClassMetadata, EnableAspectJAutoProxy.class);
if (enableAspectJAutoProxy != null) {
if (enableAspectJAutoProxy.getBoolean("proxyTargetClass")) {
AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
} if (enableAspectJAutoProxy.getBoolean("exposeProxy")) {
AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry);
}
} }
}

AspectJAutoProxyRegistrar 是来实现接口,ImportBeanDefinitionRegistrar。

重点就是ImportBeanDefinitionRegistrar,这个告诉了我们这个东西将会为我们注入一些服务,而我们需要明白的就是到底注册了一个什么服务,然后服务又是如何运行的。

AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);

表示的是,是否注册一个AspectJAnnotationAutoProxy,其实这里我们可以猜到大概是要将这个AspectJAnnotationAutoProxy注入到容器中。

继续进去看下:

@Nullable
private static BeanDefinition registerOrEscalateApcAsRequired(Class<?> cls, BeanDefinitionRegistry registry, @Nullable Object source) {
Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
if (registry.containsBeanDefinition("org.springframework.aop.config.internalAutoProxyCreator")) {
BeanDefinition apcDefinition = registry.getBeanDefinition("org.springframework.aop.config.internalAutoProxyCreator");
if (!cls.getName().equals(apcDefinition.getBeanClassName())) {
int currentPriority = findPriorityForClass(apcDefinition.getBeanClassName());
int requiredPriority = findPriorityForClass(cls);
if (currentPriority < requiredPriority) {
apcDefinition.setBeanClassName(cls.getName());
}
} return null;
} else {
RootBeanDefinition beanDefinition = new RootBeanDefinition(cls);
beanDefinition.setSource(source);
beanDefinition.getPropertyValues().add("order", -2147483648);
beanDefinition.setRole(2);
registry.registerBeanDefinition("org.springframework.aop.config.internalAutoProxyCreator", beanDefinition);
return beanDefinition;
}
}

这里可以说明了,我们大概要注册一个类型为:org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator的东西了。

上面if里面判断为,是否有一个org.springframework.aop.config.internalAutoProxyCreator的注册类型,然后去获取到类型对应的服务的class 是否为cls.getName(),也就是AnnotationAwareAspectJAutoProxyCreator。

如果没有,那么会到根容器中或注入。

当注册完之后,那么又会去干一些什么呢?

AnnotationAttributes enableAspectJAutoProxy = AnnotationConfigUtils.attributesFor(importingClassMetadata, EnableAspectJAutoProxy.class);
if (enableAspectJAutoProxy != null) {
if (enableAspectJAutoProxy.getBoolean("proxyTargetClass")) {
AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
} if (enableAspectJAutoProxy.getBoolean("exposeProxy")) {
AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry);
}
}

这时候就是去判断一些属性,然后做一些事情。

那么其实应该重点关注AnnotationAwareAspectJAutoProxyCreator,到底为我们提供了一些什么服务,然后再来分析它有一些什么属性,能为我们带来什么。

下一节

AnnotationAwareAspectJAutoProxyCreator 为我们提供了什么服务。

面向切面编程AOP[二](java @EnableAspectJAutoProxy 代码原理)的更多相关文章

  1. Spring学习手札(二)面向切面编程AOP

    AOP理解 Aspect Oriented Program面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. 但是,这种说法有些片面,因为在软件工程中,AOP的价值体现的并 ...

  2. 面向切面编程AOP

    本文的主要内容(AOP): 1.AOP面向切面编程的相关概念(思想.原理.相关术语) 2.AOP编程底层实现机制(动态代理机制:JDK代理.Cglib代理) 3.Spring的传统AOP编程的案例(计 ...

  3. Spring学习笔记:面向切面编程AOP(Aspect Oriented Programming)

    一.面向切面编程AOP 目标:让我们可以“专心做事”,避免繁杂重复的功能编码 原理:将复杂的需求分解出不同方面,将公共功能集中解决 *****所谓面向切面编程,是一种通过预编译方式和运行期动态代理实现 ...

  4. Spring框架系列(4) - 深入浅出Spring核心之面向切面编程(AOP)

    在Spring基础 - Spring简单例子引入Spring的核心中向你展示了AOP的基础含义,同时以此发散了一些AOP相关知识点; 本节将在此基础上进一步解读AOP的含义以及AOP的使用方式.@pd ...

  5. Spring框架学习笔记(2)——面向切面编程AOP

    介绍 概念 面向切面编程AOP与面向对象编程OOP有所不同,AOP不是对OOP的替换,而是对OOP的一种补充,AOP增强了OOP. 假设我们有几个业务代码,都调用了某个方法,按照OOP的思想,我们就会 ...

  6. Spring之控制反转——IoC、面向切面编程——AOP

      控制反转——IoC 提出IoC的目的 为了解决对象之间的耦合度过高的问题,提出了IoC理论,用来实现对象之间的解耦. 什么是IoC IoC是Inversion of Control的缩写,译为控制 ...

  7. 【串线篇】面向切面编程AOP

    面向切面编程AOP 描述:将某段代码“动态”的切入到“指定方法”的“指定位置”进行运行的一种编程方式 (其底层就是Java的动态代理)spring对其做了简化书写 场景: 1).AOP加日志保存到数据 ...

  8. 04 Spring:01.Spring框架简介&&02.程序间耦合&&03.Spring的 IOC 和 DI&&08.面向切面编程 AOP&&10.Spring中事务控制

    spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的A ...

  9. 设计模式之面向切面编程AOP

    动态的将代码切入到指定的方法.指定位置上的编程思想就是面向切面的编程. 代码只有两种,一种是逻辑代码.另一种是非逻辑代码.逻辑代码就是实现功能的核心代码,非逻辑代码就是处理琐碎事务的代码,比如说获取连 ...

  10. [译]如何在ASP.NET Core中实现面向切面编程(AOP)

    原文地址:ASPECT ORIENTED PROGRAMMING USING PROXIES IN ASP.NET CORE 原文作者:ZANID HAYTAM 译文地址:如何在ASP.NET Cor ...

随机推荐

  1. VC-MFC 登陆界面 + 数据库账号+密码

    1 // DlgUser.cpp : 实现文件 2 // 3 4 #include "stdafx.h" 5 #include "Login.h" 6 #inc ...

  2. ConcurrentHashMap的put方法

    使用JDK8 源码: public V put(K key, V value) { return putVal(key, value, false); } /** Implementation for ...

  3. 2024-03-09:用go语言,我们把无限数量的栈排成一行,按从左到右的次序从 0 开始编号, 每个栈的的最大容量 capacity 都相同。实现一个叫「餐盘」的类 DinnerPlates, Di

    2024-03-09:用go语言,我们把无限数量的栈排成一行,按从左到右的次序从 0 开始编号, 每个栈的的最大容量 capacity 都相同.实现一个叫「餐盘」的类 DinnerPlates, Di ...

  4. 将MindSpore运行结果输出到log文件

    技术背景 我们在Linux系统下使用一些深度学习框架(如MindSpore)运行脚本的时候,经常会用一些打印输出来判断当前执行的步骤,或者是使用打印输出来定位算法问题.但是在Linux系统下程序输出其 ...

  5. Asp .Net Core 系列:Asp .Net Core 集成 Hangfire+MySQL

    简介 https://www.hangfire.io/ 在 .NET 和 .NET Core 应用程序中执行后台处理的简单方法,无需 Windows 服务或单独的进程. Hangfire 是一个开源的 ...

  6. 3DCAT亮相糖酒会,为元宇宙展会提供实时云渲染支持

    4月12日,第108届全国糖酒商品交易会(下文简称"糖酒会")在成都正式开幕,吸引了众多酒类企业和行业人士的参与. 图片源自新华社 本次糖酒会上,某展会采用了"双线&qu ...

  7. Kotlin 协程基础使用学习

    原文: Kotlin 协程基础使用学习-Stars-One的杂货小窝 本篇阅读可能需要以下知识,否则可能阅读会有些困难 客户端开发基础(Android开发或JavaFx开发) Java多线程基础 ko ...

  8. Docker 中使用 scratch 镜像构建 Go 应用镜像,容器报错:X509: Certificate Signed by Unknown Authority

    本文首发于 Ficow Shen's Blog,原文地址: Docker 中使用 scratch 镜像构建 Go 应用镜像,容器报错:X509: Certificate Signed by Unkno ...

  9. vue项目,关闭eslint语法检测

    vue.config.js文件中 module.exports = { lintOnSave:false //关闭语法检查 } 然后重启项目生效!

  10. linux安装jdk压缩包版

    1.下载压缩包可以选择国内大厂的jdk镜像网站下载速度很快, 比如华为的:https://repo.huaweicloud.com/java/jdk/ 2.查看Linux系统是否有自带的jdk: 输入 ...