Spring ApplicationContext 容器執行個體化源碼筆記之refresh03

來源:互聯網
上載者:User

標籤:筆記   ref   end   plain   xxx   pre   div   方法   類載入   

前面兩篇文章寫到了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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.