Skip to content

Commit 976a23d

Browse files
committed
Fix duration unit of spring.messages.cache-duration
Closes gh-12183
1 parent 2b729bf commit 976a23d

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ public MessageSource messageSource() {
7676
}
7777
messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale());
7878
Duration cacheDuration = properties.getCacheDuration();
79-
messageSource.setCacheSeconds(
80-
cacheDuration == null ? -1 : (int) cacheDuration.getSeconds());
79+
if (cacheDuration != null) {
80+
messageSource.setCacheMillis(cacheDuration.toMillis());
81+
}
8182
messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
8283
messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
8384
return messageSource;

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import java.nio.charset.Charset;
2020
import java.nio.charset.StandardCharsets;
2121
import java.time.Duration;
22+
import java.time.temporal.ChronoUnit;
23+
24+
import org.springframework.boot.convert.DurationUnit;
2225

2326
/**
2427
* Configuration properties for Message Source.
@@ -44,8 +47,9 @@ public class MessageSourceProperties {
4447

4548
/**
4649
* Loaded resource bundle files cache duration. When not set, bundles are cached
47-
* forever.
50+
* forever. If a duration suffix is not specified, seconds will be used.
4851
*/
52+
@DurationUnit(ChronoUnit.SECONDS)
4953
private Duration cacheDuration;
5054

5155
/**

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
import org.springframework.beans.DirectFieldAccessor;
2525
import org.springframework.boot.autoconfigure.AutoConfigurations;
26+
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
2627
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
28+
import org.springframework.boot.test.context.runner.ContextConsumer;
2729
import org.springframework.context.MessageSource;
2830
import org.springframework.context.MessageSourceResolvable;
2931
import org.springframework.context.NoSuchMessageException;
@@ -82,6 +84,30 @@ public void testEncodingWorks() {
8284
.isEqualTo("Some text with some swedish öäå!"));
8385
}
8486

87+
@Test
88+
public void testCacheDurationNoUnit() {
89+
this.contextRunner
90+
.withPropertyValues("spring.messages.basename:test/messages",
91+
"spring.messages.cache-duration=10")
92+
.run(assertCache(10 * 1000));
93+
}
94+
95+
@Test
96+
public void testCacheDurationWithUnit() {
97+
this.contextRunner
98+
.withPropertyValues("spring.messages.basename:test/messages",
99+
"spring.messages.cache-duration=1m")
100+
.run(assertCache(60 * 1000));
101+
}
102+
103+
private ContextConsumer<AssertableApplicationContext> assertCache(long expected) {
104+
return (context) -> {
105+
assertThat(assertThat(context).hasSingleBean(MessageSource.class));
106+
assertThat(new DirectFieldAccessor(context.getBean(MessageSource.class))
107+
.getPropertyValue("cacheMillis")).isEqualTo(expected);
108+
};
109+
}
110+
85111
@Test
86112
public void testMultipleMessageSourceCreated() {
87113
this.contextRunner

spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ content into your application. Rather, pick only the properties that you need.
123123
# INTERNATIONALIZATION ({sc-spring-boot-autoconfigure}/context/MessageSourceProperties.{sc-ext}[MessageSourceProperties])
124124
spring.messages.always-use-message-format=false # Whether to always apply the MessageFormat rules, parsing even messages without arguments.
125125
spring.messages.basename=messages # Comma-separated list of basenames (essentially a fully-qualified classpath location), each following the ResourceBundle convention with relaxed support for slash based locations.
126-
spring.messages.cache-duration=-1 # Loaded resource bundle files cache duration. When not set, bundles are cached forever.
126+
spring.messages.cache-duration= # Loaded resource bundle files cache duration. When not set, bundles are cached forever. If a duration suffix is not specified, seconds will be used.
127127
spring.messages.encoding=UTF-8 # Message bundles encoding.
128128
spring.messages.fallback-to-system-locale=true # Whether to fall back to the system Locale if no files for a specific Locale have been found.
129129
spring.messages.use-code-as-default-message=false # Whether to use the message code as the default message instead of throwing a "NoSuchMessageException". Recommended during development only.

0 commit comments

Comments
 (0)