SpringBoot 常用注解
@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 常用注解的更多相关文章
- SpringBoot 常用注解(持续更新)
SpringBoot 常用注解 @SpringBootApplication @Bean @ComponentScan @ControllerAdvice @ExceptionHandler @Res ...
- SpringBoot 入门篇(二) SpringBoot常用注解以及自动配置
一.SpringBoot常用注解二.SpringBoot自动配置机制SpringBoot版本:1.5.13.RELEASE 对应官方文档链接:https://docs.spring.io/spring ...
- 接近8000字的Spring/SpringBoot常用注解总结!安排!
0.前言 大家好,我是 Guide 哥!这是我的 221 篇优质原创文章.如需转载,请在文首注明地址,蟹蟹! 本文已经收录进我的 75K Star 的 Java 开源项目 JavaGuide:http ...
- Spring/SpringBoot常用注解总结
转自:[Guide哥] 0.前言 可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用法,掌握搞懂,使 ...
- SpringBoot介绍,快速入门小例子,目录结构,不同的启动方式,SpringBoot常用注解
SpringBoot介绍 引言 为了使用ssm框架去开发,准备ssm框架的模板配置 为了Spring整合第三方框架,单独的去编写xml文件 导致ssm项目后期xml文件特别多,维护xml文件的成本也是 ...
- 菜鸟的springboot常用注解总结
菜鸟的springboot常用注解总结 0.前言 可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用 ...
- SpringBoot常用注解的介绍及使用 - 转载
常用注解 @springBootApplication 系统启动类注解,此注解是个组合注解,包括了:@SpringBootConfiguration,@EnableAutoConfiguration, ...
- 结合参数接收响应转换原理讲解SpringBoot常用注解
一.常用注解回顾 1.1 @RequestBody与@ResponseBody //注意并不要求@RequestBody与@ResponseBody成对使用. public @ResponseBody ...
- springboot系列五、springboot常用注解使用说明
一.controller相关注解 1.@Controller 控制器,处理http请求. 2.@RespController Spring4之后新加的注解,原来返回json需要@ResponseBod ...
- SpringBoot常用注解(三)
最全的Java常用开发注解 转 https://blog.csdn.net/weixin_40753536/article/details/81285046 Spring ...
随机推荐
- Linux下安装numpy
转自:https://blog.csdn.net/abc_321a/article/details/82056019 1.下载源码包 ,命令如下 wget http://jaist.dl.source ...
- JUnit学习笔记-0-JUnit启动类
[说明]:本文基于JUnit4.13版本代码,JDK1.8.0_151环境,使用工具为Eclipse,版本为Oxygen.1a Release (4.7.1a) [图示]: [正文]:JUnit4.1 ...
- php倒计时
<form name="form1"> <div align="center" align="center"> &l ...
- MySQL的计算时间差
一.MySQL计算两个日期的时间差 TIMESTAMPDIFF(DAY, datetime1, datetime2); 第一个参数为比较类型,有day, month, year, hour等: 第二个 ...
- 没有job offer,拿加拿大工签PGWP回国如何续签加拿大小签?
很多同学因为在加拿大毕业后申请了三年的工作签证PGWP之后匆匆回国,没有来得及续签小签,但是回国一段时间之后又想要回加拿大,想要用自己的三年工签来续自己的小签.拿了加拿大PGWP没有job offe ...
- ubuntu16.04下安装g2o
根本不需要编译源码直接一行命令就可以 sudo apt-get install libpcl-dev 如果没有安装pcl_viewer就再加一行命令 sudo apt-get install pcl- ...
- c#项目减少源代码大小
这次的代码缩减主要通过了这几个方面 1.bin和obj文件的删除(以前真的不知道,只是通过右键属性发现这些文件太大,然后上网搜索才知道,这些文件在源代码备份的时候是建议删掉的) 删掉的好处: 1.减少 ...
- CENTOS7常用的基础命令集合(一)
目录(?)[-] CentOS7 常用命令集合 常用命令 文件与目录操作 查看文件内容 文本内容处理 查询操作 压缩解压 yum安装器 网络相关 系统相关 系统服务启动相关 防火墙相关 RPM包管理 ...
- app锁定屏幕方向,某一个界面支持屏幕旋转~
AppDelegate.h 加 @property (nonatomic, assign) BOOL allowRotation; Appdelegate.m加 -(NSUInteger)applic ...
- WSGI and Paste学习笔记
The Problem Lots of web frameworks Zope, Quixote, Webware, SkunkWeb and Twisted Web etc Applications ...