为了账号安全,请及时绑定邮箱和手机立即绑定

Spring Bean生命周期-prepareBeanFactory(五)

标签:
Java Spring

在ApplicationContext刷新获取beanFactory之后,开始准备context使用的beanFactory。这一步相对比较简单,就是配置facotry的特性,比如:ClassLoader,post-processors等等

代码如下:

protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
		// Tell the internal bean factory to use the context's class loader etc.
		//让BeanFactory使用context的class loader
		beanFactory.setBeanClassLoader(getClassLoader());
		beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
		beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

		// Configure the bean factory with context callbacks.
		// 配置bean factory的context回调
		beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
		beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
		beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
		beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
		beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
		beanFactory.ignoreDependencyInterface(EnvironmentAware.class);

		// BeanFactory interface not registered as resolvable type in a plain factory.
		// MessageSource registered (and found for autowiring) as a bean.
		beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
		beanFactory.registerResolvableDependency(ResourceLoader.class, this);
		beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
		beanFactory.registerResolvableDependency(ApplicationContext.class, this);

		// Detect a LoadTimeWeaver and prepare for weaving, if found.
		if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
			beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
			// Set a temporary ClassLoader for type matching.
			beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
		}

		// Register default environment beans.
		// 注册默认的environment bean
		if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
			beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
		}
		if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
			beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
		}
		if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
			beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
		}
	}

分析

beanfactory的准备阶段,比较简单,主要明白准备阶段操作的几个方法。

beanFactory.addBeanPostProcessor

添加BeanPostProcessor,当bean被这个工厂创建的时候会用到PostProcessor, 在beanfactory中存储了beanPostProcessors的列表,在生效的时候,列表中的PostProcessor都会执行。

		beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
beanFactory.ignoreDependencyInterface(Class<?> ifc)

在注入的时候忽略此方法指定的接口类。也就是指定的接口不会被注入进去。

如下,不会生效

@Autowire 
ResourceLoaderAware aware;
		beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
		beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
		beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
		beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
		beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.registerResolvableDependency(Class<?> dependencyType, Object autowiredValue)

注册可以解析的依赖关系,当注入的类型为dependencyType的时候,注入autowiredValue。
注入类型与注入值的关系存储在map中。

resolvableDependencies = new ConcurrentHashMap<Class<?>, Object>(16);

// 注入类型为BeanFactory时,注入beanFactory(DefaultListableBeanFactory)
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
		beanFactory.registerResolvableDependency(ResourceLoader.class, this);

//注入类型为ApplicationEventPublisher时,注入值为this(ApplicationContext)
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);

//注入类型为ApplicationContext时,注入值也为this(ApplicationContext)
beanFactory.registerResolvableDependency(ApplicationContext.class, this);

额外

prepareBeanFactory主要做一些准备工作,没有什么逻辑,在Applicationcontext的refresh方法中往下看一步,执行

postProcessBeanFactory(beanFactory);

这一步Spring框架中没有实现,留待我们自己实现。

最后

这次看了refresh方法中的prepareBeanFactory内容比较简单,就不多说了。

点击查看更多内容
3人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
1.1万
获赞与收藏
1544

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消