05【掌握】springboot启动分析【难点】
2020-04-25 本文已影响0人
杨不易呀
05【掌握】springboot启动分析【难点】
@SpringBootApplication
@Target(ElementType.***TYPE***)
@Retention(RetentionPolicy.***RUNTIME***)
@Documented
@Inherited
@SpringBootConfiguration 就是对spring里面的@Configruation的包装
@EnableAutoConfiguration 启动自动配置
//扫描
@ComponentScan(excludeFilters = { @Filter(type = FilterType.***CUSTOM***, classes = TypeExcludeFilter.**class**),
@Filter(type = FilterType.***CUSTOM***, classes = AutoConfigurationExcludeFilter.**class**) })
@ConfigurationPropertiesScan
**public** **@interface** SpringBootApplication {
@EnableAutConfigruation
@Target(ElementType.***TYPE***)
@Retention(RetentionPolicy.***RUNTIME***)
@Documented
@Inherited
@AutoConfigurationPackage //自动配置的包
@Import(AutoConfigurationImportSelector.**class**) //导入自己配置配置的配置类
**public** **@interface** EnableAutoConfiguration {
@AutoConfigurationPackage
@Target(ElementType.***TYPE***)
@Retention(RetentionPolicy.***RUNTIME***)
@Documented
@Inherited
@Import(AutoConfigurationPackages.Registrar.**class**) //自己扫描包的配置类
**public** **@interface** AutoConfigurationPackage {
}
进入AutoConfigurationPackages.Registrar.class
image.png进入register方法
image.png可以看出默认扫描的就是当前启动类所在的包
如何加载自动配置类
回到EnableAutoConfiguration
image.png进入AutoConfigurationImportSelector
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-patten>/ </url-patten>
</servlet-mapping>
查看getAutoConfigurationEntry
image.png发现springboot默认加载了124个自动配置类
image.png下面的代码走完之后发现只有22个了。因为有些配置类不满足条件,被移除了
那124个自动配置类是如何加进来的
image.png image.png image.png image.pngspringboot提供了哪些starter
https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/#using-boot-starter