转载自 Spring Enable*高级应用及原理

Enable*

之前的文章用到了一些Enable*开头的注解,比如EnableAsync、EnableScheduling、EnableAspectJAutoProxy、EnableCaching等,Enable表示开启/允许一项功能。

Enable*工作原理

我们只需要几个很简单的注解就能开启一个复杂的功能,这是多么简易的用法,这是怎么办到的?

首先来看看计划任务@EnableScheduling的源代码

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(SchedulingConfiguration.class)
@Documented
public @interface EnableScheduling {}

主要核心的配置就是导入了一个配置文件,所以谜底也就接开了。

@Import(SchedulingConfiguration.class)

@Import用法

来看看Import的源码

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {/*** {@link Configuration}, {@link ImportSelector}, {@link ImportBeanDefinitionRegistrar}* or regular component classes to import.*/Class<?>[] value();}

1、Configuration

即上面的用法,直接导入Configuration配置类。

2、ImportSelector

根据条件选择导入不同的配置类,参考@EnableAsync

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AsyncConfigurationSelector.class)
public @interface EnableAsync {
public class AsyncConfigurationSelector extends AdviceModeImportSelector<EnableAsync> {private static final String ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME ="org.springframework.scheduling.aspectj.AspectJAsyncConfiguration";/*** {@inheritDoc}* @return {@link ProxyAsyncConfiguration} or {@code AspectJAsyncConfiguration} for* {@code PROXY} and {@code ASPECTJ} values of {@link EnableAsync#mode()}, respectively*/@Overridepublic String[] selectImports(AdviceMode adviceMode) {switch (adviceMode) {case PROXY:return new String[] { ProxyAsyncConfiguration.class.getName() };case ASPECTJ:return new String[] { ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME };default:return null;}}}

3、ImportBeanDefinitionRegistrar

动态注册Bean,参考@EnableAspectJAutoProxy

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AspectJAutoProxyRegistrar.class)
public @interface EnableAspectJAutoProxy {
class AspectJAutoProxyRegistrar implements ImportBeanDefinitionRegistrar {/*** Register, escalate, and configure the AspectJ auto proxy creator based on the value* of the @{@link EnableAspectJAutoProxy#proxyTargetClass()} attribute on the importing* {@code @Configuration} class.*/@Overridepublic void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);AnnotationAttributes enableAspectJAutoProxy =AnnotationConfigUtils.attributesFor(importingClassMetadata, EnableAspectJAutoProxy.class);if (enableAspectJAutoProxy.getBoolean("proxyTargetClass")) {AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);}if (enableAspectJAutoProxy.getBoolean("exposeProxy")) {AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry);}}}

Spring Enable*高级应用及原理相关推荐

  1. Spring Boot的自动化配置原理

    转载自 Spring Boot的自动化配置原理 随着Ruby.Groovy等动态语言的流行,相比较之下Java的开发显得格外笨重.繁多的配置.低下的开发效率.复杂的部署流程以及第三方技术集成难度大等问 ...

  2. 吹爆 阿里新产Spring Boot高级笔记,肝完变秃也变强

    前言 Java各种开发框架发展了很多年,影响了一代又一代的程序员.但环顾当前Java开源世界中的流行技术框架,能同时满足快速开发和分布式系统架构,还是群众基础最好.功能最全.基于Spring技术的Sp ...

  3. Spring Enable 是什么?

    本文内容如有错误.不足之处,欢迎技术爱好者们一同探讨,在本文下面讨论区留言,感谢. 文章目录 简述 分类 核心类 实现 原理 @Import 的作用 @Configuration和@Import @E ...

  4. spring的BeanWrapper类的原理和使用方法

    转自:http://blog.sina.com.cn/s/blog_79ae79b30100t4hh.html 如果动态设置一个对象属性,可以借助Java的Reflection机制完成: Class ...

  5. Spring 容器AOP的实现原理——动态代理

    本文来自极客学院 Spring 容器AOP的实现原理--动态代理 之前写了一篇关于IOC的博客--<Spring容器IOC解析及简单实现>,今天再来聊聊AOP.大家都知道Spring的两大 ...

  6. spring手动控制事务开启_“上帝视角”图解Spring事务的传播机制原理

    转载:https://mp.weixin.qq.com/s/odP1DKgRtXsCcAKxwGahug 数据库事务的"抓手" 数据库的事务功能已经由数据库自身实现,它留给用户的就 ...

  7. spring(3)高级装配

    [0]README 0)本文部分文字描述转自:"Spring In Action(中/英文版)",旨在review  spring(3)高级装配 的相关知识: [1]环境与prof ...

  8. Spring读取xml配置文件的原理与实现

    2019独角兽企业重金招聘Python工程师标准>>> Spring读取xml配置文件的原理与实现 本篇博文的目录: 一:前言 二:spring的配置文件 三:依赖的第三方库.使用技 ...

  9. Spring定时任务高级使用篇

    Spring定时任务高级使用篇 前面一篇博文 <Spring之定时任务基本使用篇> 介绍了Spring环境下,定时任务的简单使用姿势,也留了一些问题,这一篇则希望能针对这些问题给个答案 I ...

最新文章

  1. 【电子基础】模拟电路问答
  2. 目前的计算机聪原理上讲,计算机组成原理-第23-24讲(第7章)+课件.ppt
  3. SVN中trunk、branch、tag区别
  4. 东南大学校内智能车竞赛
  5. Java设计模式10:观察者模式
  6. c++ map 修改value_C++知识分享之STL容器:set 容器与 map 容器的简单应用
  7. 【STM32】定时器相关函数和类型
  8. 农历控件源码(C#)
  9. 与大家分享一个我最近开始用的不错的JavaScript IDE
  10. WPF程序将DLL嵌入到EXE的两种方法
  11. GIS-004-Cesium版权信息隐藏
  12. 【Python基础入门系列】第05天:Python函数
  13. 计算机测试性评估,计算机CPU性能评估软件PCMark
  14. 如何最简单把pdf转换word格式
  15. ANSYS预紧力螺栓连接钢结构节点分析实例(前处理基于HyperMesh)
  16. sqlite中的 database is locked 问题
  17. kermit开发板启动Linux乱码,Ubuntu下C-kermit的使用
  18. henu云计算复习-图片版
  19. 华为5g鸿蒙折叠,华为再次亮剑!5G新旗舰已经确认,折叠屏+升级到鸿蒙2.0,价格过万...
  20. 基于FPGA的混沌系统实现

热门文章

  1. [Spring5]IOC容器_Bean管理注解方式_完全注解开发
  2. [剑指offer]面试题48:不能被继承的类
  3. Invitation Cards POJ - 1511 SPFA(dijkstra+反向建图+邻接表(下标过大)+输入输出用stdio(iostream超时))
  4. bigdecimal 平均数_MapReduce实例-必须用Combine--求平均数
  5. Redis基础数据结构内部实现简单介绍
  6. 通过Java编写一个服务器理解动态Web,静态Web
  7. 算法---字符串顺序平移
  8. workman php 安装,workerman安装及遇到的问题解决
  9. P4196 [CQOI2006]凸多边形 /【模板】半平面交
  10. P2260 [清华集训2012]模积和,P2834 能力测验(二维除法分块)