From ce8b59fc7da00b529572ec793307d6615d0dfd6d Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 29 Sep 2017 16:09:30 +0200 Subject: [PATCH] Extract configuration of MessageSourceAutoConfiguration This commit extracts the configuration of MessageSourceAutoConfiguration in a dedicated object. Closes gh-9666 --- .../MessageSourceAutoConfiguration.java | 93 +++------------- .../context/MessageSourceProperties.java | 100 ++++++++++++++++++ .../MessageSourceAutoConfigurationTests.java | 27 +++-- 3 files changed, 132 insertions(+), 88 deletions(-) create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceProperties.java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfiguration.java index 61d05ccdc5e..3bd237dcd98 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfiguration.java @@ -16,8 +16,6 @@ package org.springframework.boot.autoconfigure.context; -import java.nio.charset.Charset; - import org.springframework.boot.autoconfigure.AutoConfigureOrder; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionMessage; @@ -53,98 +51,33 @@ import org.springframework.util.StringUtils; @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) @Conditional(ResourceBundleCondition.class) @EnableConfigurationProperties -@ConfigurationProperties(prefix = "spring.messages") public class MessageSourceAutoConfiguration { private static final Resource[] NO_RESOURCES = {}; - /** - * 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; + @Bean + @ConfigurationProperties(prefix = "spring.messages") + public MessageSourceProperties messageSourceProperties() { + return new MessageSourceProperties(); + } @Bean public MessageSource messageSource() { + MessageSourceProperties properties = messageSourceProperties(); ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); - if (StringUtils.hasText(this.basename)) { + if (StringUtils.hasText(properties.getBasename())) { messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray( - StringUtils.trimAllWhitespace(this.basename))); + StringUtils.trimAllWhitespace(properties.getBasename()))); } - if (this.encoding != null) { - messageSource.setDefaultEncoding(this.encoding.name()); + if (properties.getEncoding() != null) { + messageSource.setDefaultEncoding(properties.getEncoding().name()); } - messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale); - messageSource.setCacheSeconds(this.cacheSeconds); - messageSource.setAlwaysUseMessageFormat(this.alwaysUseMessageFormat); + messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale()); + messageSource.setCacheSeconds(properties.getCacheSeconds()); + messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat()); 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 { private static ConcurrentReferenceHashMap cache = new ConcurrentReferenceHashMap<>(); diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceProperties.java new file mode 100644 index 00000000000..b4a1fd94ba1 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceProperties.java @@ -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; + } + +} diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java index a839f1745a0..19501894d59 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java @@ -22,6 +22,7 @@ import org.junit.After; import org.junit.Ignore; import org.junit.Test; +import org.springframework.beans.DirectFieldAccessor; import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.MessageSource; @@ -104,31 +105,41 @@ public class MessageSourceAutoConfigurationTests { @Test public void testFallbackDefault() throws Exception { load("spring.messages.basename:test/messages"); - assertThat(this.context.getBean(MessageSourceAutoConfiguration.class) - .isFallbackToSystemLocale()).isTrue(); + assertThat(isFallbackToSystemLocale(this.context.getBean(MessageSource.class))) + .isTrue(); } @Test public void testFallbackTurnOff() throws Exception { load("spring.messages.basename:test/messages", "spring.messages.fallback-to-system-locale:false"); - assertThat(this.context.getBean(MessageSourceAutoConfiguration.class) - .isFallbackToSystemLocale()).isFalse(); + assertThat(isFallbackToSystemLocale(this.context.getBean(MessageSource.class))) + .isFalse(); } @Test public void testFormatMessageDefault() throws Exception { load("spring.messages.basename:test/messages"); - assertThat(this.context.getBean(MessageSourceAutoConfiguration.class) - .isAlwaysUseMessageFormat()).isFalse(); + assertThat(isAlwaysUseMessageFormat(this.context.getBean(MessageSource.class))) + .isFalse(); } @Test public void testFormatMessageOn() throws Exception { load("spring.messages.basename:test/messages", "spring.messages.always-use-message-format:true"); - assertThat(this.context.getBean(MessageSourceAutoConfiguration.class) - .isAlwaysUseMessageFormat()).isTrue(); + assertThat(isAlwaysUseMessageFormat(this.context.getBean(MessageSource.class))) + .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