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. Source Insight中的多行注释

    转自:http://www.cnblogs.com/dongzhiquan/archive/2013/03/04/2943448.html 我们经常要对一整段代码进行注释,很多代码编辑器都提供了这样的 ...

  2. 发布.NET Core到IIS

    目录: 支持操作系统 IIS配置 安装.NET Core Windows Server Hosting 部署应用程序 在IIS配置网站 创建一个数据保护注册表项 常见的错误 额外的资源 支持操作系统 ...

  3. TCP连接 三次握手 四次挥手

    前言: TCP协议是面向连接.安全可靠.基于字节流的传输层协议,在进行http协议访问时就用到了tcp连接.在建立TCP连接时需要经历三次握手,断开连接时需要经历四次挥手.在此进行记录. 内容: TC ...

  4. [转帖] windows server 不同版本说明

    Windows Server 2016与Windows Server Current Version区别比较  http://365vcloud.net/2018/04/13/windows-serv ...

  5. 【.Net】c# 让double保留两位小数

    1.Math.Round(0.333333,2);//按照四舍五入的国际标准2.    double dbdata=0.335333;    string str1=String.Format(&qu ...

  6. std::string 赋值为nullptr引起程序崩溃

    一个错误排查两天,std::string赋初值时最好为"", 如果赋初值为nullptr,因为std::string不能和nullptr作比较,所以后面用的时候会引起崩溃. 佩服我 ...

  7. 【ActiveMQ】- 发布/订阅模式

    publish/subscribe 特点:A发送的消息可以被所有监听A的对象的接收,就好比学校的广播,所有的学生都可以收听校园广播信息. 消息生产者: package com.zhiwei.advan ...

  8. 我的shell脚本

    问题:在ip.lt文件中有600个IP,有3个文档模版,三个文档的名称结构都是“ip+一系列字符串”,要求:1.将600个IP分成3分,以三个模版为基础创建600个文档,名字结构与模版相同:2修改60 ...

  9. CentOS7单节点部署redis-cluster

    准备一台机器,系统版本为CentOS7.(注意本文描述的是redis-cluster,不是主从复制) 1.下载软件包 # wget http://download.redis.io/releases/ ...

  10. 解题:USACO18FEB Taming the Herd

    题面 从零开始的DP学习系列之贰(我的DP真的就这么烂TAT) 设DP状态的另一个技巧,考虑题目中有关答案的各种信息 然后这种和结尾有关系的$dp$可以考虑向前找结尾来转移 设$dp[i][j]$表示 ...