查看全部
// Instantiate all remaining (non-lazy-init) singletons. 初始化剩下的单实例bean finishBeanFactoryInitialization(beanFactory);
- 初始化容器中的类转换器,id 为
conversionService
// Initialize conversion service for this context. if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) && beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) { beanFactory.setConversionService( beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)); }
- 注册值解析器
// Register a default embedded value resolver if no bean post-processor // (such as a PropertyPlaceholderConfigurer bean) registered any before: // at this point, primarily for resolution in annotation attribute values. if (!beanFactory.hasEmbeddedValueResolver()) { beanFactory.addEmbeddedValueResolver(new StringValueResolver() { @Override public String resolveStringValue(String strVal) { return getEnvironment().resolvePlaceholders(strVal); } }); }
- AspectJ相关
// Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early. String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false); for (String weaverAwareName : weaverAwareNames) { getBean(weaverAwareName); }
- 对beanFactory做一些配置
// Stop using the temporary ClassLoader for type matching. // 停止使用临时的ClassLoader进行类型匹配。 beanFactory.setTempClassLoader(null); // Allow for caching all bean definition metadata, not expecting further changes. //允许缓存所有bean定义信息 beanFactory.freezeConfiguration();
- 初始化剩余的单实例bean
// Instantiate all remaining (non-lazy-init) singletons. beanFactory.preInstantiateSingletons();
- 获得所有的bean name
List<String> beanNames = new ArrayList<String>(this.beanDefinitionNames);
- 循环遍历beanNames
- 获取bean定义
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
- 初始化的bean必须满足的条件 - 非抽象类&&是单实例的&&非懒加载
- bean如果实现了FactoryBean,则使用FactoryBean的getBean创建bean;否则使用getBean创建bean
注:getBean创建bean的过程for (String beanName : beanNames) { RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName); if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) { if (isFactoryBean(beanName)) { final FactoryBean<?> factory = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName); boolean isEagerInit; if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) { isEagerInit = AccessController.doPrivileged(new PrivilegedAction<Boolean>() { @Override public Boolean run() { return ((SmartFactoryBean<?>) factory).isEagerInit(); } }, getAccessControlContext()); } else { isEagerInit = (factory instanceof SmartFactoryBean && ((SmartFactoryBean<?>) factory).isEagerInit()); } if (isEagerInit) { getBean(beanName); } } else { getBean(beanName); } } }
- 再次循环遍历,如果实现了SmartInitializingSingleton接口,则回调
afterSingletonsInstantiated
- 获得所有的bean name
网友评论