spring源码解析之ioc-prepareBeanFactory

上一篇,我们分析了beanDefinition的定义,接着,我们继续跟着源码,来看看spring ioc在启动阶段都做了什么事

还是来贴出核心代码

public void refresh() throws BeansException, IllegalStateException {
        synchronized(this.startupShutdownMonitor) {
            this.prepareRefresh();
            ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
            this.prepareBeanFactory(beanFactory);

            try {
                this.postProcessBeanFactory(beanFactory);
                this.invokeBeanFactoryPostProcessors(beanFactory);
                this.registerBeanPostProcessors(beanFactory);
                this.initMessageSource();
                this.initApplicationEventMulticaster();
                this.onRefresh();
                this.registerListeners();
                this.finishBeanFactoryInitialization(beanFactory);
                this.finishRefresh();
            } catch (BeansException var9) {
                if (this.logger.isWarnEnabled()) {
                    this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9);
                }

                this.destroyBeans();
                this.cancelRefresh(var9);
                throw var9;
            } finally {
                this.resetCommonCaches();
            }

        }
    }

在拿到了beanFactory之后,接着源码到了这一块 这也是本篇核心分析的内容

在beanFactory正式工作之前的准备

//往下看
this.prepareBeanFactory(beanFactory);

准备工作

protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
		//设置classLoader
        beanFactory.setBeanClassLoader(this.getClassLoader());
        //设置bean表达式的解析器
        beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
        //设置属性文件的注册器(可能理解不对~~~)
        beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, this.getEnvironment()));
        //注册了第一个beanPostProcessor:ApplicationContextAwareProcessor  是new出来的
        beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
        //设置忽略的一些接口
        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
        beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
        beanFactory.registerResolvableDependency(ResourceLoader.class, this);
        beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
        beanFactory.registerResolvableDependency(ApplicationContext.class, this);
        //注册第二个beanPostProcessor:ApplicationListenerDetector  也是new出来的
        beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
        //loadTimeWarer的注册  我们没这个bean  忽略  感兴趣可以分析这个bean的作用
        if (beanFactory.containsBean("loadTimeWeaver")) {
            beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
            beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
        }

		//设置环境相关的bean   我们这里是默认的StandardEnvironment
        if (!beanFactory.containsLocalBean("environment")) {
            beanFactory.registerSingleton("environment", this.getEnvironment());
        }
		//设置系统参数相关的bean  默认的Properties
        if (!beanFactory.containsLocalBean("systemProperties")) {
            beanFactory.registerSingleton("systemProperties", this.getEnvironment().getSystemProperties());
        }
		//设置系统唤醒相关bean 还是默认的Collections$UnmodifiabneMap
        if (!beanFactory.containsLocalBean("systemEnvironment")) {
            beanFactory.registerSingleton("systemEnvironment", this.getEnvironment().getSystemEnvironment());
        }
    }

至此,准备工作做完,我们需要知道的,这里注册了两个beanPostProcessor

beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

你可能感兴趣的:(spring源码解析之ioc-prepareBeanFactory)