Spring ApplicationContext 容器实例化源码笔记之refresh03

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring ApplicationContext 容器实例化源码笔记之refresh03相关的知识,希望对你有一定的参考价值。

前面两篇文章写到了refresh方法的

ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

根据配置文件将配置的类加载到内存中(bean定义)并返回了默认的beanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory)实例

接下来是

    // Prepare the bean factory for use in this context.
    prepareBeanFactory(beanFactory);

这里主要是设置了spring在运行时需要使用的一些属性

具体代码

protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
        // Tell the internal bean factory to use the context‘s class loader etc.
        beanFactory.setBeanClassLoader(getClassLoader());
        beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver());
        beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this));

        // Configure the bean factory with context callbacks.
        beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
        beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
        beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
        beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
        beanFactory.ignoreDependencyInterface(ApplicationContextAware.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.
        if (!beanFactory.containsBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
            Map systemProperties;
            try {
                systemProperties = System.getProperties();
            }
            catch (AccessControlException ex) {
                systemProperties = new ReadOnlySystemAttributesMap() {
                    @Override
                    protected String getSystemAttribute(String propertyName) {
                        try {
                            return System.getProperty(propertyName);
                        }
                        catch (AccessControlException ex) {
                            if (logger.isInfoEnabled()) {
                                logger.info("Not allowed to obtain system property [" + propertyName + "]: " +
                                        ex.getMessage());
                            }
                            return null;
                        }
                    }
                };
            }
            beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, systemProperties);
        }

        if (!beanFactory.containsBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
            Map<String,String> systemEnvironment;
            try {
                systemEnvironment = System.getenv();
            }
            catch (AccessControlException ex) {
                systemEnvironment = new ReadOnlySystemAttributesMap() {
                    @Override
                    protected String getSystemAttribute(String variableName) {
                        try {
                            return System.getenv(variableName);
                        }
                        catch (AccessControlException ex) {
                            if (logger.isInfoEnabled()) {
                                logger.info("Not allowed to obtain system environment variable [" + variableName + "]: " +
                                        ex.getMessage());
                            }
                            return null;
                        }
                    }
                };
            }
            beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, systemEnvironment);
        }
    }

 例如:StandardBeanExpressionResolver

spring3增加了表达式语言的支持,默认可以使用#{bean.xxx}的形式来调用相关属性值。

以上是关于Spring ApplicationContext 容器实例化源码笔记之refresh03的主要内容,如果未能解决你的问题,请参考以下文章

Spring -- Spring相关API (ApplicationContext getBean)

spring BeanFactory 与ApplicationContext

BeanFactory and ApplicationContext in Spring

Spring获取ApplicationContext

Spring 学习笔记——IoC容器(ApplicationContext)

Spring ApplicationContext下的refresh()方法