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的实现,以及常规的组件类。

相关推荐
2401_8576363914 分钟前
计算机课程管理平台:Spring Boot与工程认证的结合
java·spring boot·后端
懒惰的bit2 小时前
基础网络安全知识
学习·web安全·1024程序员节
2401_858286113 小时前
L7.【LeetCode笔记】相交链表
笔记·leetcode·链表
2401_857610035 小时前
多维视角下的知识管理:Spring Boot应用
java·spring boot·后端
代码小鑫5 小时前
A027-基于Spring Boot的农事管理系统
java·开发语言·数据库·spring boot·后端·毕业设计
Natural_yz5 小时前
大数据学习09之Hive基础
大数据·hive·学习
龙中舞王5 小时前
Unity学习笔记(2):场景绘制
笔记·学习·unity
Natural_yz5 小时前
大数据学习10之Hive高级
大数据·hive·学习
love_and_hope6 小时前
Pytorch学习--神经网络--完整的模型训练套路
人工智能·pytorch·python·深度学习·神经网络·学习
青椒大仙KI116 小时前
24/11/7 算法笔记 PCA主成分分析
笔记·算法·信息可视化