“This is the 11th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

1. Introduction

This article focuses on internationalization initialization during the Spring IOC container initialization process (initMessageSource() method in refresh()) Initialization and events of multicast (refresh () in the initApplicationEventMulticaster () method), if you want to see before to view the content of the Spring IOC container initialization principle analysis (section 3)

2. Refresh () the source code

If you need to copy code, head to the Spring IOC container initialization principles analysis (Section 1)

InitMessageSource (

3.1 MessageSource interface

  1. In order to make it easier to understand, we will first talk about the MessageSource interface. The source code is as follows:

We can see that there are three refactoring methods with the same name in MessageSource.

  • The first method: parse the corresponding code and return it. If the corresponding code cannot be parsed, return the defaultMessage defaultMessage.

  • The second method: If the corresponding code cannot be parsed, an exception will be thrown. The NoSuchMessageException parameter contains one less String defaultMessage than the above parameter. NoSuchMessageException is thrown without returning the default value

  • The third method: parse corresponding information through the transmitted MessageSourceResolvable correspondence. MessageSourceResolvable is an interface, and we can customize the rules for parsing MessageSource.

  • Parameter Description:

    1. @param Code The code that needs to be resolved, corresponding to an attribute name in the resource file that encourages users to base message names on qualified class or package names to avoid potential conflicts and ensure maximum clarity.
    2. @param args An array of parameters used to populate the parameters in the message (the parameters in the message look like “{0}”, “{1,date}”, “{2,time}”)
    3. @param defaultMessage Specifies the default value to be returned if the corresponding code does not exist
    4. @param locale Specifies the corresponding locale
    5. Param MessageSourceResolvable MessageSource parsing class
  1. Check out the source code for initMessageSource()

Since the code here is quite long, I will comment directly on the source code for convenience.

Protected void initMessageSource () {/ / get the current the beanFactory ConfigurableListableBeanFactory the beanFactory = getBeanFactory ();  / / whether the current the beanFactory contains famous for messageSource bean if (the beanFactory. ContainsLocalBean (MESSAGE_SOURCE_BEAN_NAME)) {/ / MessageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.  // Make MessageSource aware of parent MessageSource. And when this MessageSource object is HierarchicalMessageSource type if (this. The parent! = null && enclosing messageSource instanceof HierarchicalMessageSource) {/ / to make it strong to HierarchicalMessageSource type HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource; // If the parent class MessageSource is empty, Set the superclass MessageSource to internal of the parent MessageSource if (HMS) getParentMessageSource () = = null) {/ / Only set the 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 DelegatingMessageSource bean DelegatingMessageSource dms = new DelegatingMessageSource(); / / set the parent class MessageSource DMS. SetParentMessageSource (getInternalParentMessageSource ()); this.messageSource = dms; / / try to register the bean into the beanFactory the beanFactory. RegisterSingleton (MESSAGE_SOURCE_BEAN_NAME, enclosing messageSource); if (logger.isTraceEnabled()) { logger.trace("No '" + MESSAGE_SOURCE_BEAN_NAME + "' bean, using [" + this.messageSource + "]"); }}}Copy the code

DelegatingMessageSource: The message source resolves the delegate class (when not specified by the user, the current class is used by default by the initial U of the Spring IOC container). The function is relatively simple: format the string and parameter array into a message string

This part of the code is also relatively simple, which can be summarized as follows: Read the user-configured MessageSource first, if Spring does not create a DelegatingMessageSource by default

4. initApplicationEventMulticaster()

This method is that judge whether you custom ApplicationEventMulticaster, first register a without words. SimpleApplicationEventMulticaster.

protected void initApplicationEventMulticaster() { ConfigurableListableBeanFactory beanFactory = getBeanFactory(); / / judgment have container applicationEventMulticaster the bean the if (the beanFactory containsLocalBean (APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {/ / a Assigned to it own applicationEventMulticaster this.applicationEventMulticaster = beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class); if (logger.isTraceEnabled()) { logger.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "] "); }} else {/ / no Create a new SimpleApplicationEventMulticaster object, Registered to the beanFactory enclosing applicationEventMulticaster = new SimpleApplicationEventMulticaster (the beanFactory); beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster); if (logger.isTraceEnabled()) { logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " + "[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]"); }}}Copy the code

Note: SimpleApplicationEventMulticaster is to match with the back of the ApplicationListener is used, the in the subsequent ApplicationListener this time I’m fine, Together, they belong to the observer mode of the design pattern (PS: I’ll talk more about this later) and are used to listen for events in the callback container.

The spring IOC container will create a default for you if you don’t have one. Now many people may feel that spring configuration complex, SpringBoot is very humane, in fact, we learn spring in-depth will find that Spring Boot is in fact on the basis of spring to us another layer of encapsulation, so that we write a lot of configuration files.