Skip to content

Commit a3bfa75

Browse files
committed
MessageSourceSupport uses locale-specific MessageFormat cache for default messages
Issue: SPR-9607
1 parent 8e3a007 commit a3bfa75

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

org.springframework.context/src/main/java/org/springframework/context/support/MessageSourceSupport.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,7 +52,8 @@ public abstract class MessageSourceSupport {
5252
* Used for passed-in default messages. MessageFormats for resolved
5353
* codes are cached on a specific basis in subclasses.
5454
*/
55-
private final Map<String, MessageFormat> cachedMessageFormats = new HashMap<String, MessageFormat>();
55+
private final Map<String, Map<Locale, MessageFormat>> messageFormatsPerMessage =
56+
new HashMap<String, Map<Locale, MessageFormat>>();
5657

5758

5859
/**
@@ -114,9 +115,16 @@ protected String formatMessage(String msg, Object[] args, Locale locale) {
114115
if (msg == null || (!this.alwaysUseMessageFormat && ObjectUtils.isEmpty(args))) {
115116
return msg;
116117
}
117-
MessageFormat messageFormat;
118-
synchronized (this.cachedMessageFormats) {
119-
messageFormat = this.cachedMessageFormats.get(msg);
118+
MessageFormat messageFormat = null;
119+
synchronized (this.messageFormatsPerMessage) {
120+
Map<Locale, MessageFormat> messageFormatsPerLocale = this.messageFormatsPerMessage.get(msg);
121+
if (messageFormatsPerLocale != null) {
122+
messageFormat = messageFormatsPerLocale.get(locale);
123+
}
124+
else {
125+
messageFormatsPerLocale = new HashMap<Locale, MessageFormat>();
126+
this.messageFormatsPerMessage.put(msg, messageFormatsPerLocale);
127+
}
120128
if (messageFormat == null) {
121129
try {
122130
messageFormat = createMessageFormat(msg, locale);
@@ -130,7 +138,7 @@ protected String formatMessage(String msg, Object[] args, Locale locale) {
130138
// silently proceed with raw message if format not enforced
131139
messageFormat = INVALID_MESSAGE_FORMAT;
132140
}
133-
this.cachedMessageFormats.put(msg, messageFormat);
141+
messageFormatsPerLocale.put(locale, messageFormat);
134142
}
135143
}
136144
if (messageFormat == INVALID_MESSAGE_FORMAT) {
@@ -153,9 +161,8 @@ protected MessageFormat createMessageFormat(String msg, Locale locale) {
153161

154162
/**
155163
* Template method for resolving argument objects.
156-
* <p>The default implementation simply returns the given argument
157-
* array as-is. Can be overridden in subclasses in order to resolve
158-
* special argument types.
164+
* <p>The default implementation simply returns the given argument array as-is.
165+
* Can be overridden in subclasses in order to resolve special argument types.
159166
* @param args the original argument array
160167
* @param locale the Locale to resolve against
161168
* @return the resolved argument array

0 commit comments

Comments
 (0)