【细读Spring Boot源码】prepareContext之load

前言

启动过程中准备上下文中有一步加载在资源,下面看下详情

详情

调用点

private void prepareContext(DefaultBootstrapContext bootstrapContext, ConfigurableApplicationContext context,
			ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
			ApplicationArguments applicationArguments, Banner printedBanner) {
	省略。。。
	load(context, sources.toArray(new Object[0]));
	省略。。。
}

load方法

protected void load(ApplicationContext context, Object[] sources) {
	if (logger.isDebugEnabled()) {
		logger.debug("Loading source " + StringUtils.arrayToCommaDelimitedString(sources));
	}
	// 创建BeanDefinitionLoader。详看1
	BeanDefinitionLoader loader = createBeanDefinitionLoader(getBeanDefinitionRegistry(context), sources);
	if (this.beanNameGenerator != null) {
		loader.setBeanNameGenerator(this.beanNameGenerator);
	}
	if (this.resourceLoader != null) {
		loader.setResourceLoader(this.resourceLoader);
	}
	if (this.environment != null) {
		loader.setEnvironment(this.environment);
	}
	// 加载。详看2
	loader.load();
}

详看1——创建Bean定义加载器

BeanDefinitionLoader是从基础源加载bean定义,包括XML和JavaConfig。作为AnnotatedBeanDefinitionReader、XmlBeanDefinitionReader和ClassPathBeanDefinitionScanner的简单门面。
这个对象的创建时包含了:

BeanDefinitionLoader(BeanDefinitionRegistry registry, Object... sources) {
	Assert.notNull(registry, "Registry must not be null");
	Assert.notEmpty(sources, "Sources must not be empty");
	this.sources = sources;
	// 注解阅读器
	// 若没有internalConfigurationAnnotationProcessor注册进去
	// 若没有internalAutowiredAnnotationProcessor注册进去
	// 若没有internalCommonAnnotationProcessor注册进去
	// 若没有internalEventListenerProcessor注册进去
	// 若没有internalEventListenerFactory注册进去
	this.annotatedReader = new AnnotatedBeanDefinitionReader(registry);
	// XML阅读器
	this.xmlReader = (XML_ENABLED ? new XmlBeanDefinitionReader(registry) : null);
	// groovy阅读器
	this.groovyReader = (isGroovyPresent() ? new GroovyBeanDefinitionReader(registry) : null);
	// 类路径Bean定义扫描仪
	this.scanner = new ClassPathBeanDefinitionScanner(registry);
	// 类路径扫描仪排除一些路径
	this.scanner.addExcludeFilter(new ClassExcludeFilter(sources));
}

XML阅读器、groovy阅读器

其中XML阅读器、groovy阅读器继承AbstractBeanDefinitionReader 实现BeanDefinitionReader和EnvironmentCapable接口
在这里插入图片描述
看下功能约束
在这里插入图片描述
另一个接口就一个功能,获取环境对象
在这里插入图片描述

类路径Bean定义扫描仪

public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
			Environment environment, @Nullable ResourceLoader resourceLoader) {

	Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
	this.registry = registry;
	
	if (useDefaultFilters) {
		// 为 @ Component注册默认过滤器
		registerDefaultFilters();
	}
	// 设置环境
	setEnvironment(environment);
	// 设置资源
	setResourceLoader(resourceLoader);
}

详看2——加载

void load() {
	// 循环加载
	for (Object source : this.sources) {
		load(source);
	}
}

把资源分类

	private void load(Object source) {
		Assert.notNull(source, "Source must not be null");
		if (source instanceof Class<?>) {
			load((Class<?>) source);
			return;
		}
		if (source instanceof Resource) {
			load((Resource) source);
			return;
		}
		if (source instanceof Package) {
			load((Package) source);
			return;
		}
		if (source instanceof CharSequence) {
			load((CharSequence) source);
			return;
		}
		throw new IllegalArgumentException("Invalid source type " + source.getClass());
	}

Class类型的加载

private void load(Class<?> source) {
	if (isGroovyPresent() && GroovyBeanDefinitionSource.class.isAssignableFrom(source)) {
		// Any GroovyLoaders added in beans{} DSL can contribute beans here
		GroovyBeanDefinitionSource loader = BeanUtils.instantiateClass(source, GroovyBeanDefinitionSource.class);
		((GroovyBeanDefinitionReader) this.groovyReader).beans(loader.getBeans());
	}
	// 检查Bean是否符合注册条件
	if (isEligible(source)) {
		// 注册
		this.annotatedReader.register(source);
	}
}

来到了AnnotatedBeanDefinitionReader类进行注册

public void register(Class<?>... componentClasses) {
	for (Class<?> componentClass : componentClasses) {
		registerBean(componentClass);
	}
}

public void registerBean(Class<?> beanClass) {
	doRegisterBean(beanClass, null, null, null, null);
}

开始做注册Bean动作:把给的bean类注册为一个bean,得到它注解的元数据

private <T> void doRegisterBean(Class<T> beanClass, @Nullable String name,
		@Nullable Class<? extends Annotation>[] qualifiers, @Nullable Supplier<T> supplier,
		@Nullable BeanDefinitionCustomizer[] customizers) {
	// 构建一个带注释的通用Bean定义
	AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(beanClass);
	// 根据 @ Conditional注释判断是否跳过项目
	if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
		return;
	}

	abd.setInstanceSupplier(supplier);
	// 解析对应于提供的bean定义的ScopeMetadata,作用范围
	ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
	// 设置作用范围名称
	abd.setScope(scopeMetadata.getScopeName());
	// 为给定的bean定义生成一个bean名称
	String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));

	// 处理通用定义注释:Lazy、Primary、DependsOn、Role、Description
	AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
	if (qualifiers != null) {
		// 除bean类级别的限定符外,还需要考虑特定的限定符注释 (如果有)
		for (Class<? extends Annotation> qualifier : qualifiers) {
			if (Primary.class == qualifier) {
				abd.setPrimary(true);
			}
			else if (Lazy.class == qualifier) {
				abd.setLazyInit(true);
			}
			else {
				abd.addQualifier(new AutowireCandidateQualifier(qualifier));
			}
		}
	}
	if (customizers != null) {
		// 用于自定义工厂的BeanDefinition的一个或多个回调,例如设置lazy-init或主标志
		for (BeanDefinitionCustomizer customizer : customizers) {
			customizer.customize(abd);
		}
	}

	BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
	// 给应用范围设置代理模式
	definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
	// 注册Bean定义
	BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

园长的牧歌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值