Save This Page
Home » spring-framework-2.5.5-with-dependencies » org.springframework » context » support » [javadoc | source]
org.springframework.context.support
abstract public class: AbstractMessageSource [javadoc | source]
java.lang.Object
   org.springframework.context.support.MessageSourceSupport
      org.springframework.context.support.AbstractMessageSource

All Implemented Interfaces:
    HierarchicalMessageSource

Direct Known Subclasses:
    StaticMessageSource, ReloadableResourceBundleMessageSource, ResourceBundleMessageSource

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 method. For efficient resolution of messages without arguments, the #resolveCodeWithoutArguments 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.

Fields inherited from org.springframework.context.support.MessageSourceSupport:
logger
Method from org.springframework.context.support.AbstractMessageSource Summary:
getDefaultMessage,   getMessage,   getMessage,   getMessage,   getMessageFromParent,   getMessageInternal,   getParentMessageSource,   isUseCodeAsDefaultMessage,   renderDefaultMessage,   resolveArguments,   resolveCode,   resolveCodeWithoutArguments,   setParentMessageSource,   setUseCodeAsDefaultMessage
Methods from org.springframework.context.support.MessageSourceSupport:
createMessageFormat,   formatMessage,   isAlwaysUseMessageFormat,   renderDefaultMessage,   resolveArguments,   setAlwaysUseMessageFormat
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.springframework.context.support.AbstractMessageSource Detail:
 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.

 public final String getMessage(MessageSourceResolvable resolvable,
    Locale locale) throws NoSuchMessageException 
 public final String getMessage(String code,
    Object[] args,
    Locale locale) throws NoSuchMessageException 
 public final String getMessage(String code,
    Object[] args,
    String defaultMessage,
    Locale locale) 
 protected String getMessageFromParent(String code,
    Object[] args,
    Locale locale) 
    Try to retrieve the given message from the parent MessageSource, if any.
 protected String getMessageInternal(String code,
    Object[] args,
    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.
 public MessageSource getParentMessageSource() 
 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 method to return a custom fallback message for an unresolvable code.

 protected String renderDefaultMessage(String defaultMessage,
    Object[] args,
    Locale locale) 
    Render the given default message String. The default message is passed in as specified by the caller and can be rendered into a fully formatted default message shown to the user.

    The default implementation passes the String to formatMessage, resolving any argument placeholders found in them. Subclasses may override this method to plug in custom processing of default messages.

 protected Object[] resolveArguments(Object[] args,
    Locale locale) 
    Searches through the given array of objects, find any MessageSourceResolvable objects and resolve them.

    Allows for messages to have MessageSourceResolvables as arguments.

 abstract protected 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 resolveCodeWithoutArguments javadoc for details.

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

    The default implementation does use MessageFormat, through delegating to the resolveCode 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's advisable to circumvent MessageFormat completely for messages without arguments.

 public  void setParentMessageSource(MessageSource parent) 
 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 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.