回首Spring源码之IOC:prepareBeanFactory

15 篇文章 3 订阅

前言

        上篇讲到了基于注解的容器AnnotationConfigApplicationContext的创建过程,紧接着就是容器的加载refresh,这是spring ioc的主流程,里面包含了多个阶段的逻辑,主要分析一下刷新容器的几个主要方法,本篇着重分析容器刷新前的准备工作prepareBeanFactory。

prepareBeanFactory

首先进入AbstractApplicationContext#refresh:

public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			//准备刷新,这个方法很简单,主要是将早期的监听器加入容器
            //对于Spring是没有早期的监听器的,在springboot中做了扩张,会有很多早期的监听器
			prepareRefresh();

			//得到bean工厂,比较简单的方法
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			//准备bean工厂
			prepareBeanFactory(beanFactory);

			//..待续
		}
}

着重看下prepareBeanFactory这个方法:

protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
		beanFactory.setBeanClassLoader(getClassLoader());
        //为容器设置SPEL表达式解析器,像@value注解经常会用到spel表达式
		beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
        //为容器设置属性编辑注册器,稍微讲一下
        //这个注册器注册了很多属性编辑器PropertyEditor,用来为bean的成员属性做类型转换
        //这在配置文件时代用的较多,因为在配置文件中设置属性都是字符串
        //spring需要将这些字符串转换成相应的类型,现在应该是不太常用了
		beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

		//添加ApplicationContextAwareProcessor,这是一个bean的后置处理器,在bean的初始化前置调用中会用到
        //用来为bean设置一些context相关的属性,如applicationContext,Environment等
		beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));

        //设置需要忽略的类,啥意思?
        //设置之后,在bean的属性注入阶段容器将不会为是以下类的成员属性赋值
        //容器会在其它地方为这些类型的成员属性赋值
		beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
		beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
		beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
		beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
		beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
		beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

		//注册可解析的依赖,啥意思?
		//容器在为bean设置以下类型的成员属性时,设置的就是第二个参数
        //因为这些类型的bean在容器中很可能有多个
		beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
		beanFactory.registerResolvableDependency(ResourceLoader.class, this);
		beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
		beanFactory.registerResolvableDependency(ApplicationContext.class, this);

		//添加ApplicationContextAwareProcessor,这是一个bean的后置处理器,在bean的初始化后置调用中会用到
        //主要用来把用户自定义的监听器也就是实现了ApplicationListener接口的bean添加到容器中
		beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

		//如果容器中有loadTimeWeaver,那么会进入,主要用在aop的静态织入
        //不常用,反正笔者没用过
		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()));
		}

		//下面3个if,为容器注册了3个环境相关的bean,这些bean,在创建context的时候就已经创建了
		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());
		}
}

总结

        到此prepareBeanFactory流程结束了,下一篇主要讲bean工厂的后置处理器BeanFactoryPostProcessor       

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值