Enable*

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

Enable*工作原理

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

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

  1. @Target(ElementType.TYPE)

  2. @Retention(RetentionPolicy.RUNTIME)

  3. @Import(SchedulingConfiguration.class)

  4. @Documented

  5. public @interface EnableScheduling {

  6. }

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

@Import(SchedulingConfiguration.class)

@Import用法

来看看Import的源码

  1. @Target(ElementType.TYPE)

  2. @Retention(RetentionPolicy.RUNTIME)

  3. @Documented

  4. public @interface Import {

  5.    /**

  6.     * {@link Configuration}, {@link ImportSelector}, {@link ImportBeanDefinitionRegistrar}

  7.     * or regular component classes to import.

  8.     */

  9.    Class<?>[] value();

  10. }

1、Configuration

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

2、ImportSelector

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

  1. @Target(ElementType.TYPE)

  2. @Retention(RetentionPolicy.RUNTIME)

  3. @Documented

  4. @Import(AsyncConfigurationSelector.class)

  5. public @interface EnableAsync {

  1. public class AsyncConfigurationSelector extends AdviceModeImportSelector<EnableAsync> {

  2.    private static final String ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME =

  3.            "org.springframework.scheduling.aspectj.AspectJAsyncConfiguration";

  4.    /**

  5.     * {@inheritDoc}

  6.     * @return {@link ProxyAsyncConfiguration} or {@code AspectJAsyncConfiguration} for

  7.     * {@code PROXY} and {@code ASPECTJ} values of {@link EnableAsync#mode()}, respectively

  8.     */

  9.    @Override

  10.    public String[] selectImports(AdviceMode adviceMode) {

  11.        switch (adviceMode) {

  12.            case PROXY:

  13.                return new String[] { ProxyAsyncConfiguration.class.getName() };

  14.            case ASPECTJ:

  15.                return new String[] { ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME };

  16.            default:

  17.                return null;

  18.        }

  19.    }

  20. }

3、ImportBeanDefinitionRegistrar

动态注册Bean,参考@EnableAspectJAutoProxy

  1. @Target(ElementType.TYPE)

  2. @Retention(RetentionPolicy.RUNTIME)

  3. @Documented

  4. @Import(AspectJAutoProxyRegistrar.class)

  5. public @interface EnableAspectJAutoProxy {

  1. class AspectJAutoProxyRegistrar implements ImportBeanDefinitionRegistrar {

  2.    /**

  3.     * Register, escalate, and configure the AspectJ auto proxy creator based on the value

  4.     * of the @{@link EnableAspectJAutoProxy#proxyTargetClass()} attribute on the importing

  5.     * {@code @Configuration} class.

  6.     */

  7.    @Override

  8.    public void registerBeanDefinitions(

  9.            AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

  10.        AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);

  11.        AnnotationAttributes enableAspectJAutoProxy =

  12.                AnnotationConfigUtils.attributesFor(importingClassMetadata, EnableAspectJAutoProxy.class);

  13.        if (enableAspectJAutoProxy.getBoolean("proxyTargetClass")) {

  14.            AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);

  15.        }

  16.        if (enableAspectJAutoProxy.getBoolean("exposeProxy")) {

  17.            AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry);

  18.        }

  19.    }

  20. }

