@SpringBootApplication 

这个配置等同于:@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三个配置。

@EnableAutoConfiguration

Spring Boot自动配置(auto-configuration),可以将@EnableAutoConfiguration或者@SpringBootApplication注解添加到一个@Configuration类上来选择自动配置。

如果发现应用了你不想要的特定自动配置类,可以使用@EnableAutoConfiguration注解的排除属性来禁用它们。

@Configuration

相当于传统的xml配置文件,如果有些第三方库需要用到xml文件,建议仍然通过@Configuration类作为项目的配置主类,可以使用@ImportResource注解加载xml配置文件。

@ComponentScan

@ComponentScan 注解对应XML配置形式中的 <context:component-scan> 元素,表示启用组件扫描,Spring 会自动扫描所有通过注解配置的 bean,然后将其注册到 IOC 容器中。

可以通过 basePackages 等属性来指定 @ComponentScan 自动扫描的范围,如果不指定,默认从声明 @ComponentScan 所在类的 package 进行扫描。

正因为如此,SpringBoot 的启动类都默认在 src/main/java 下。

@ImportResource

用来加载xml配置文件。

@Import

第一类:直接导入配置类

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

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

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

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AsyncConfigurationSelector.class)
public @interface EnableAsync {
Class<? extends Annotation> annotation() default Annotation.class;
boolean proxyTargetClass() default false;
AdviceMode mode() default AdviceMode.PROXY;
int order() default Ordered.LOWEST_PRECEDENCE; }

AsyncConfigurationSelector通过条件来选择需要导入的配置类,

AsyncConfigurationSelector的根接口为ImportSelector,这个接口需要重写selectImports方法,在此方法内进行事先条件判断。

若adviceMode为PORXY,则返回ProxyAsyncConfiguration这个配置类。

若activeMode为ASPECTJ,则返回AspectJAsyncConfiguration配置类。

第三类:动态注册Bean

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

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

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

其中,AnnotationMetadata参数用来获得当前配置类上的注解;

BeanDefinittionRegistry参数用来注册Bean。

@EnableAspectJAutoProxy

@EnableAspectJAutoProxy注解 激活Aspect自动代理

<aop:aspectj-autoproxy/>

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

@EnableAsync

@EnableAsync注解开启异步方法的支持。

@EnableScheduling

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

@EnableWebMVC

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

@EnableConfigurationProperties

@EnableConfigurationProperties 注解表示对 @ConfigurationProperties 的内嵌支持。

默认会将对应 Properties Class 作为 bean 注入的 IOC 容器中,即在相应的 Properties 类上不用加 @Component 注解。

@EnableJpaRepositories

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

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

@EnableTransactionManagement

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

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

@EnableCaching

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

@Conditional

@Conditional 注解表示在满足某种条件后才初始化一个 bean 或者启用某些配置。

它一般用在由 @Component、@Service、@Configuration 等注解标识的类上面,或者由 @Bean 标记的方法上。

如果一个 @Configuration 类标记了 @Conditional,则该类中所有标识了 @Bean 的方法和 @Import 注解导入的相关类将遵从这些条件。

在 Spring 里可以很方便的编写你自己的条件类,所要做的就是实现 Condition 接口,并覆盖它的 matches()方法。

SpringBoot 常用注解的更多相关文章

  1. SpringBoot 常用注解(持续更新)

    SpringBoot 常用注解 @SpringBootApplication @Bean @ComponentScan @ControllerAdvice @ExceptionHandler @Res ...

  2. SpringBoot 入门篇(二) SpringBoot常用注解以及自动配置

    一.SpringBoot常用注解二.SpringBoot自动配置机制SpringBoot版本:1.5.13.RELEASE 对应官方文档链接:https://docs.spring.io/spring ...

  3. 接近8000字的Spring/SpringBoot常用注解总结!安排!

    0.前言 大家好,我是 Guide 哥!这是我的 221 篇优质原创文章.如需转载,请在文首注明地址,蟹蟹! 本文已经收录进我的 75K Star 的 Java 开源项目 JavaGuide:http ...

  4. Spring/SpringBoot常用注解总结

    转自:[Guide哥] 0.前言 可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用法,掌握搞懂,使 ...

  5. SpringBoot介绍,快速入门小例子,目录结构,不同的启动方式,SpringBoot常用注解

    SpringBoot介绍 引言 为了使用ssm框架去开发,准备ssm框架的模板配置 为了Spring整合第三方框架,单独的去编写xml文件 导致ssm项目后期xml文件特别多,维护xml文件的成本也是 ...

  6. 菜鸟的springboot常用注解总结

    菜鸟的springboot常用注解总结 0.前言 可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用 ...

  7. SpringBoot常用注解的介绍及使用 - 转载

    常用注解 @springBootApplication 系统启动类注解,此注解是个组合注解,包括了:@SpringBootConfiguration,@EnableAutoConfiguration, ...

  8. 结合参数接收响应转换原理讲解SpringBoot常用注解

    一.常用注解回顾 1.1 @RequestBody与@ResponseBody //注意并不要求@RequestBody与@ResponseBody成对使用. public @ResponseBody ...

  9. springboot系列五、springboot常用注解使用说明

    一.controller相关注解 1.@Controller 控制器,处理http请求. 2.@RespController Spring4之后新加的注解,原来返回json需要@ResponseBod ...

  10. SpringBoot常用注解(三)

    最全的Java常用开发注解 转   https://blog.csdn.net/weixin_40753536/article/details/81285046              Spring ...

随机推荐

  1. Python从入门到精通之eighth!

    函数式编程与内置函数 函数作用域: def test1(): print('in the test1') def test(): print('in the test') return test1() ...

  2. ES部署报错 max file size 和 kibana 报错File size limit exceeded

    启动失败一 ERROR: [2] bootstrap checks failed [1]: max file descriptors [4096] for elasticsearch process ...

  3. call和apply(学习笔记)

    call() call() 方法调用一个函数, 其具有一个指定的this值和分别地提供的参数(参数的列表). 语法: function.call(thisArg, arg1, arg2, ...) 参 ...

  4. DarwinStreamServer 6.0.3 rtsp服务器搭建

    14:46:34 环境:Centos 7.3 编译安装 1.下载Darwin源码 http://dss.macosforge.org/downloads/DarwinStreamingSrvr6.0. ...

  5. 无网 离线状态下pip3安装 django等软件

    https://stackoverflow.com/questions/7300321/how-to-use-pythons-pip-to-download-and-keep-the-zipped-f ...

  6. PWM of STM32

    下面是STM32用来产生PWM得文件,分别是PWM.c和PWM.h /***************************************************************** ...

  7. SecureCRT使用帮助

    文件上传下载 1. 安装 yum -y install lrzsz (参数-y中"y"的意思是:当安装过程提示选择全部为"yes") 2.上传 第一种方式:rz ...

  8. Jeecg框架简介

    官方地址:http://www.jeecg.org/

  9. Maven package 报错解决记录以及编译scala的pom.xml

    可以打包的pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:/ ...

  10. Web browser发展演变

    浏览器是指可以显示网页服务器或者文件系统的HTML文件内容,并让用户与这些文件交互的一种软件.网页浏览器主要通过HTTP协议与网页服务器交互并获取网页,这些网页由URL指定,文件格式通常为HTML.大 ...