1. 程式人生 > >Spring ApplicationContext 容器實例化源碼筆記之refresh03

Spring ApplicationContext 容器實例化源碼筆記之refresh03

end over lis 語言 factory rar messages intern attribute

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