出自:http://blog.csdn.net/qq_26525215

@EnableAspectJAutoProxy

@EnableAspectJAutoProxy注解 激活Aspect自动代理

  1. <aop:aspectj-autoproxy/>

开启对AspectJ自动代理的支持。

在用到AOP的自动代理的时候用,如果你理解了Java的动态代理,很容易的就会熟悉AOP的自动代理的。

@EnableAsync

@EnableAsync注解开启异步方法的支持。 
这个相信大家都比较熟悉的。对于异步应该都理解的。 
不太熟悉的,可以看这篇博客:-有示例 
【Spring】Spring高级话题-多线程-TaskExecutor

@EnableScheduling

@EnableScheduling注解开启计划任务的支持。

也就是字面上的意思,开启计划任务的支持! 
一般都需要@Scheduled注解的配合。

详情见此博客: 
【Spring】Spring高级话题-计划任务-@EnableScheduling

@EnableWebMVC

@EnableWebMVC注解用来开启Web MVC的配置支持。

也就是写Spring MVC时的时候会用到。

@EnableConfigurationProperties

@EnableConfigurationProperties注解是用来开启对@ConfigurationProperties注解配置Bean的支持。

也就是@EnableConfigurationProperties注解告诉Spring Boot 使能支持@ConfigurationProperties

@EnableJpaRepositories

@EnableJpaRepositories注解开启对Spring Data JPA Repostory的支持。

Spring Data JPA 框架,主要针对的就是 Spring 唯一没有简化到的业务逻辑代码,至此,开发者连仅剩的实现持久层业务逻辑的工作都省了,唯一要做的,就只是声明持久层的接口,其他都交给 Spring Data JPA 来帮你完成!

简单的说,Spring Data JPA是用来持久化数据的框架。

@EnableTransactionManagement

@EnableTransactionManagement注解开启注解式事务的支持。

注解@EnableTransactionManagement通知Spring,@Transactional注解的类被事务的切面包围。这样@Transactional就可以使用了。

@EnableCaching

@EnableCaching注解开启注解式的缓存支持

通过这些简单的@Enable*可以开启一项功能的支持,从而避免自己配置大量的代码,很大程度上降低了使用难度。

我们一起来观察下这些@Enable*注解的源码,可以发现所有的注解都有一个@Import注解。

@Import注解是用来导入配置类的,这也就是说这些自动开启的实现其实是导入了一些自动配置的Bean。

这些导入配置方式主要分为以下三种类型。

@Import注解导入配置方式的三种类型

第一类:直接导入配置类

  1. //
  2. // Source code recreated from a .class file by IntelliJ IDEA
  3. // (powered by Fernflower decompiler)
  4. //
  5.  
  6. package org.springframework.scheduling.annotation;
  7.  
  8. import java.lang.annotation.Documented;
  9. import java.lang.annotation.ElementType;
  10. import java.lang.annotation.Retention;
  11. import java.lang.annotation.RetentionPolicy;
  12. import java.lang.annotation.Target;
  13. import org.springframework.context.annotation.Import;
  14. import org.springframework.scheduling.annotation.SchedulingConfiguration;
  15.  
  16. @Target({ElementType.TYPE})
  17. @Retention(RetentionPolicy.RUNTIME)
  18. @Import({SchedulingConfiguration.class})
  19. @Documented
  20. public @interface EnableScheduling {
  21. }

直接导入配置类SchedulingConfiguration,这个类注解了@Configuration,且注册了一个scheduledAnnotationProcessor的Bean,源码如下:

  1. /*
  2. * Copyright 2002-2015 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16.  
  17. package org.springframework.scheduling.annotation;
  18.  
  19. import org.springframework.beans.factory.config.BeanDefinition;
  20. import org.springframework.context.annotation.Bean;
  21. import org.springframework.context.annotation.Configuration;
  22. import org.springframework.context.annotation.Role;
  23. import org.springframework.scheduling.config.TaskManagementConfigUtils;
  24.  
  25. /**
  26. * {@code @Configuration} class that registers a {@link ScheduledAnnotationBeanPostProcessor}
  27. * bean capable of processing Spring's @{@link Scheduled} annotation.
  28. *
  29. * <p>This configuration class is automatically imported when using the
  30. * @{@link EnableScheduling} annotation. See {@code @EnableScheduling}'s javadoc
  31. * for complete usage details.
  32. *
  33. * @author Chris Beams
  34. * @since 3.1
  35. * @see EnableScheduling
  36. * @see ScheduledAnnotationBeanPostProcessor
  37. */
  38. @Configuration
  39. @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
  40. public class SchedulingConfiguration {
  41.  
  42. @Bean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
  43. @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
  44. public ScheduledAnnotationBeanPostProcessor scheduledAnnotationProcessor() {
  45. return new ScheduledAnnotationBeanPostProcessor();
  46. }
  47.  
  48. }

