您的位置:首页 > 房产 > 家装 > Spring Boot常用注解类

Spring Boot常用注解类

2024/10/7 2:29:54 来源:https://blog.csdn.net/Extra_warrior/article/details/140319075  浏览:    关键词: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的实现,以及常规的组件类。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com