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

世事洞明皆学问,人情练达即文章。这篇文章主要讲述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在运行时需要使用的一些属性
【Spring ApplicationContext 容器实例化源码笔记之refresh03】具体代码
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}的形式来调用相关属性值。

    推荐阅读