Skip to content

Commit ce8b59f

Browse files
committed
Extract configuration of MessageSourceAutoConfiguration
This commit extracts the configuration of MessageSourceAutoConfiguration in a dedicated object. Closes gh-9666
1 parent bbca612 commit ce8b59f

File tree

3 files changed

+132
-88
lines changed

3 files changed

+132
-88
lines changed

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

Lines changed: 13 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.autoconfigure.context;
1818

19-
import java.nio.charset.Charset;
20-
2119
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
2220
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2321
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
@@ -53,98 +51,33 @@
5351
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
5452
@Conditional(ResourceBundleCondition.class)
5553
@EnableConfigurationProperties
56-
@ConfigurationProperties(prefix = "spring.messages")
5754
public class MessageSourceAutoConfiguration {
5855

5956
private static final Resource[] NO_RESOURCES = {};
6057

61-
/**
62-
* Comma-separated list of basenames, each following the ResourceBundle convention.
63-
* Essentially a fully-qualified classpath location. If it doesn't contain a package
64-
* qualifier (such as "org.mypackage"), it will be resolved from the classpath root.
65-
*/
66-
private String basename = "messages";
67-
68-
/**
69-
* Message bundles encoding.
70-
*/
71-
private Charset encoding = Charset.forName("UTF-8");
72-
73-
/**
74-
* Loaded resource bundle files cache expiration, in seconds. When set to -1, bundles
75-
* are cached forever.
76-
*/
77-
private int cacheSeconds = -1;
78-
79-
/**
80-
* Set whether to fall back to the system Locale if no files for a specific Locale
81-
* have been found. if this is turned off, the only fallback will be the default file
82-
* (e.g. "messages.properties" for basename "messages").
83-
*/
84-
private boolean fallbackToSystemLocale = true;
85-
86-
/**
87-
* Set whether to always apply the MessageFormat rules, parsing even messages without
88-
* arguments.
89-
*/
90-
private boolean alwaysUseMessageFormat = false;
58+
@Bean
59+
@ConfigurationProperties(prefix = "spring.messages")
60+
public MessageSourceProperties messageSourceProperties() {
61+
return new MessageSourceProperties();
62+
}
9163

9264
@Bean
9365
public MessageSource messageSource() {
66+
MessageSourceProperties properties = messageSourceProperties();
9467
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
95-
if (StringUtils.hasText(this.basename)) {
68+
if (StringUtils.hasText(properties.getBasename())) {
9669
messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(
97-
StringUtils.trimAllWhitespace(this.basename)));
70+
StringUtils.trimAllWhitespace(properties.getBasename())));
9871
}
99-
if (this.encoding != null) {
100-
messageSource.setDefaultEncoding(this.encoding.name());
72+
if (properties.getEncoding() != null) {
73+
messageSource.setDefaultEncoding(properties.getEncoding().name());
10174
}
102-
messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale);
103-
messageSource.setCacheSeconds(this.cacheSeconds);
104-
messageSource.setAlwaysUseMessageFormat(this.alwaysUseMessageFormat);
75+
messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale());
76+
messageSource.setCacheSeconds(properties.getCacheSeconds());
77+
messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
10578
return messageSource;
10679
}
10780

