@SpringBootApplication
进入@SpringBootApplication注解可以看见@SpringBootApplication中有7个注解;其中有4个注解为元注解来对注解进行说明,还有三个注解:@SpringBootConfiguration;@EnableAutoConfiguration;@ComponentScan
@SpringBootConfiguration
同上,@SpringBootConfiguration中实际注解是@Configuration
跟入@Configuration
可见@Configuration实际上是一个@Component标记的注解,而在Spring注解中就可以知道,@Component是标记当前类为Spring中的一个Bean;
拓展:其中的proxyBeanMethods()方法看方法名就可以看出是一个代理Bean方法,默认值为true;
则容器会在使用时创建一个Bean的代理对象,不管创建几次Bean对象都是一个
@ComponentScan
这个注解学习过Spring注解,应该知道是和上面@Configuration注解内部的@Component是相对应的,目的就是扫描来获取哪些类要被注册为Bean
其中默认扫描当前类所在目录及其子包下路径。
@EnableAutoConfiguration
看英文很简洁明了的就知道这注解时开启自动配置的。
进入后注意两个注解:
@AutoConfigurationPackage 和 @Import({AutoConfigurationImportSelector.class})
@AutoConfigurationPackage
出现Import这个注解就肯定有他的道理,说明这个Registrar类需要注意一下。进去瞧瞧
自动定位到了这个位置
先看第一个方法RegisterBeanDefinitions → 还是那么的见名知意,就可以看出这个方法就是注册Bean定义的一个方法,
进去看这个方法就更好知道这是一个注册Bean的方法
到这里就可以知道@EnableAutoConfiguration中的@AutoConfigurationPackage中如何实现了SpringBoot自动创建Bean。
接下来看@EnableAutoConfiguration的@Import中的AutoConfigurationImportSelector类,为什么要导入他呢
进入这个类后,需要连跳几下,大家上车要加速了。
连跳+1
连跳+2