spring boot 2.0启动 失败_spring-boot启动过程与starter分析

f82df04d06cc2e0006184e6d9200eb3d.png

具体可关注公众号: 戏说码农职场


元注解

元标签有 @Retention、@Documented、@Target、@Inherited、@Repeatable 5 种。

FactoryBean getObject

1. 静态注入

b4fe3729ec31926f54d4fc07a68eaeeb.png

2. 动态引入

基于ImportSelector接口实现

56e7f225e93f0862139678e8b6819807.png

7957c934773bae5ff52311d64141531b.png

bc5d29cf49d392b5d65505060b86dc6b.png

3. 基于ImportBeanDefinition

abc076b099d2be0bef03890ff7023fbd.png

4. spring.factories

dcf585f8b8d2fec994704e37e2de04a0.png

09302fe04acef2a1e3425d1abe2ab7af.png

@conditional 加载到容器的条件

第一个决定要加载的类型, 第二个决定是否加载?

d4dab4f399e2d5d45a6d4d567a031f03.png

扩展下. 其实springCloud组件 很多用到@Import @Conditional , 还有些select等, 顺藤摸瓜 我简答列列举几个:

  1. SpringBoot 启动注解

@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 {}

303cacbf7fe577543518f35d6b6e0a83.png

最顶层继承了ImportSelector 和 Aware接口:

  • ImportSelector---控制哪些类可以被加载到Spring容器进行管理
public interface ImportSelector {

	/**
	 * Select and return the names of which class(es) should be imported based on
	 * the {@link AnnotationMetadata} of the importing @{@link Configuration} class.
	 */
	String[] selectImports(AnnotationMetadata importingClassMetadata);

}
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的内容, 简单列举出现:

1821655a76f69d08cf0cea6e126bedb1.png

说明下:

SpringFactoriesLoader属于Spring框架私有的一种扩

展方案,其主要功能就是从指定的配置文件META-INF/spring.factories加载配置。

public final class SpringFactoriesLoader {

	/**
	 * The location to look for factories.
	 * <p>Can be present in multiple JAR files.
	 */
	public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";


	private static final Log logger = LogFactory.getLog(SpringFactoriesLoader.class);

	private static final Map<ClassLoader, MultiValueMap<String, String>> cache = new ConcurrentReferenceHashMap<>();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值