108-
public String getBasename() {
109-
return this.basename;
110-
}
111-
112-
public void setBasename(String basename) {
113-
this.basename = basename;
114-
}
115-
116-
public Charset getEncoding() {
117-
return this.encoding;
118-
}
119-
120-
public void setEncoding(Charset encoding) {
121-
this.encoding = encoding;
122-
}
123-
124-
public int getCacheSeconds() {
125-
return this.cacheSeconds;
126-
}
127-
128-
public void setCacheSeconds(int cacheSeconds) {
129-
this.cacheSeconds = cacheSeconds;
130-
}
131-
132-
public boolean isFallbackToSystemLocale() {
133-
return this.fallbackToSystemLocale;
134-
}
135-
136-
public void setFallbackToSystemLocale(boolean fallbackToSystemLocale) {
137-
this.fallbackToSystemLocale = fallbackToSystemLocale;
138-
}
139-
140-
public boolean isAlwaysUseMessageFormat() {
141-
return this.alwaysUseMessageFormat;
142-
}
143-
144-
public void setAlwaysUseMessageFormat(boolean alwaysUseMessageFormat) {
145-
this.alwaysUseMessageFormat = alwaysUseMessageFormat;
146-
}
147-
14881
protected static class ResourceBundleCondition extends SpringBootCondition {
14982

15083
private static ConcurrentReferenceHashMap<String, ConditionOutcome> cache = new ConcurrentReferenceHashMap<>();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.context;
18+
19+
import java.nio.charset.Charset;
20+
21+
/**
22+
* Configuration properties for Message Source.
23+
*
24+
* @author Stephane Nicoll
25+
* @since 2.0.0
26+
*/
27+
public class MessageSourceProperties {
28+
29+
/**
30+
* Comma-separated list of basenames, each following the ResourceBundle convention.
31+
* Essentially a fully-qualified classpath location. If it doesn't contain a package
32+
* qualifier (such as "org.mypackage"), it will be resolved from the classpath root.
33+
*/
34+
private String basename = "messages";
35+
36+
/**
37+
* Message bundles encoding.
38+
*/
39+
private Charset encoding = Charset.forName("UTF-8");
40+
41+
/**
42+
* Loaded resource bundle files cache expiration, in seconds. When set to -1, bundles
43+
* are cached forever.
44+
*/
45+
private int cacheSeconds = -1;
46+
47+
/**
48+
* Set whether to fall back to the system Locale if no files for a specific Locale
49+
* have been found. if this is turned off, the only fallback will be the default file
50+
* (e.g. "messages.properties" for basename "messages").
51+
*/
52+
private boolean fallbackToSystemLocale = true;
53+
54+
/**
55+
* Set whether to always apply the MessageFormat rules, parsing even messages without
56+
* arguments.
57+
*/
58+
private boolean alwaysUseMessageFormat = false;
59+
60+
public String getBasename() {
61+
return this.basename;
62+
}
63+
64+
public void setBasename(String basename) {
65+
this.basename = basename;
66+
}
67+
68+
public Charset getEncoding() {
69+
return this.encoding;
70+
}
71+
72+
public void setEncoding(Charset encoding) {
73+
this.encoding = encoding;
74+
}
75+
76+
public int getCacheSeconds() {
77+
return this.cacheSeconds;
78+
}
79+
80+
public void setCacheSeconds(int cacheSeconds) {
81+
this.cacheSeconds = cacheSeconds;
82+
}
83+
84+
public boolean isFallbackToSystemLocale() {
85+
return this.fallbackToSystemLocale;
86+
}
87+
88+
public void setFallbackToSystemLocale(boolean fallbackToSystemLocale) {
89+
this.fallbackToSystemLocale = fallbackToSystemLocale;
90+
}
91+
92+
public boolean isAlwaysUseMessageFormat() {
93+
return this.alwaysUseMessageFormat;
94+
}
95+
96+
public void setAlwaysUseMessageFormat(boolean alwaysUseMessageFormat) {
97+
this.alwaysUseMessageFormat = alwaysUseMessageFormat;
98+
}
99+
100+
}

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.Ignore;
2323
import org.junit.Test;
2424

25+
import org.springframework.beans.DirectFieldAccessor;
2526
import org.springframework.boot.test.util.TestPropertyValues;
2627
import org.springframework.context.ConfigurableApplicationContext;
2728
import org.springframework.context.MessageSource;
@@ -104,31 +105,41 @@ public void testMessageSourceFromPropertySourceAnnotation() throws Exception {
104105
@Test
105106
public void testFallbackDefault() throws Exception {
106107
load("spring.messages.basename:test/messages");
107-
assertThat(this.context.getBean(MessageSourceAutoConfiguration.class)
108-
.isFallbackToSystemLocale()).isTrue();
108+
assertThat(isFallbackToSystemLocale(this.context.getBean(MessageSource.class)))
109+
.isTrue();
109110
}
110111

111112
@Test
112113
public void testFallbackTurnOff() throws Exception {
113114
load("spring.messages.basename:test/messages",
114115
"spring.messages.fallback-to-system-locale:false");
115-
assertThat(this.context.getBean(MessageSourceAutoConfiguration.class)
116-
.isFallbackToSystemLocale()).isFalse();
116+
assertThat(isFallbackToSystemLocale(this.context.getBean(MessageSource.class)))
117+
.isFalse();
117118
}
118119

119120
@Test
120121
public void testFormatMessageDefault() throws Exception {
121122
load("spring.messages.basename:test/messages");
122-
assertThat(this.context.getBean(MessageSourceAutoConfiguration.class)
123-
.isAlwaysUseMessageFormat()).isFalse();
123+
assertThat(isAlwaysUseMessageFormat(this.context.getBean(MessageSource.class)))
124+
.isFalse();
124125
}
125126

126127
@Test
127128
public void testFormatMessageOn() throws Exception {
128129
load("spring.messages.basename:test/messages",
129130
"spring.messages.always-use-message-format:true");
130-
assertThat(this.context.getBean(MessageSourceAutoConfiguration.class)
131-
.isAlwaysUseMessageFormat()).isTrue();
131+
assertThat(isAlwaysUseMessageFormat(this.context.getBean(MessageSource.class)))
132+
.isTrue();
133+
}
134+
135+
private boolean isFallbackToSystemLocale(MessageSource messageSource) {
136+
return (boolean) new DirectFieldAccessor(messageSource)
137+
.getPropertyValue("fallbackToSystemLocale");
138+
}
139+
140+
private boolean isAlwaysUseMessageFormat(MessageSource messageSource) {
141+
return (boolean) new DirectFieldAccessor(messageSource)
142+
.getPropertyValue("alwaysUseMessageFormat");
132143
}
133144

134145
@Test

0 commit comments

Comments
 (0)