preface

After finish reading registerBeanPostProcessors source code, the next step is to enter initMessageSource, main effect of this step is to initialize the internationalization files.

As before, you will learn the use of internationalization through the official website and then explore the source code.

MessageSource internationalization

As shown in 1.15.1. Internationalization using MessageSource, the main function of Internationalization is to customize different messages.

Note that the Bean name defined by MessageSource must be MessageSource, and if it is not found then DelegatingMessageSource is registered as the Bean of the MessageSource by default.

use

1. Create an internationalization file

2. Statement MessageSource

Declare MessageSource in JavaConfig. Be sure to call it MessageSource!

@Configuration
@ComponentScan("com.liuzhihang")
public class JavaConfig {

	@Bean(name = "messageSource")
	public MessageSource getMessageSource(a) {

		ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();

		messageSource.setDefaultEncoding("UTF-8");
		messageSource.addBasenames("message"."message_en");

		returnmessageSource; }}Copy the code

3. Test results

public class AnnotationConfigApplicationTest {

	public static void main(String[] args) {

		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

		context.register(JavaConfig.class);

		context.refresh();

		MessageSource messageSource = context.getBean(MessageSource.class);

		String zhMessage = messageSource.getMessage("user.name".null.null, Locale.CHINA);
		String enMessage = messageSource.getMessage("user.name".null.null, Locale.ENGLISH);

		System.out.println("zhMessage = " + zhMessage);

		System.out.println("enMessage = "+ enMessage); }}Copy the code

As shown above, the output is as follows:

After knowing how internationalization is used, think again about the source code of this step, and you will know what it is!

InitMessageSource source

protected void initMessageSource(a) {
    ConfigurableListableBeanFactory beanFactory = getBeanFactory();

    // The Bean name must be messageSource
    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.isTraceEnabled()) {
            logger.trace("Using MessageSource [" + this.messageSource + "]"); }}else {
        // Use empty MessageSource to be able to accept getMessage calls.
        // Otherwise, use the default
        DelegatingMessageSource dms = new DelegatingMessageSource();
        dms.setParentMessageSource(getInternalParentMessageSource());
        this.messageSource = dms;
        beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
        if (logger.isTraceEnabled()) {
            logger.trace("No '" + MESSAGE_SOURCE_BEAN_NAME + "' bean, using [" + this.messageSource + "]"); }}}Copy the code

The only interesting aspect of this source code is that the Bean name must be messageSource.

conclusion

This article through the official website, understand what is internationalization, and the use of internationalization, and combined with the code and source code, know why.

Of course, the important thing to note in this article is that the Bean name that internationalizes the MessageSource must be MessageSource.

Related to recommend

  • The Spring source learning 12: registerBeanPostProcessors
  • Spring source study 11: invokeBeanFactoryPostProcessors
  • PrepareBeanFactory and postProcessBeanFactory