Spring Enable*高级应用及原理的更多相关文章

  1. Spring Enable高级应用及原理

    Enable* 之前的文章用到了一些Enable*开头的注解,比如EnableAsync.EnableScheduling.EnableAspectJAutoProxy.EnableCaching等, ...

  2. 【转】Spring学习---Spring IoC容器的核心原理

    [原文] Spring的两个核心概念:IoC和AOP的雏形,Spring的历史变迁和如今的生态帝国. IoC和DI的基本概念 IoC(控制反转,英文含义:Inverse of Control)是Spr ...

  3. Spring Boot的自动配置原理及启动流程源码分析

    概述 Spring Boot 应用目前应该是 Java 中用得最多的框架了吧.其中 Spring Boot 最具特点之一就是自动配置,基于Spring Boot 的自动配置,我们可以很快集成某个模块, ...

  4. spring boot 自动装配的原理

    参考: https://blog.csdn.net/Dongguabai/article/details/80865599.如有侵权,请联系本人删除! 入口: import org.springfra ...

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

    本篇博文的目录: 一:前言 二:spring的配置文件 三:依赖的第三方库.使用技术.代码布局 四:Document实现 五:获取Element的实现 六:解析Element元素 七:Bean创造器 ...

  6. Spring AOP高级——源码实现(3)AopProxy代理对象之JDK动态代理的创建过程

    spring-aop-4.3.7.RELEASE  在<Spring AOP高级——源码实现(1)动态代理技术>中介绍了两种动态代理技术,当然在Spring AOP中代理对象的生成也是运用 ...

  7. Spring Boot 文件上传原理

    首先我们要知道什么是Spring Boot,这里简单说一下,Spring Boot可以看作是一个框架中的框架--->集成了各种框架,像security.jpa.data.cloud等等,它无须关 ...

  8. Spring系列之IOC的原理及手动实现

    目录 Spring系列之IOC的原理及手动实现 Spring系列之DI的原理及手动实现 导语 Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架.也是几乎所有J ...

  9. Spring系列之DI的原理及手动实现

    目录 Spring系列之IOC的原理及手动实现 Spring系列之DI的原理及手动实现 前言 在上一章中,我们介绍和简单实现了容器的部分功能,但是这里还留下了很多的问题.比如我们在构造bean实例的时 ...

随机推荐

  1. Arduino Leonardo读取DHT22温湿度传感器

    首先在该地址下载库:https://codeload.github.com/nethoncho/Arduino-DHT22/zip/master 使用以下代码测试: /**************** ...

  2. java 数据结构与算法---链表

    原理来自百度百科  一.链表的定义 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运 ...

  3. BAT等公司必问的8道Java经典面试题,你都会了吗?

    工作多年以及在面试中,我经常能体会到,有些面试者确实是认真努力工作,但坦白说表现出的能力水平却不足以通过面试,通常是两方面原因: 1.“知其然不知其所以然”.做了多年技术,开发了很多业务应用,但似乎并 ...

  4. 子类的实例给父类的引用 在编译器时候 jvm认为他是父类的实例 只会去寻找父类里面的方法 如果调用子类里面的属性或者方法时候 jvm会认为该引用并没有 所以会报错

  5. Django 2.0 学习(20):Django 中间件详解

    Django 中间件详解 Django中间件 在Django中,中间件(middleware)其实就是一个类,在请求到来和结束后,Django会根据自己的规则在合适的时机执行中间件中相应的方法. 1. ...

  6. pyCharm最新2019激活码

    破解补丁激活优点:到期时间为2099年,基本为永久啦 缺点:相对服务器激活麻烦些,但是一共只需要3个步骤,其实并不麻烦 一.下载 https://pan.baidu.com/s/1mcQM8CLUnw ...

  7. PowerDesigner在生成SQL时报错Generation aborted due to errors detected during the verification of the mod

    一.本章节要用到  ODBC连接数据库直接创建表,请先创建连接库的ODBC 请参考  新建  http://www.cnblogs.com/wdw31210/p/7580286.html 二.生成 去 ...

  8. LINQ 模糊搜索

    IList<entity> ls = new List<entity>(); ls = (from k in ls where k.Name.Contains("sa ...

  9. MT【153】缩小包围圈

    (清华2017.4.29标准学术能力测试3) 集合$S=\{1,2,\cdots,25\}$,$A\subseteq S$,且$A$ 的所有子集中元素之和不同.则下列选项正确的有(      ) A. ...

  10. BZOJ 2527 Meteors | 整体二分

    BZOJ 2527 Meteors 题意 一个圆环上有m个位置,编号为1~m,分别属于n个国家. 有k个时刻,每个时刻都会给圆环上的一个区间中每个位置的值加上一个数. 每个国家有一个目标,问对于每个国 ...