第二类:依据条件选择配置类

EnableAsync 注解核心代码:

  1. @Target(ElementType.TYPE)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Import(AsyncConfigurationSelector.class)
  5. public @interface EnableAsync {
  6. Class<? extends Annotation> annotation() default Annotation.class;
  7. boolean proxyTargetClass() default false;
  8. AdviceMode mode() default AdviceMode.PROXY;
  9. int order() default Ordered.LOWEST_PRECEDENCE;
  10.  
  11. }

AsyncConfigurationSelector通过条件来选择需要导入的配置类, 
AsyncConfigurationSelector的根接口为ImportSelector,这个接口需要重写selectImports方法,在此方法内进行事先条件判断。

在下面的源码中,若adviceMode为PORXY,则返回ProxyAsyncConfiguration这个配置类。 
若activeMode为ASPECTJ,则返回AspectJAsyncConfiguration配置类。 
源码如下:

  1. public class AsyncConfigurationSelector extends AdviceModeImportSelector<EnableAsync> {
  2.  
  3. private static final String ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME =
  4. "org.springframework.scheduling.aspectj.AspectJAsyncConfiguration";
  5.  
  6. /**
  7. * {@inheritDoc}
  8. * @return {@link ProxyAsyncConfiguration} or {@code AspectJAsyncConfiguration} for
  9. * {@code PROXY} and {@code ASPECTJ} values of {@link EnableAsync#mode()}, respectively
  10. */
  11. @Override
  12. public String[] selectImports(AdviceMode adviceMode) {
  13. switch (adviceMode) {
  14. case PROXY:
  15. return new String[] { ProxyAsyncConfiguration.class.getName() };
  16. case ASPECTJ:
  17. return new String[] { ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME };
  18. default:
  19. return null;
  20. }
  21. }
  22.  
  23. }

第三类:动态注册Bean

  1. @Target(ElementType.TYPE)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Import(AspectJAutoProxyRegistrar.class)
  5. public @interface EnableAspectJAutoProxy {
  6. boolean proxyTargetClass() default false;
  7. }

AspectJAutoProxyRegistrar 事先了ImportBeanDefinitionRegistrar接口,ImportBeanDefinitionRegistrar的作用是在运行时自动添加Bean到已有的配置类,通过重写方法:

  1. @Override
  2. public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry)

其中,AnnotationMetadata参数用来获得当前配置类上的注解; BeanDefinittionRegistry参数用来注册Bean。 
源码如下:

  1. class AspectJAutoProxyRegistrar implements ImportBeanDefinitionRegistrar {
  2.  
  3. /**
  4. * Register, escalate, and configure the AspectJ auto proxy creator based on the value
  5. * of the @{@link EnableAspectJAutoProxy#proxyTargetClass()} attribute on the importing
  6. * {@code @Configuration} class.
  7. */
  8. @Override
  9. public void registerBeanDefinitions(
  10. AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
  11.  
  12. AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);
  13.  
  14. AnnotationAttributes enableAJAutoProxy =
  15. AnnotationConfigUtils.attributesFor(importingClassMetadata, EnableAspectJAutoProxy.class);
  16. if (enableAJAutoProxy.getBoolean("proxyTargetClass")) {
  17. AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
  18. }
  19. }
  20.  
  21. }
  1.  

