public class

StaticMessageSource

extends AbstractMessageSource
java.lang.Object
   ↳ org.springframework.context.support.MessageSourceSupport
     ↳ org.springframework.context.support.AbstractMessageSource
       ↳ org.springframework.context.support.StaticMessageSource

Class Overview

Simple implementation of MessageSource which allows messages to be registered programmatically. This MessageSource supports basic internationalization.

Intended for testing rather than for use in production systems.

Summary

[Expand]
Inherited Fields
From class org.springframework.context.support.MessageSourceSupport
Public Constructors
StaticMessageSource()
Public Methods
void addMessage(String code, Locale locale, String msg)
Associate the given message with the given code.
void addMessages(Map<StringString> messages, Locale locale)
Associate the given message values with the given keys as codes.
String toString()
Protected Methods
MessageFormat resolveCode(String code, Locale locale)
Subclasses must implement this method to resolve a message.
String resolveCodeWithoutArguments(String code, Locale locale)
Subclasses can override this method to resolve a message without arguments in an optimized fashion, i.e.
[Expand]
Inherited Methods
From class org.springframework.context.support.AbstractMessageSource
From class org.springframework.context.support.MessageSourceSupport
From class java.lang.Object
From interface org.springframework.context.HierarchicalMessageSource
From interface org.springframework.context.MessageSource

Public Constructors

public StaticMessageSource ()

Public Methods

public void addMessage (String code, Locale locale, String msg)

Associate the given message with the given code.

Parameters
code the lookup code
locale the locale that the message should be found within
msg the message associated with this lookup code

public void addMessages (Map<StringString> messages, Locale locale)

Associate the given message values with the given keys as codes.

Parameters
messages the messages to register, with messages codes as keys and message texts as values
locale the locale that the messages should be found within

public String toString ()

Protected Methods

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 the resolveCodeWithoutArguments(String, 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

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(String, 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