org.springframework.context.support

Class MessageSourceAccessor

  • java.lang.Object
    • org.springframework.context.support.MessageSourceAccessor


  • public class MessageSourceAccessor
    extends java.lang.Object
    Helper class for easy access to messages from a MessageSource, providing various overloaded getMessage methods.

    Available from ApplicationObjectSupport, but also reusable as a standalone helper to delegate to in application objects.

    Since:
    23.10.2003
    Author:
    Juergen Hoeller
    See Also:
    ApplicationObjectSupport.getMessageSourceAccessor()
    • Constructor Summary

      Constructors 
      Constructor and Description
      MessageSourceAccessor(MessageSource messageSource)
      Create a new MessageSourceAccessor, using LocaleContextHolder's locale as default locale.
      MessageSourceAccessor(MessageSource messageSource, java.util.Locale defaultLocale)
      Create a new MessageSourceAccessor, using the given default locale.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      protected java.util.Locale getDefaultLocale()
      Return the default locale to use if no explicit locale has been given.
      java.lang.String getMessage(MessageSourceResolvable resolvable)
      Retrieve the given MessageSourceResolvable (e.g.
      java.lang.String getMessage(MessageSourceResolvable resolvable, java.util.Locale locale)
      Retrieve the given MessageSourceResolvable (e.g.
      java.lang.String getMessage(java.lang.String code)
      Retrieve the message for the given code and the default Locale.
      java.lang.String getMessage(java.lang.String code, java.util.Locale locale)
      Retrieve the message for the given code and the given Locale.
      java.lang.String getMessage(java.lang.String code, java.lang.Object[] args)
      Retrieve the message for the given code and the default Locale.
      java.lang.String getMessage(java.lang.String code, java.lang.Object[] args, java.util.Locale locale)
      Retrieve the message for the given code and the given Locale.
      java.lang.String getMessage(java.lang.String code, java.lang.Object[] args, java.lang.String defaultMessage)
      Retrieve the message for the given code and the default Locale.
      java.lang.String getMessage(java.lang.String code, java.lang.Object[] args, java.lang.String defaultMessage, java.util.Locale locale)
      Retrieve the message for the given code and the given Locale.
      java.lang.String getMessage(java.lang.String code, java.lang.String defaultMessage)
      Retrieve the message for the given code and the default Locale.
      java.lang.String getMessage(java.lang.String code, java.lang.String defaultMessage, java.util.Locale locale)
      Retrieve the message for the given code and the given Locale.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MessageSourceAccessor

        public MessageSourceAccessor(MessageSource messageSource)
        Create a new MessageSourceAccessor, using LocaleContextHolder's locale as default locale.
        Parameters:
        messageSource - the MessageSource to wrap
        See Also:
        LocaleContextHolder.getLocale()
      • MessageSourceAccessor

        public MessageSourceAccessor(MessageSource messageSource,
                                     java.util.Locale defaultLocale)
        Create a new MessageSourceAccessor, using the given default locale.
        Parameters:
        messageSource - the MessageSource to wrap
        defaultLocale - the default locale to use for message access
    • Method Detail

      • getMessage

        public java.lang.String getMessage(java.lang.String code,
                                           java.lang.String defaultMessage)
        Retrieve the message for the given code and the default Locale.
        Parameters:
        code - code of the message
        defaultMessage - the String to return if the lookup fails
        Returns:
        the message
      • getMessage

        public java.lang.String getMessage(java.lang.String code,
                                           java.lang.String defaultMessage,
                                           java.util.Locale locale)
        Retrieve the message for the given code and the given Locale.
        Parameters:
        code - code of the message
        defaultMessage - the String to return if the lookup fails
        locale - the Locale in which to do lookup
        Returns:
        the message
      • getMessage

        public java.lang.String getMessage(java.lang.String code,
                                           @Nullable
                                           java.lang.Object[] args,
                                           java.lang.String defaultMessage)
        Retrieve the message for the given code and the default Locale.
        Parameters:
        code - code of the message
        args - arguments for the message, or null if none
        defaultMessage - the String to return if the lookup fails
        Returns:
        the message
      • getMessage

        public java.lang.String getMessage(java.lang.String code,
                                           @Nullable
                                           java.lang.Object[] args,
                                           java.lang.String defaultMessage,
                                           java.util.Locale locale)
        Retrieve the message for the given code and the given Locale.
        Parameters:
        code - code of the message
        args - arguments for the message, or null if none
        defaultMessage - the String to return if the lookup fails
        locale - the Locale in which to do lookup
        Returns:
        the message
      • getMessage

        public java.lang.String getMessage(java.lang.String code)
                                    throws NoSuchMessageException
        Retrieve the message for the given code and the default Locale.
        Parameters:
        code - code of the message
        Returns:
        the message
        Throws:
        NoSuchMessageException - if not found
      • getMessage

        public java.lang.String getMessage(java.lang.String code,
                                           java.util.Locale locale)
                                    throws NoSuchMessageException
        Retrieve the message for the given code and the given Locale.
        Parameters:
        code - code of the message
        locale - the Locale in which to do lookup
        Returns:
        the message
        Throws:
        NoSuchMessageException - if not found
      • getMessage

        public java.lang.String getMessage(java.lang.String code,
                                           @Nullable
                                           java.lang.Object[] args)
                                    throws NoSuchMessageException
        Retrieve the message for the given code and the default Locale.
        Parameters:
        code - code of the message
        args - arguments for the message, or null if none
        Returns:
        the message
        Throws:
        NoSuchMessageException - if not found
      • getMessage

        public java.lang.String getMessage(java.lang.String code,
                                           @Nullable
                                           java.lang.Object[] args,
                                           java.util.Locale locale)
                                    throws NoSuchMessageException
        Retrieve the message for the given code and the given Locale.
        Parameters:
        code - code of the message
        args - arguments for the message, or null if none
        locale - the Locale in which to do lookup
        Returns:
        the message
        Throws:
        NoSuchMessageException - if not found
      • getMessage

        public java.lang.String getMessage(MessageSourceResolvable resolvable,
                                           java.util.Locale locale)
                                    throws NoSuchMessageException
        Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance) in the given Locale.
        Parameters:
        resolvable - the MessageSourceResolvable
        locale - the Locale in which to do lookup
        Returns:
        the message
        Throws:
        NoSuchMessageException - if not found