Springboot启动过程
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot启动过程相关的知识,希望对你有一定的参考价值。
参考技术A 元注解元标签有 @Retention、@Documented、@Target、@Inherited、@Repeatable 5 种。
FactoryBean getObject
1. 静态注入
2. 动态引入
基于ImportSelector接口实现
3. 基于ImportBeanDefinition
4. spring.factories
@conditional 加载到容器的条件
第一个决定要加载的类型, 第二个决定是否加载?
扩展下. 其实springCloud组件 很多用到@Import @Conditional , 还有些select等, 顺藤摸瓜 我简答列列举几个:
@SpringBootApplication-->@EnableAutoConfiguration
@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
@Import(AutoConfigurationImportSelector.class)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration
重点说下Selector:
public class AutoConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware,
ResourceLoaderAware, BeanFactoryAware, EnvironmentAware, Ordered
最顶层继承了ImportSelector 和 Aware接口:
public interface ImportSelector
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes)
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
getBeanClassLoader());
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
+ "are using a custom packaging, make sure that file is correct.");
return configurations;
protected Class<?> getSpringFactoriesLoaderFactoryClass()
return EnableAutoConfiguration.class;
其实最终是寻找EnableAutoConfiguration对应的类组合, EnableAutoConfiguration来自package org.springframework.boot.autoconfigure;
所以最终初始化时, 是加载了spring-boot-autoconfigure里面的spring.factories的内容, 简单列举出现:
说明下:
SpringFactoriesLoader属于Spring框架私有的一种扩
展方案,其主要功能就是从指定的配置文件META-INF/spring.factories加载配置。
public final class SpringFactoriesLoader
以上是关于Springboot启动过程的主要内容,如果未能解决你的问题,请参考以下文章