Top Description Inners Fields Constructors Methods
org.springframework.context.support

public Class StaticMessageSource

extends AbstractMessageSource
Class Inheritance
Imports
java.text.MessageFormat, java.util.HashMap, .Locale, .Map, org.springframework.lang.Nullable, org.springframework.util.Assert

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

Intended for testing rather than for use in production systems.

Authors
Rod Johnson, Juergen Hoeller

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private final Map<String, Map<Locale, StaticMessageSource.MessageHolder>>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
addMessage(String
the lookup code
code
,
Locale
the locale that the message should be found within
locale
,
String
the message associated with this lookup code
msg
)

Associate the given message with the given code.

public void
addMessages(Map<String, String>
the messages to register, with messages codes as keys and message texts as values
messages
,
Locale
the locale that the messages should be found within
locale
)

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

protected MessageFormat
resolveCode(String
the code of the message to resolve
code
,
Locale
the locale to resolve the code for (subclasses are encouraged to support internationalization)
locale
)

Implements abstract org.springframework.context.support.AbstractMessageSource.resolveCode.

Subclasses must implement this method to resolve a message.

protected String
resolveCodeWithoutArguments(String
the code of the message to resolve
code
,
Locale
the locale to resolve the code for (subclasses are encouraged to support internationalization)
locale
)

Overrides org.springframework.context.support.AbstractMessageSource.resolveCodeWithoutArguments.

Subclasses can override this method to resolve a message without arguments in an optimized fashion, i.e. to resolve without involving a MessageFormat.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from org.springframework.context.support.AbstractMessageSource:
getCommonMessagesgetDefaultMessagegetDefaultMessagegetMessagegetMessagegetMessagegetMessageFromParentgetMessageInternalgetParentMessageSourceisUseCodeAsDefaultMessageresolveArgumentssetCommonMessagessetParentMessageSourcesetUseCodeAsDefaultMessage

Field Detail

messageMapback to summary
private final Map<String, Map<Locale, StaticMessageSource.MessageHolder>> messageMap

Constructor Detail

StaticMessageSourceback to summary
public StaticMessageSource()

Method Detail

addMessageback to summary
public void addMessage(String code, Locale locale, String msg)

Associate the given message with the given code.

Parameters
code:String

the lookup code

locale:Locale

the locale that the message should be found within

msg:String

the message associated with this lookup code

addMessagesback to summary
public void addMessages(Map<String, String> messages, Locale locale)

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

Parameters
messages:Map<String, String>

the messages to register, with messages codes as keys and message texts as values

locale:Locale

the locale that the messages should be found within

resolveCodeback to summary
protected MessageFormat resolveCode(String code, Locale locale)

Implements abstract org.springframework.context.support.AbstractMessageSource.resolveCode.

Doc from org.springframework.context.support.AbstractMessageSource.resolveCode.

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

Parameters
code:String

the code of the message to resolve

locale:Locale

the locale to resolve the code for (subclasses are encouraged to support internationalization)

Returns:MessageFormat

the MessageFormat for the message, or null if not found

Annotations
@Override
@Nullable
resolveCodeWithoutArgumentsback to summary
protected String resolveCodeWithoutArguments(String code, Locale locale)

Overrides org.springframework.context.support.AbstractMessageSource.resolveCodeWithoutArguments.

Doc from org.springframework.context.support.AbstractMessageSource.resolveCodeWithoutArguments.

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 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:String

the code of the message to resolve

locale:Locale

the locale to resolve the code for (subclasses are encouraged to support internationalization)

Returns:String

the message String, or null if not found

Annotations
@Override
@Nullable
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
org.springframework.context.support back to summary

private Class StaticMessageSource.MessageHolder

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.context.support.StaticMessageSource.MessageHolder

Field Summary

Modifier and TypeField and Description
private volatile MessageFormat
private final Locale
private final String

Constructor Summary

AccessConstructor and Description
public
MessageHolder(String message, Locale locale)

Method Summary

Modifier and TypeMethod and Description
public String
public MessageFormat
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

cachedFormatback to summary
private volatile MessageFormat cachedFormat
Annotations
@Nullable
localeback to summary
private final Locale locale
messageback to summary
private final String message

Constructor Detail

MessageHolderback to summary
public MessageHolder(String message, Locale locale)

Method Detail

getMessageback to summary
public String getMessage()
getMessageFormatback to summary
public MessageFormat getMessageFormat()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override