世事洞明皆学问,人情练达即文章。这篇文章主要讲述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}的形式来调用相关属性值。
推荐阅读
- AIDL安卓接口定义语言
- SpringBoot-URL路由(@Controller和@RequestMapping)
- Android studio安装与配置
- android黑科技系列——解析公众号文章消息和链接文章消息自动打开原理
- android黑科技系列——防自动抢红包外挂原理解析
- android黑科技系列——分析某直播App的协议加密原理以及调用加密方法进行协议参数构造
- android黑科技系列——静态分析技术来破解Apk
- android黑科技系列——获取加固后应用App的所有方法信息
- Java互操作性(从Java调用Kotlin代码)