Class AbstractMessageSource

java.lang.Object
org.springframework.context.support.MessageSourceSupport
org.springframework.context.support.AbstractMessageSource
All Implemented Interfaces:
HierarchicalMessageSource, MessageSource
Direct Known Subclasses:
AbstractResourceBasedMessageSource, StaticMessageSource

public abstract class AbstractMessageSource extends MessageSourceSupport implements HierarchicalMessageSource
Abstract implementation of the HierarchicalMessageSource interface, implementing common handling of message variants, making it easy to implement a specific strategy for a concrete MessageSource.

Subclasses must implement the abstract resolveCode(java.lang.String, java.util.Locale) method. For efficient resolution of messages without arguments, the resolveCodeWithoutArguments(java.lang.String, java.util.Locale) method should be overridden as well, resolving messages without a MessageFormat being involved.

Note: By default, message texts are only parsed through MessageFormat if arguments have been passed in for the message. In case of no arguments, message texts will be returned as-is. As a consequence, you should only use MessageFormat escaping for messages with actual arguments, and keep all other messages unescaped. If you prefer to escape all messages, set the "alwaysUseMessageFormat" flag to "true".

Supports not only MessageSourceResolvables as primary messages but also resolution of message arguments that are in turn MessageSourceResolvables themselves.

This class does not implement caching of messages per code, thus subclasses can dynamically change messages over time. Subclasses are encouraged to cache their messages in a modification-aware fashion, allowing for hot deployment of updated messages.

