Springコンテナ作成プロセス(6)国際化サポート

15680 ワード

InitMessageSource初期化MessageSource,Spring国際化サポート(前の実行手順はシリーズ記事で参照)
コンテナrefreshの概要:
		synchronized (this.startupShutdownMonitor) {
			//  , 。
			prepareRefresh();
			
			//  BeanFactory(DefaultListableBeanFactor), bean 。
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			//  BeanFactory 	
			prepareBeanFactory(beanFactory);

			try {
				//  , beanFactory 
				postProcessBeanFactory(beanFactory);

				//  beanFactory 
				invokeBeanFactoryPostProcessors(beanFactory);

				//  bean 
				registerBeanPostProcessors(beanFactory);

				//  messageSource, 
				initMessageSource();

				// Initialize event multicaster for this context.
				initApplicationEventMulticaster();

				// Initialize other special beans in specific context subclasses.
				onRefresh();

				// Check for listener beans and register them.
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
				finishBeanFactoryInitialization(beanFactory);

				// Last step: publish corresponding event.
				finishRefresh();
			}

			catch (BeansException ex) {
				if (logger.isWarnEnabled()) {
					logger.warn("Exception encountered during context initialization - " +
							"cancelling refresh attempt: " + ex);
				}

				// Destroy already created singletons to avoid dangling resources.
				destroyBeans();

				// Reset 'active' flag.
				cancelRefresh(ex);

				// Propagate exception to caller.
				throw ex;
			}

			finally {
				// Reset common introspection caches in Spring's core, since we
				// might not ever need metadata for singleton beans anymore...
				resetCommonCaches();
			}
		}

	protected void initMessageSource() {
		Configurabl
		eListableBeanFactory beanFactory = getBeanFactory();
		//   messageSource messageSource bean 
		if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
			this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
			// Make MessageSource aware of parent MessageSource.
			if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
				HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;
				if (hms.getParentMessageSource() == null) {
					// Only set parent context as parent MessageSource if no parent MessageSource
					// registered already.
					hms.setParentMessageSource(getInternalParentMessageSource());
				}
			}
			if (logger.isDebugEnabled()) {
				logger.debug("Using MessageSource [" + this.messageSource + "]");
			}
		}
		else {
			//  
			DelegatingMessageSource dms = new DelegatingMessageSource();
			dms.setParentMessageSource(getInternalParentMessageSource());
			this.messageSource = dms;
			beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
			if (logger.isDebugEnabled()) {
				logger.debug("Unable to locate MessageSource with name '" + MESSAGE_SOURCE_BEAN_NAME +
						"': using default [" + this.messageSource + "]");
			}
		}
	}

MessageSourceを構成し、beanFactory直接getmessageが国際化をサポート
    <bean id="messageSource"
          class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="i18n"/>
    bean>
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext("classpath:application.xml");

//   i18n_en_US.properties  
String message = factory.getMessage("person.name", new Object[]{"world"}, Locale.US);