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

相关推荐

  1. SpringBoot注解

    2024-07-11 09:36:01       58 阅读
  2. springBoot 注解

    2024-07-11 09:36:01       34 阅读
  3. springboot注释

    2024-07-11 09:36:01       32 阅读
  4. Spring Boot注解

    2024-07-11 09:36:01       24 阅读
  5. SpringBoot项目中注解总结

    2024-07-11 09:36:01       33 阅读
  6. Springboot工具

    2024-07-11 09:36:01       33 阅读
  7. SpringBoot20个注解及其作用

    2024-07-11 09:36:01       25 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 09:36:01       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 09:36:01       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 09:36:01       57 阅读
  4. Python语言-面向对象

    2024-07-11 09:36:01       68 阅读

热门阅读

  1. Perl伪哈希探秘:深入理解Perl中的高级数据结构

    2024-07-11 09:36:01       22 阅读
  2. Python:引号应用、字符串应用

    2024-07-11 09:36:01       23 阅读
  3. Hadoop之HDFS重点架构原理简介

    2024-07-11 09:36:01       22 阅读
  4. Spark SQL----ALTER DATABASE

    2024-07-11 09:36:01       19 阅读
  5. SpringBoot3+Redis实现分布式锁

    2024-07-11 09:36:01       18 阅读
  6. 五种常见排序算法

    2024-07-11 09:36:01       18 阅读
  7. uniapp 防止重复提交数据

    2024-07-11 09:36:01       21 阅读
  8. 通过实例说明.NET Autofac依赖注入的多种方式

    2024-07-11 09:36:01       22 阅读
  9. .NET 9 预览版 5 发布

    2024-07-11 09:36:01       26 阅读
  10. 【Android12】第三方APP开机自启

    2024-07-11 09:36:01       24 阅读
  11. 深入理解CSS中的透明效果实现

    2024-07-11 09:36:01       17 阅读
  12. mac查看31295端口被占

    2024-07-11 09:36:01       21 阅读
  13. 简述框架和函数库的区别

    2024-07-11 09:36:01       22 阅读
  14. WPF自定义模板--ToggleButton

    2024-07-11 09:36:01       21 阅读