Author:
Juergen Hoeller, Rod Johnson
See Also:
  • Constructor Details

    • AbstractMessageSource

      public AbstractMessageSource()
  • Method Details

    • setParentMessageSource

      public void setParentMessageSource(@Nullable MessageSource parent)
      Description copied from interface: HierarchicalMessageSource
      Set the parent that will be used to try to resolve messages that this object can't resolve.
      Specified by:
      setParentMessageSource in interface HierarchicalMessageSource
      Parameters:
      parent - the parent MessageSource that will be used to resolve messages that this object can't resolve. May be null, in which case no further resolution is possible.
    • getParentMessageSource

      @Nullable public MessageSource getParentMessageSource()
      Description copied from interface: HierarchicalMessageSource
      Return the parent of this MessageSource, or null if none.
      Specified by:
      getParentMessageSource in interface HierarchicalMessageSource
    • setCommonMessages

      public void setCommonMessages(@Nullable Properties commonMessages)
      Specify locale-independent common messages, with the message code as key and the full message String (may contain argument placeholders) as value.

      May also link to an externally defined Properties object, e.g. defined through a PropertiesFactoryBean.

    • getCommonMessages

      @Nullable protected Properties getCommonMessages()
      Return a Properties object defining locale-independent common messages, if any.
    • setUseCodeAsDefaultMessage

      public void setUseCodeAsDefaultMessage(boolean useCodeAsDefaultMessage)
      Set whether to use the message code as default message instead of throwing a NoSuchMessageException. Useful for development and debugging. Default is "false".

      Note: In case of a MessageSourceResolvable with multiple codes (like a FieldError) and a MessageSource that has a parent MessageSource, do not activate "useCodeAsDefaultMessage" in the parent: Else, you'll get the first code returned as message by the parent, without attempts to check further codes.

      To be able to work with "useCodeAsDefaultMessage" turned on in the parent, AbstractMessageSource and AbstractApplicationContext contain special checks to delegate to the internal getMessageInternal(java.lang.String, java.lang.Object[], java.util.Locale) method if available. In general, it is recommended to just use "useCodeAsDefaultMessage" during development and not rely on it in production in the first place, though.

      See Also:
    • isUseCodeAsDefaultMessage

      protected boolean isUseCodeAsDefaultMessage()
      Return whether to use the message code as default message instead of throwing a NoSuchMessageException. Useful for development and debugging. Default is "false".

      Alternatively, consider overriding the getDefaultMessage(org.springframework.context.MessageSourceResolvable, java.util.Locale) method to return a custom fallback message for an unresolvable code.

      See Also:
    • getMessage

      @Nullable public final String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale)
      Description copied from interface: MessageSource
      Try to resolve the message. Return default message if no message was found.
      Specified by:
      getMessage in interface MessageSource
      Parameters:
      code - the message code to look up, e.g. 'calculator.noRateSet'. MessageSource users are encouraged to base message names on qualified class or package names, avoiding potential conflicts and ensuring maximum clarity.
      args - an array of arguments that will be filled in for params within the message (params look like "{0}", "{1,date}", "{2,time}" within a message), or null if none
      defaultMessage - a default message to return if the lookup fails
      locale - the locale in which to do the lookup
      Returns:
      the resolved message if the lookup was successful, otherwise the default message passed as a parameter (which may be null)
      See Also:
    • getMessage

      public final String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException
      Description copied from interface: MessageSource
      Try to resolve the message. Treat as an error if the message can't be found.
      Specified by:
      getMessage in interface MessageSource
      Parameters:
      code - the message code to look up, e.g. 'calculator.noRateSet'. MessageSource users are encouraged to base message names on qualified class or package names, avoiding potential conflicts and ensuring maximum clarity.
      args - an array of arguments that will be filled in for params within the message (params look like "{0}", "{1,date}", "{2,time}" within a message), or null if none
      locale - the locale in which to do the lookup
      Returns:
      the resolved message (never null)
      Throws:
      NoSuchMessageException - if no corresponding message was found
      See Also:
    • getMessage

      public final String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException
      Description copied from interface: MessageSource
      Try to resolve the message using all the attributes contained within the MessageSourceResolvable argument that was passed in.

      NOTE: We must throw a NoSuchMessageException on this method since at the time of calling this method we aren't able to determine if the defaultMessage property of the resolvable is null or not.

      Specified by:
      getMessage in interface MessageSource
      Parameters:
      resolvable - the value object storing attributes required to resolve a message (may include a default message)
      locale - the locale in which to do the lookup
      Returns:
      the resolved message (never null since even a MessageSourceResolvable-provided default message needs to be non-null)
      Throws:
      NoSuchMessageException - if no corresponding message was found (and no default message was provided by the MessageSourceResolvable)
      See Also:
    • getMessageInternal

      @Nullable protected String getMessageInternal(@Nullable String code, @Nullable Object[] args, @Nullable Locale locale)
      Resolve the given code and arguments as message in the given Locale, returning null if not found. Does not fall back to the code as default message. Invoked by getMessage methods.
      Parameters:
      code - the code to lookup up, such as 'calculator.noRateSet'
      args - array of arguments that will be filled in for params within the message
      locale - the locale in which to do the lookup
      Returns:
      the resolved message, or null if not found
      See Also:
    • getMessageFromParent

      @Nullable protected String getMessageFromParent(String code, @Nullable Object[] args, Locale locale)
      Try to retrieve the given message from the parent MessageSource, if any.
      Parameters:
      code - the code to lookup up, such as 'calculator.noRateSet'
      args - array of arguments that will be filled in for params within the message
      locale - the locale in which to do the lookup
      Returns:
      the resolved message, or null if not found
      See Also:
    • getDefaultMessage

      @Nullable protected String getDefaultMessage(MessageSourceResolvable resolvable, Locale locale)
      Get a default message for the given MessageSourceResolvable.

      This implementation fully renders the default message if available, or just returns the plain default message String if the primary message code is being used as a default message.

      Parameters:
      resolvable - the value object to resolve a default message for
      locale - the current locale
      Returns:
      the default message, or null if none
      Since:
      4.3.6
      See Also:
    • getDefaultMessage

      @Nullable protected String getDefaultMessage(String code)
      Return a fallback default message for the given code, if any.

      Default is to return the code itself if "useCodeAsDefaultMessage" is activated, or return no fallback else. In case of no fallback, the caller will usually receive a NoSuchMessageException from getMessage.

      Parameters:
      code - the message code that we couldn't resolve and that we didn't receive an explicit default message for
      Returns:
      the default message to use, or null if none
      See Also:
    • resolveArguments

      protected Object[] resolveArguments(@Nullable Object[] args, Locale locale)
      Searches through the given array of objects, finds any MessageSourceResolvable objects and resolves them.

      Allows for messages to have MessageSourceResolvables as arguments.

      Overrides:
      resolveArguments in class MessageSourceSupport
      Parameters:
      args - array of arguments for a message
      locale - the locale to resolve through
      Returns:
      an array of arguments with any MessageSourceResolvables resolved
    • resolveCodeWithoutArguments

      @Nullable protected String resolveCodeWithoutArguments(String code, Locale locale)
      Subclasses can override this method to resolve a message without arguments in an optimized fashion, i.e. to resolve without involving a MessageFormat.

      The default implementation does use MessageFormat, through delegating to the resolveCode(java.lang.String, java.util.Locale) method. Subclasses are encouraged to replace this with optimized resolution.

      Unfortunately, java.text.MessageFormat is not implemented in an efficient fashion. In particular, it does not detect that a message pattern doesn't contain argument placeholders in the first place. Therefore, it is advisable to circumvent MessageFormat for messages without arguments.

      Parameters:
      code - the code of the message to resolve
      locale - the locale to resolve the code for (subclasses are encouraged to support internationalization)
      Returns:
      the message String, or null if not found
      See Also:
    • resolveCode

      @Nullable protected abstract MessageFormat resolveCode(String code, Locale locale)
      Subclasses must implement this method to resolve a message.

      Returns a MessageFormat instance rather than a message String, to allow for appropriate caching of MessageFormats in subclasses.

      Subclasses are encouraged to provide optimized resolution for messages without arguments, not involving MessageFormat. See the resolveCodeWithoutArguments(java.lang.String, java.util.Locale) javadoc for details.

      Parameters:
      code - the code of the message to resolve
      locale - the locale to resolve the code for (subclasses are encouraged to support internationalization)
      Returns:
      the MessageFormat for the message, or null if not found
      See Also: