@SpringBootApplication注解的理解
@SpringBootApplication
废话不多说,我们直接贴出来SpringBoot的官方给出的源码:
package org.springframework.boot.autoconfigure;
@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })public @interface SpringBootApplication {}
可以分为这两类:第一类@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited第二类@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan
第一类我们忽略,直接看第二类下面是每一类的源码:
@SpringBootConfiguration
package org.springframework.boot;
@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Configurationpublic @interface SpringBootConfiguration {}
@EnableAutoConfiguration
package org.springframework.boot.autoconfigure;
@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@AutoConfigurationPackage@Import(AutoConfigurationImportSelector.class)public @interface EnableAutoConfiguration {}
@ComponentScan
package org.springframework.context.annotation;
@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE)@Documented@Repeatable(ComponentScans.class)public @interface ComponentScan {}
于此得出@SpringBootApplication由@@ComponentScan组成。
我们来看看官方https://docs.spring.io/spring-boot/docs/2.2.6.RELEASE/api/ API是怎么解释的
@SpringBootApplication 注解在 这个包目录下 org.springframework.boot.autoconfigure;
重点就是这句话“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
.”
我们翻一下:
指示一个配置类,该类声明一个或多个@Bean方法,并且还触发自动配置和组件扫描。这是一个方便注释,等效于声明@ Configuration,@ EnableAutoConfiguration和@ComponentScan。
从中我们可以明确的知道@SpringBootApplication==@ComponentScan
2020-04-25 00:12:18
相关推荐
与卿画眉共浮生 2020-11-13
CaesarHome 2020-11-09
瑞风轻拂 2020-11-02
hellowordmonkey 2020-11-02
丽丽 2020-10-30
流年浅滩 2020-10-23
feinifi 2020-10-14
yangjinpingc 2020-10-09
RickyIT 2020-09-27
lisongchuang 2020-09-27
permanent00 2020-09-15
李玉志 2020-08-19
spring 2020-08-18
meleto 2020-08-17
幸运小侯子 2020-08-14
85407718 2020-08-09
csuzxm000 2020-08-02
咻pur慢 2020-08-02
libowenhit 2020-07-29