11_残りの単一インスタンスbeanを初期化

3603 ワード

// 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 beanNames = new ArrayList(this.beanDefinitionNames);
    
  • を取得
  • サイクルbeanName
  • bean定義
  • を取得する
    RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
    
  • 初期化beanが満たさなければならない条件-非抽象クラス&&は単一インスタンスの&&非怠惰ロード
  • bean FactoryBeanが実装された場合、FactoryBeanの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() {
                        @Override
                        public Boolean run() {
                            return ((SmartFactoryBean>) factory).isEagerInit();
                        }
                    }, getAccessControlContext());
                }
                else {
                    isEagerInit = (factory instanceof SmartFactoryBean &&
                            ((SmartFactoryBean>) factory).isEagerInit());
                }
                if (isEagerInit) {
                    getBean(beanName);
                }
            }
            else {
                getBean(beanName);
            }
        }
    }
    
    注:getBeanがbeanを作成するプロセス
  • は再びループし、SmartInitializingSingletonインタフェースが実装されると、afterSingletonsInstantiated
  • にコールバックする.