Spring Boot常用注解类

常用注解类

复制代码
packge org.springframework.boot.autoconfigure

@EnableAutoConfiguration

Enable auto-configuration of the Spring Application Context, attempting to guess and configure beans that you are likely to need. Auto-configuration classes are usually applied based on your classpath and what beans you have defined. For example, if you have tomcat-embedded.jar on your classpath you are likely to want a TomcatServletWebServerFactory (unless you have defined your own ServletWebServerFactory bean).

@EnableAutoConfiguration用于开启Spring应用上下文的自动配置,尝试猜测并配置你可能需要的beans。哪些自动配置类的会被采用,通常基于你的classpath和你定义的beans。例如,如果你的classpath有tocat-embedded.jar,你可能需要一个TomcatServletWebServerFactory (除非你已定义了自己的ServletWebServerFactory bean)

When using @SpringBootApplication, the auto-configuration of the context is automatically enabled and adding this annotation has therefore no additional effect.

当使用@SpringBootApplication,上下文的自动配置就自动启用了,这时添加@EnableAutoConfiguration不会有额外的影响。

Auto-configuration tries to be as intelligent as possible and will back-away as you define more of your own configuration. You can always manually exclude() any configuration that you never want to apply (use excludeName() if you don't have access to them). You can also exclude them via the spring.autoconfigure.exclude property. Auto-configuration is always applied after user-defined beans have been registered.

自动配置力求尽可能地智能,当你定义了更多的自己的配置,相关的自动配置就会减少。你可以手动排除你不想使用的配置(使用excludeName(), 如果你不需要访问它们)你也可以用spring.autoconfigure.exclude属性来排除不需要的配置。自动配置总是在用户定义的beans注册后被调用。

The package of the class that is annotated with @EnableAutoConfiguration, usually via @SpringBootApplication, has specific significance and is often used as a 'default'. For example, it will be used when scanning for @Entity classes. It is generally recommended that you place @EnableAutoConfiguration (if you're not using @SpringBootApplication) in a root package so that all sub-packages and classes can be searched.

Auto-configuration classes are regular Spring @Configuration beans. They are located using the SpringFactoriesLoader mechanism (keyed against this class). Generally auto-configuration beans are @Conditional beans (most often using @ConditionalOnClass and @ConditionalOnMissingBean annotations).

使用@EnableAutoConfiguration注解的类所在的包,通常使用@SpringBootApplication,有特别的意义且经常作为default包。例如,扫描的时候默认扫描default包下面的Entity。通常建议把@EnableAutoConfiguration放在根包里,以便所有的子包和类可以被搜索到。Auto-configuration类是常规的Spring @Configuration beans。自动配置类采用SpringFactoriesLoader加载。通常自动配置beans就是@Conditional Beans。(最常用的是@ConditionalOnClass, @ConditionalOnMissingBean)

@SpringBootApplication

Indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. This is a convenience annotation that is equivalent to declaring @Configuration , @EnableAutoConfiguration and @ComponentScan.

@SpringBootApplication表示一个配置类,该类声明一个或多个@Bean方法并且触发自动配置和组件扫描。使用一个@SpringBootApplication和同时使用@Configuration、@EnableAutoConfiguration、@ComponentScan是等效的。

@Import

Indicates one or more component classes to import --- typically @Configuration classes.

Provides functionality equivalent to the <import/> element in Spring XML. Allows for importing @Configuration classes, ImportSelector and ImportBeanDefinitionRegistrar implementations, as well as regular component classes (as of 4.2; analogous to AnnotationConfigApplicationContext.register).

@Import指示导入一个或多个组件类----通常是@Configuration类。和Spring xml文件里的<import/>标签等价。可以导入@Configruation类、ImportSelector和ImportBeanDefinitionRegistrar的实现,以及常规的组件类。

相关推荐
优思学院42 分钟前
优思学院|如何从零开始自己学习六西格玛?
大数据·运维·服务器·学习·六西格玛黑带·cssbb
计算机程序设计开发44 分钟前
计算机毕业设计公交站点线路查询网站登录注册搜索站点线路车次/springboot/javaWEB/J2EE/MYSQL数据库/vue前后分离小程序
数据库·vue.js·spring boot·课程设计·计算机毕业设计
骆晨学长1 小时前
基于SpringBoot的校园失物招领系统
java·spring boot
计算机编程-吉哥1 小时前
计算机毕业设计 二手图书交易系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试
java·spring boot·毕业设计·毕业论文·计算机毕业设计选题·计算机毕业设计开题报告·二手图书交易系统
qq_35323353891 小时前
【原创】java+springboot+mysql高校社团网系统设计与实现
java·spring boot·mysql
LN花开富贵1 小时前
stm32g431rbt6芯片中VREF+是什么?在电路中怎么设计?
笔记·stm32·单片机·嵌入式硬件·学习
怀九日1 小时前
C++(学习)2024.9.18
开发语言·c++·学习·面向对象·引用·
一道秘制的小菜1 小时前
C++第七节课 运算符重载
服务器·开发语言·c++·学习·算法
qq21084629531 小时前
【stm32笔记】使用rtt-studio与stm32CubeMx联合创建项目
笔记·stm32·嵌入式硬件
evanYang_1 小时前
Spring Boot配置文件敏感信息加密
spring boot·后端·oracle