Extract configuration of MessageSourceAutoConfiguration
This commit extracts the configuration of MessageSourceAutoConfiguration in a dedicated object. Closes gh-9666
This commit is contained in:
parent
bbca612a57
commit
ce8b59fc7d
|
@ -16,8 +16,6 @@
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.context;
|
package org.springframework.boot.autoconfigure.context;
|
||||||
|
|
||||||
import java.nio.charset.Charset;
|
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
|
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
|
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
|
||||||
|
@ -53,98 +51,33 @@ import org.springframework.util.StringUtils;
|
||||||
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
|
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
|
||||||
@Conditional(ResourceBundleCondition.class)
|
@Conditional(ResourceBundleCondition.class)
|
||||||
@EnableConfigurationProperties
|
@EnableConfigurationProperties
|
||||||
@ConfigurationProperties(prefix = "spring.messages")
|
|
||||||
public class MessageSourceAutoConfiguration {
|
public class MessageSourceAutoConfiguration {
|
||||||
|
|
||||||
private static final Resource[] NO_RESOURCES = {};
|
private static final Resource[] NO_RESOURCES = {};
|
||||||
|
|
||||||
/**
|
@Bean
|
||||||
* Comma-separated list of basenames, each following the ResourceBundle convention.
|
@ConfigurationProperties(prefix = "spring.messages")
|
||||||
* Essentially a fully-qualified classpath location. If it doesn't contain a package
|
public MessageSourceProperties messageSourceProperties() {
|
||||||
* qualifier (such as "org.mypackage"), it will be resolved from the classpath root.
|
return new MessageSourceProperties();
|
||||||
*/
|
}
|
||||||
private String basename = "messages";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Message bundles encoding.
|
|
||||||
*/
|
|
||||||
private Charset encoding = Charset.forName("UTF-8");
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Loaded resource bundle files cache expiration, in seconds. When set to -1, bundles
|
|
||||||
* are cached forever.
|
|
||||||
*/
|
|
||||||
private int cacheSeconds = -1;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set whether to fall back to the system Locale if no files for a specific Locale
|
|
||||||
* have been found. if this is turned off, the only fallback will be the default file
|
|
||||||
* (e.g. "messages.properties" for basename "messages").
|
|
||||||
*/
|
|
||||||
private boolean fallbackToSystemLocale = true;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set whether to always apply the MessageFormat rules, parsing even messages without
|
|
||||||
* arguments.
|
|
||||||
*/
|
|
||||||
private boolean alwaysUseMessageFormat = false;
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public MessageSource messageSource() {
|
public MessageSource messageSource() {
|
||||||
|
MessageSourceProperties properties = messageSourceProperties();
|
||||||
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||||
if (StringUtils.hasText(this.basename)) {
|
if (StringUtils.hasText(properties.getBasename())) {
|
||||||
messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(
|
messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(
|
||||||
StringUtils.trimAllWhitespace(this.basename)));
|
StringUtils.trimAllWhitespace(properties.getBasename())));
|
||||||
}
|
}
|
||||||
if (this.encoding != null) {
|
if (properties.getEncoding() != null) {
|
||||||
messageSource.setDefaultEncoding(this.encoding.name());
|
messageSource.setDefaultEncoding(properties.getEncoding().name());
|
||||||
}
|
}
|
||||||
messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale);
|
messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale());
|
||||||
messageSource.setCacheSeconds(this.cacheSeconds);
|
messageSource.setCacheSeconds(properties.getCacheSeconds());
|
||||||
messageSource.setAlwaysUseMessageFormat(this.alwaysUseMessageFormat);
|
messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
|
||||||
return messageSource;
|
return messageSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBasename() {
|
|
||||||
return this.basename;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBasename(String basename) {
|
|
||||||
this.basename = basename;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Charset getEncoding() {
|
|
||||||
return this.encoding;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setEncoding(Charset encoding) {
|
|
||||||
this.encoding = encoding;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getCacheSeconds() {
|
|
||||||
return this.cacheSeconds;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCacheSeconds(int cacheSeconds) {
|
|
||||||
this.cacheSeconds = cacheSeconds;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isFallbackToSystemLocale() {
|
|
||||||
return this.fallbackToSystemLocale;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFallbackToSystemLocale(boolean fallbackToSystemLocale) {
|
|
||||||
this.fallbackToSystemLocale = fallbackToSystemLocale;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isAlwaysUseMessageFormat() {
|
|
||||||
return this.alwaysUseMessageFormat;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAlwaysUseMessageFormat(boolean alwaysUseMessageFormat) {
|
|
||||||
this.alwaysUseMessageFormat = alwaysUseMessageFormat;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static class ResourceBundleCondition extends SpringBootCondition {
|
protected static class ResourceBundleCondition extends SpringBootCondition {
|
||||||
|
|
||||||
private static ConcurrentReferenceHashMap<String, ConditionOutcome> cache = new ConcurrentReferenceHashMap<>();
|
private static ConcurrentReferenceHashMap<String, ConditionOutcome> cache = new ConcurrentReferenceHashMap<>();
|
||||||
|
|
|
@ -0,0 +1,100 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-2017 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.autoconfigure.context;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration properties for Message Source.
|
||||||
|
*
|
||||||
|
* @author Stephane Nicoll
|
||||||
|
* @since 2.0.0
|
||||||
|
*/
|
||||||
|
public class MessageSourceProperties {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Comma-separated list of basenames, each following the ResourceBundle convention.
|
||||||
|
* Essentially a fully-qualified classpath location. If it doesn't contain a package
|
||||||
|
* qualifier (such as "org.mypackage"), it will be resolved from the classpath root.
|
||||||
|
*/
|
||||||
|
private String basename = "messages";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Message bundles encoding.
|
||||||
|
*/
|
||||||
|
private Charset encoding = Charset.forName("UTF-8");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loaded resource bundle files cache expiration, in seconds. When set to -1, bundles
|
||||||
|
* are cached forever.
|
||||||
|
*/
|
||||||
|
private int cacheSeconds = -1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set whether to fall back to the system Locale if no files for a specific Locale
|
||||||
|
* have been found. if this is turned off, the only fallback will be the default file
|
||||||
|
* (e.g. "messages.properties" for basename "messages").
|
||||||
|
*/
|
||||||
|
private boolean fallbackToSystemLocale = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set whether to always apply the MessageFormat rules, parsing even messages without
|
||||||
|
* arguments.
|
||||||
|
*/
|
||||||
|
private boolean alwaysUseMessageFormat = false;
|
||||||
|
|
||||||
|
public String getBasename() {
|
||||||
|
return this.basename;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBasename(String basename) {
|
||||||
|
this.basename = basename;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Charset getEncoding() {
|
||||||
|
return this.encoding;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEncoding(Charset encoding) {
|
||||||
|
this.encoding = encoding;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCacheSeconds() {
|
||||||
|
return this.cacheSeconds;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCacheSeconds(int cacheSeconds) {
|
||||||
|
this.cacheSeconds = cacheSeconds;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isFallbackToSystemLocale() {
|
||||||
|
return this.fallbackToSystemLocale;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFallbackToSystemLocale(boolean fallbackToSystemLocale) {
|
||||||
|
this.fallbackToSystemLocale = fallbackToSystemLocale;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isAlwaysUseMessageFormat() {
|
||||||
|
return this.alwaysUseMessageFormat;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAlwaysUseMessageFormat(boolean alwaysUseMessageFormat) {
|
||||||
|
this.alwaysUseMessageFormat = alwaysUseMessageFormat;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -22,6 +22,7 @@ import org.junit.After;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.beans.DirectFieldAccessor;
|
||||||
import org.springframework.boot.test.util.TestPropertyValues;
|
import org.springframework.boot.test.util.TestPropertyValues;
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
import org.springframework.context.MessageSource;
|
import org.springframework.context.MessageSource;
|
||||||
|
@ -104,31 +105,41 @@ public class MessageSourceAutoConfigurationTests {
|
||||||
@Test
|
@Test
|
||||||
public void testFallbackDefault() throws Exception {
|
public void testFallbackDefault() throws Exception {
|
||||||
load("spring.messages.basename:test/messages");
|
load("spring.messages.basename:test/messages");
|
||||||
assertThat(this.context.getBean(MessageSourceAutoConfiguration.class)
|
assertThat(isFallbackToSystemLocale(this.context.getBean(MessageSource.class)))
|
||||||
.isFallbackToSystemLocale()).isTrue();
|
.isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFallbackTurnOff() throws Exception {
|
public void testFallbackTurnOff() throws Exception {
|
||||||
load("spring.messages.basename:test/messages",
|
load("spring.messages.basename:test/messages",
|
||||||
"spring.messages.fallback-to-system-locale:false");
|
"spring.messages.fallback-to-system-locale:false");
|
||||||
assertThat(this.context.getBean(MessageSourceAutoConfiguration.class)
|
assertThat(isFallbackToSystemLocale(this.context.getBean(MessageSource.class)))
|
||||||
.isFallbackToSystemLocale()).isFalse();
|
.isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFormatMessageDefault() throws Exception {
|
public void testFormatMessageDefault() throws Exception {
|
||||||
load("spring.messages.basename:test/messages");
|
load("spring.messages.basename:test/messages");
|
||||||
assertThat(this.context.getBean(MessageSourceAutoConfiguration.class)
|
assertThat(isAlwaysUseMessageFormat(this.context.getBean(MessageSource.class)))
|
||||||
.isAlwaysUseMessageFormat()).isFalse();
|
.isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFormatMessageOn() throws Exception {
|
public void testFormatMessageOn() throws Exception {
|
||||||
load("spring.messages.basename:test/messages",
|
load("spring.messages.basename:test/messages",
|
||||||
"spring.messages.always-use-message-format:true");
|
"spring.messages.always-use-message-format:true");
|
||||||
assertThat(this.context.getBean(MessageSourceAutoConfiguration.class)
|
assertThat(isAlwaysUseMessageFormat(this.context.getBean(MessageSource.class)))
|
||||||
.isAlwaysUseMessageFormat()).isTrue();
|
.isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isFallbackToSystemLocale(MessageSource messageSource) {
|
||||||
|
return (boolean) new DirectFieldAccessor(messageSource)
|
||||||
|
.getPropertyValue("fallbackToSystemLocale");
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isAlwaysUseMessageFormat(MessageSource messageSource) {
|
||||||
|
return (boolean) new DirectFieldAccessor(messageSource)
|
||||||
|
.getPropertyValue("alwaysUseMessageFormat");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue