lazy creation of MessageFormats

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3593 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Juergen Hoeller 2010-08-15 23:04:02 +00:00
parent bcd552abfe
commit d2a5c927ce
1 changed files with 24 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -36,25 +36,44 @@ import org.springframework.util.Assert;
public class StaticMessageSource extends AbstractMessageSource { public class StaticMessageSource extends AbstractMessageSource {
/** Map from 'code + locale' keys to message Strings */ /** Map from 'code + locale' keys to message Strings */
private final Map<String, MessageFormat> messages = new HashMap<String, MessageFormat>(); private final Map<String, String> messages = new HashMap<String, String>();
private final Map<String, MessageFormat> cachedMessageFormats = new HashMap<String, MessageFormat>();
@Override @Override
protected MessageFormat resolveCode(String code, Locale locale) { protected String resolveCodeWithoutArguments(String code, Locale locale) {
return this.messages.get(code + "_" + locale.toString()); return this.messages.get(code + "_" + locale.toString());
} }
@Override
protected MessageFormat resolveCode(String code, Locale locale) {
String key = code + "_" + locale.toString();
String msg = this.messages.get(key);
if (msg == null) {
return null;
}
synchronized (this.cachedMessageFormats) {
MessageFormat messageFormat = this.cachedMessageFormats.get(key);
if (messageFormat == null) {
messageFormat = createMessageFormat(msg, locale);
this.cachedMessageFormats.put(key, messageFormat);
}
return messageFormat;
}
}
/** /**
* Associate the given message with the given code. * Associate the given message with the given code.
* @param code the lookup code * @param code the lookup code
* @param locale the locale that the message should be found within * @param locale the locale that the message should be found within
* @param msg the message associated with this lookup code * @param msg the message associated with this lookup code
*/ */
public void addMessage(String code, Locale locale, String msg) { public void addMessage(String code, Locale locale, String msg) {
Assert.notNull(code, "Code must not be null"); Assert.notNull(code, "Code must not be null");
Assert.notNull(locale, "Locale must not be null"); Assert.notNull(locale, "Locale must not be null");
Assert.notNull(msg, "Message must not be null"); Assert.notNull(msg, "Message must not be null");
this.messages.put(code + "_" + locale.toString(), createMessageFormat(msg, locale)); this.messages.put(code + "_" + locale.toString(), msg);
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Added message [" + msg + "] for code [" + code + "] and Locale [" + locale + "]"); logger.debug("Added message [" + msg + "] for code [" + code + "] and Locale [" + locale + "]");
} }