Spring高级话题-@Enable***注解的工作原理的更多相关文章

  1. Spring高级特性之三:@Enable*注解的工作原理

    Spring Boot中阐述热插拔技术的时候,简单地提及@Enable*注解.随着多种框架的应用及深入了解,@Enable*这个注解在各种框架中应用相当普及. 那么@Enable*注解工作原理是怎么样 ...

  2. Spring的@Enable*注解的工作原理

    转自:https://blog.csdn.net/chengqiuming/article/details/81586948 一 列举几个@Enable*注解的功能 @EnableAspectJAut ...

  3. springBoot @Enable*注解的工作原理

    使用注解实现异步 RunnableDemo类 package com.boot.enable.bootenable; import org.springframework.scheduling.ann ...

  4. @Enable*注解的工作原理

    @EnableAspectJAutoProxy @EnableAsync @EnableScheduling @EnableWebMv @EnableConfigurationProperties @ ...

  5. Spring Boot实战(3) Spring高级话题

    1. Spring Aware Spring的依赖注入的最大亮点就是你所有的Bean对Spring容器的存在是没有意识的.即你可以将你的容器替换成别的容器. 实际项目中,不可避免地会用到Spring容 ...

  6. EnableAutoConfiguration注解的工作原理(org.springframework.boot.autoconfigure.EnableAutoConfiguration=core.bean.MyConfig)

    EnableAutoConfiguration注解的工作原理(org.springframework.boot.autoconfigure.EnableAutoConfiguration=core.b ...

  7. Spring Boot实战笔记(九)-- Spring高级话题(组合注解与元注解)

    一.组合注解与元注解 从Spring 2开始,为了响应JDK 1.5推出的注解功能,Spring开始大量加入注解来替代xml配置.Spring的注解主要用来配置注入Bean,切面相关配置(@Trans ...

  8. Spring中的@Enable注解

    本文转载自SpringBoot中神奇的@Enable注解? 导语 在SpringBoot开发过程,我们经常会遇到@Enable开始的好多注解,比如@EnableEurekaServer.@Enable ...

  9. 注解 @EnableFeignClients 工作原理

    概述在Spring cloud应用中,当我们要使用feign客户端时,一般要做以下三件事情 : 使用注解@EnableFeignClients启用feign客户端:示例 : @SpringBootAp ...

随机推荐

  1. Android学习问题记录之open failed EACCES (Permission denied)

    1.问题描述 Android调用相机拍照保存,然后读取保存好的照片,在读取照片时出现异常(该异常是因为没有SD卡的读取权限所致): 11-08 11:07:46.421 8539-8539/com.c ...

  2. Ubuntu12.04中Gvim无法固定到启动器的解决办法

    sudo vim /usr/share/applications/gvim.desktop 修改Categories键值如下: Categories=Application;Development;

  3. BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 【LCT】【分块】

    BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始, ...

  4. 使用python处理selenium中的xpath定位元素的模糊匹配问题

    # 用contains,寻找页面中style属性值包含有sp.gif这个关键字的所有div元素,其中@后面可以跟该元素任意的属性名. self.driver.find_element_by_xpath ...

  5. 接口测试基础——第6篇unittest模块(一)

    我们先来简单介绍一下unittest框架,先上代码,跟住了哦~~ 1.建立如下结构的文件夹: 注意,上面的文件夹都是package,也就是说你在new新建文件夹的时候不要选directory,而是要选 ...

  6. python 读取 xlsx

    >>> xl = pd.ExcelFile("dummydata.xlsx") >>> xl.sheet_names [u'Sheet1', u ...

  7. grpc xservice 使用

    1. 安装(此处比较简单) dep 包管理 配置环境变量 GOPATH/bin GO/bin protoc 下载并配置环境变量 2. xservice 安装 a. 预备(一些需要的依赖) mkdir ...

  8. nomad 安装(单机)试用

    备注:     nomad  可以实现基础设施的调度管理,类似kubernetes ,但是在多云以及多平台支持上比较好,     还是hashicrop 工具出品的,很不错,同时本地测试因为使用默认的 ...

  9. Emacs golang用户代码无法补全问题

    现象:Emacs使用company-go可以正常补全标准库函数和go get安装库函数:而对于自已写的代码,只能补全当前包下的变量和函数. 原因:company-go后台是使用了gocode,而goc ...

  10. Erlang generic standard behaviours -- gen_server hibernate

    hibernate 主要用于在内存空闲时,通过整理进程的stack,回收进程的heap 来达到回收内存节省资源的效果. hibernate 可用于OTP 进程以及普通进程, hibernate 的官方 ...