From 08271fa445ec391635533773f09fa7df84380eae Mon Sep 17 00:00:00 2001 From: kanghailin <1227734668@qq.com> Date: Tue, 19 Sep 2023 17:36:52 +0800 Subject: [PATCH 1/2] Optimize MessageSourceSupport See gh-31267 --- .../context/support/MessageSourceSupport.java | 40 +++++++------------ 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/support/MessageSourceSupport.java b/spring-context/src/main/java/org/springframework/context/support/MessageSourceSupport.java index 79acbfaf2d9..795ec5462b5 100644 --- a/spring-context/src/main/java/org/springframework/context/support/MessageSourceSupport.java +++ b/spring-context/src/main/java/org/springframework/context/support/MessageSourceSupport.java @@ -17,9 +17,9 @@ package org.springframework.context.support; import java.text.MessageFormat; -import java.util.HashMap; import java.util.Locale; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -53,7 +53,7 @@ public abstract class MessageSourceSupport { * Used for passed-in default messages. MessageFormats for resolved * codes are cached on a specific basis in subclasses. */ - private final Map> messageFormatsPerMessage = new HashMap<>(); + private final Map> messageFormatsPerMessage = new ConcurrentHashMap<>(); /** @@ -116,32 +116,22 @@ public abstract class MessageSourceSupport { if (!isAlwaysUseMessageFormat() && ObjectUtils.isEmpty(args)) { return msg; } - MessageFormat messageFormat = null; - synchronized (this.messageFormatsPerMessage) { - Map messageFormatsPerLocale = this.messageFormatsPerMessage.get(msg); - if (messageFormatsPerLocale != null) { - messageFormat = messageFormatsPerLocale.get(locale); + Map messageFormatsPerLocale = this.messageFormatsPerMessage + .computeIfAbsent(msg, key -> new ConcurrentHashMap<>()); + MessageFormat messageFormat = messageFormatsPerLocale.computeIfAbsent(locale, key -> { + try { + return createMessageFormat(msg, locale); } - else { - messageFormatsPerLocale = new HashMap<>(); - this.messageFormatsPerMessage.put(msg, messageFormatsPerLocale); - } - if (messageFormat == null) { - try { - messageFormat = createMessageFormat(msg, locale); - } - catch (IllegalArgumentException ex) { - // Invalid message format - probably not intended for formatting, - // rather using a message structure with no arguments involved... - if (isAlwaysUseMessageFormat()) { - throw ex; - } - // Silently proceed with raw message if format not enforced... - messageFormat = INVALID_MESSAGE_FORMAT; + catch (IllegalArgumentException ex) { + // Invalid message format - probably not intended for formatting, + // rather using a message structure with no arguments involved... + if (isAlwaysUseMessageFormat()) { + throw ex; } - messageFormatsPerLocale.put(locale, messageFormat); + // Silently proceed with raw message if format not enforced... + return INVALID_MESSAGE_FORMAT; } - } + }); if (messageFormat == INVALID_MESSAGE_FORMAT) { return msg; } From 182f9117aa4fea8e03631b2ddd3c8e8d14405c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Wed, 27 Sep 2023 09:14:20 +0200 Subject: [PATCH 2/2] Update copyright year of changed file See gh-31267 --- .../springframework/context/support/MessageSourceSupport.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-context/src/main/java/org/springframework/context/support/MessageSourceSupport.java b/spring-context/src/main/java/org/springframework/context/support/MessageSourceSupport.java index 795ec5462b5..9aaaf33bcdf 100644 --- a/spring-context/src/main/java/org/springframework/context/support/MessageSourceSupport.java +++ b/spring-context/src/main/java/org/springframework/context/support/MessageSourceSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.