Skip to content

Commit 06569af

Browse files
committed
Polish 'Allow common messages to be specified for message sources'
See gh-42472
1 parent 573ccc5 commit 06569af

File tree

4 files changed

+50
-32
lines changed

4 files changed

+50
-32
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfiguration.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.UncheckedIOException;
2121
import java.time.Duration;
22+
import java.util.List;
2223
import java.util.Properties;
2324

2425
import org.springframework.aot.hint.RuntimeHints;
@@ -41,6 +42,7 @@
4142
import org.springframework.context.annotation.ImportRuntimeHints;
4243
import org.springframework.context.support.AbstractApplicationContext;
4344
import org.springframework.context.support.ResourceBundleMessageSource;
45+
import org.springframework.core.CollectionFactory;
4446
import org.springframework.core.Ordered;
4547
import org.springframework.core.io.Resource;
4648
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
@@ -85,20 +87,24 @@ public MessageSource messageSource(MessageSourceProperties properties) {
8587
}
8688
messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
8789
messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
90+
messageSource.setCommonMessages(loadCommonMessages(properties.getCommonMessages()));
91+
return messageSource;
92+
}
8893

89-
try {
90-
if (properties.getCommonMessages() != null) {
91-
Properties commonProperties = new Properties();
92-
for (Resource commonResource : properties.getCommonMessages()) {
93-
PropertiesLoaderUtils.fillProperties(commonProperties, commonResource);
94-
}
95-
messageSource.setCommonMessages(commonProperties);
96-
}
94+
private Properties loadCommonMessages(List<Resource> resources) {
95+
if (CollectionUtils.isEmpty(resources)) {
96+
return null;
9797
}
98-
catch (IOException ex) {
99-
throw new UncheckedIOException("Failed to load common messages", ex);
98+
Properties properties = CollectionFactory.createSortedProperties(false);
99+
for (Resource resource : resources) {
100+
try {
101+
PropertiesLoaderUtils.fillProperties(properties, resource);
102+
}
103+
catch (IOException ex) {
104+
throw new UncheckedIOException("Failed to load common messages from '%s'".formatted(resource), ex);
105+
}
100106
}
101-
return messageSource;
107+
return properties;
102108
}
103109

104110
protected static class ResourceBundleCondition extends SpringBootCondition {

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class MessageSourceProperties {
4646
private List<String> basename = new ArrayList<>(List.of("messages"));
4747

4848
/**
49-
* Comma-separated list of locale-independent common messages.
49+
* List of locale-independent property file resources containing common messages.
5050
*/
5151
private List<Resource> commonMessages;
5252

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 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.
@@ -58,38 +58,38 @@ void testDefaultMessageSource() {
5858

5959
@Test
6060
void propertiesBundleWithSlashIsDetected() {
61-
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages").run((context) -> {
61+
this.contextRunner.withPropertyValues("spring.messages.basename=test/messages").run((context) -> {
6262
assertThat(context).hasSingleBean(MessageSource.class);
6363
assertThat(context.getMessage("foo", null, "Foo message", Locale.UK)).isEqualTo("bar");
6464
});
6565
}
6666

6767
@Test
6868
void propertiesBundleWithDotIsDetected() {
69-
this.contextRunner.withPropertyValues("spring.messages.basename:test.messages").run((context) -> {
69+
this.contextRunner.withPropertyValues("spring.messages.basename=test.messages").run((context) -> {
7070
assertThat(context).hasSingleBean(MessageSource.class);
7171
assertThat(context.getMessage("foo", null, "Foo message", Locale.UK)).isEqualTo("bar");
7272
});
7373
}
7474

7575
@Test
7676
void testEncodingWorks() {
77-
this.contextRunner.withPropertyValues("spring.messages.basename:test/swedish")
77+
this.contextRunner.withPropertyValues("spring.messages.basename=test/swedish")
7878
.run((context) -> assertThat(context.getMessage("foo", null, "Foo message", Locale.UK))
7979
.isEqualTo("Some text with some swedish öäå!"));
8080
}
8181

8282
@Test
8383
void testCacheDurationNoUnit() {
8484
this.contextRunner
85-
.withPropertyValues("spring.messages.basename:test/messages", "spring.messages.cache-duration=10")
85+
.withPropertyValues("spring.messages.basename=test/messages", "spring.messages.cache-duration=10")
8686
.run(assertCache(10 * 1000));
8787
}
8888

8989
@Test
9090
void testCacheDurationWithUnit() {
9191
this.contextRunner
92-
.withPropertyValues("spring.messages.basename:test/messages", "spring.messages.cache-duration=1m")
92+
.withPropertyValues("spring.messages.basename=test/messages", "spring.messages.cache-duration=1m")
9393
.run(assertCache(60 * 1000));
9494
}
9595

@@ -102,7 +102,7 @@ private ContextConsumer<AssertableApplicationContext> assertCache(long expected)
102102

103103
@Test
104104
void testMultipleMessageSourceCreated() {
105-
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages,test/messages2")
105+
this.contextRunner.withPropertyValues("spring.messages.basename=test/messages,test/messages2")
106106
.run((context) -> {
107107
assertThat(context.getMessage("foo", null, "Foo message", Locale.UK)).isEqualTo("bar");
108108
assertThat(context.getMessage("foo-foo", null, "Foo-Foo message", Locale.UK)).isEqualTo("bar-bar");
@@ -119,55 +119,65 @@ void testMessageSourceFromPropertySourceAnnotation() {
119119
@Test
120120
void testCommonMessages() {
121121
this.contextRunner
122-
.withPropertyValues("spring.messages.basename:test/messages",
123-
"spring.messages.common-messages:test/common-messages")
122+
.withPropertyValues("spring.messages.basename=test/messages",
123+
"spring.messages.common-messages=classpath:test/common-messages.properties")
124124
.run((context) -> assertThat(context.getMessage("hello", null, "Hello!", Locale.UK)).isEqualTo("world"));
125125
}
126126

127+
@Test
128+
void testCommonMessagesWhenNotFound() {
129+
this.contextRunner
130+
.withPropertyValues("spring.messages.basename=test/messages",
131+
"spring.messages.common-messages=classpath:test/common-messages-missing.properties")
132+
.run((context) -> assertThat(context).getFailure()
133+
.hasMessageContaining(
134+
"Failed to load common messages from 'class path resource [test/common-messages-missing.properties]'"));
135+
}
136+
127137
@Test
128138
void testFallbackDefault() {
129-
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
139+
this.contextRunner.withPropertyValues("spring.messages.basename=test/messages")
130140
.run((context) -> assertThat(context.getBean(MessageSource.class))
131141
.hasFieldOrPropertyWithValue("fallbackToSystemLocale", true));
132142
}
133143

134144
@Test
135145
void testFallbackTurnOff() {
136146
this.contextRunner
137-
.withPropertyValues("spring.messages.basename:test/messages",
147+
.withPropertyValues("spring.messages.basename=test/messages",
138148
"spring.messages.fallback-to-system-locale:false")
139149
.run((context) -> assertThat(context.getBean(MessageSource.class))
140150
.hasFieldOrPropertyWithValue("fallbackToSystemLocale", false));
141151
}
142152

143153
@Test
144154
void testFormatMessageDefault() {
145-
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
155+
this.contextRunner.withPropertyValues("spring.messages.basename=test/messages")
146156
.run((context) -> assertThat(context.getBean(MessageSource.class))
147157
.hasFieldOrPropertyWithValue("alwaysUseMessageFormat", false));
148158
}
149159

150160
@Test
151161
void testFormatMessageOn() {
152162
this.contextRunner
153-
.withPropertyValues("spring.messages.basename:test/messages",
163+
.withPropertyValues("spring.messages.basename=test/messages",
154164
"spring.messages.always-use-message-format:true")
155165
.run((context) -> assertThat(context.getBean(MessageSource.class))
156166
.hasFieldOrPropertyWithValue("alwaysUseMessageFormat", true));
157167
}
158168

159169
@Test
160170
void testUseCodeAsDefaultMessageDefault() {
161-
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
171+
this.contextRunner.withPropertyValues("spring.messages.basename=test/messages")
162172
.run((context) -> assertThat(context.getBean(MessageSource.class))
163173
.hasFieldOrPropertyWithValue("useCodeAsDefaultMessage", false));
164174
}
165175

166176
@Test
167177
void testUseCodeAsDefaultMessageOn() {
168178
this.contextRunner
169-
.withPropertyValues("spring.messages.basename:test/messages",
170-
"spring.messages.use-code-as-default-message:true")
179+
.withPropertyValues("spring.messages.basename=test/messages",
180+
"spring.messages.use-code-as-default-message=true")
171181
.run((context) -> assertThat(context.getBean(MessageSource.class))
172182
.hasFieldOrPropertyWithValue("useCodeAsDefaultMessage", true));
173183
}
@@ -181,13 +191,13 @@ void existingMessageSourceIsPreferred() {
181191
@Test
182192
void existingMessageSourceInParentIsIgnored() {
183193
this.contextRunner.run((parent) -> this.contextRunner.withParent(parent)
184-
.withPropertyValues("spring.messages.basename:test/messages")
194+
.withPropertyValues("spring.messages.basename=test/messages")
185195
.run((context) -> assertThat(context.getMessage("foo", null, "Foo message", Locale.UK)).isEqualTo("bar")));
186196
}
187197

188198
@Test
189199
void messageSourceWithNonStandardBeanNameIsIgnored() {
190-
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
200+
this.contextRunner.withPropertyValues("spring.messages.basename=test/messages")
191201
.withUserConfiguration(CustomBeanNameMessageSourceConfiguration.class)
192202
.run((context) -> assertThat(context.getMessage("foo", null, Locale.US)).isEqualTo("bar"));
193203
}

spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/internationalization.adoc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ The basename of the resource bundle as well as several other attributes can be c
1414
----
1515
spring:
1616
messages:
17-
basename: "messages,config.i18n.messages"
17+
basename: "messages, config.i18n.messages"
18+
common-messages: "classpath:my-common-messages.properties"
1819
fallback-to-system-locale: false
1920
----
2021

21-
TIP: `spring.messages.basename` supports comma-separated list of locations, either a package qualifier or a resource resolved from the classpath root.
22+
TIP: The configprop:spring.messages.basename[] property supports a list of locations, either a package qualifier or a resource resolved from the classpath root.
23+
The configprop:spring.messages.common-messages[] property supports a list of property file resources.
2224

2325
See javadoc:org.springframework.boot.autoconfigure.context.MessageSourceProperties[] for more supported options.

0 commit comments

Comments
 (0)