From 7be2d97d49b89d8da1449faccc1c6b86c018f60d Mon Sep 17 00:00:00 2001 From: Chris Savory Date: Wed, 19 Mar 2014 13:21:44 -0700 Subject: [PATCH] Allow multiple MessageSources that are comma separated. Fixes gh-532, Fixes gh-506 --- .../MessageSourceAutoConfiguration.java | 10 ++++++++-- .../MessageSourceAutoConfigurationTests.java | 20 ++++++++++++++++--- .../test/resources/test/messages2.properties | 1 + 3 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 spring-boot-autoconfigure/src/test/resources/test/messages2.properties diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfiguration.java index ba6de3589ff..d57af0a8dcc 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfiguration.java @@ -16,6 +16,9 @@ package org.springframework.boot.autoconfigure; +import static org.springframework.util.StringUtils.commaDelimitedListToStringArray; +import static org.springframework.util.StringUtils.trimAllWhitespace; + import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.context.EnvironmentAware; @@ -26,10 +29,11 @@ import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.core.env.Environment; +import org.springframework.util.StringUtils; /** * {@link EnableAutoConfiguration Auto-configuration} for {@link MessageSource}. - * + * * @author Dave Syer */ @Configuration @@ -48,7 +52,9 @@ public class MessageSourceAutoConfiguration implements EnvironmentAware { public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); String basename = this.environment.getProperty("basename", "messages"); - messageSource.setBasename(basename); + if (StringUtils.hasText(basename)) { + messageSource.setBasenames(commaDelimitedListToStringArray(trimAllWhitespace(basename))); + } String encoding = this.environment.getProperty("encoding", "utf-8"); messageSource.setDefaultEncoding(encoding); return messageSource; diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfigurationTests.java index bfce17b1776..b053cddb90e 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfigurationTests.java @@ -16,17 +16,17 @@ package org.springframework.boot.autoconfigure; +import static org.junit.Assert.assertEquals; + import java.util.Locale; import org.junit.Test; import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import static org.junit.Assert.assertEquals; - /** * Tests for {@link MessageSourceAutoConfiguration}. - * + * * @author Dave Syer */ public class MessageSourceAutoConfigurationTests { @@ -55,6 +55,20 @@ public class MessageSourceAutoConfigurationTests { this.context.getMessage("foo", null, "Foo message", Locale.UK)); } + @Test + public void testMultipleMessageSourceCreated() throws Exception { + this.context = new AnnotationConfigApplicationContext(); + this.context.register(MessageSourceAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.messages.basename:test/messages,test/messages2"); + this.context.refresh(); + assertEquals("bar", + this.context.getMessage("foo", null, "Foo message", Locale.UK)); + assertEquals("bar-bar", + this.context.getMessage("foo-foo", null, "Foo-Foo message", Locale.UK)); + } + @Test public void testBadEncoding() throws Exception { this.context = new AnnotationConfigApplicationContext(); diff --git a/spring-boot-autoconfigure/src/test/resources/test/messages2.properties b/spring-boot-autoconfigure/src/test/resources/test/messages2.properties new file mode 100644 index 00000000000..d5962a89bb5 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/resources/test/messages2.properties @@ -0,0 +1 @@ +foo-foo=bar-bar \ No newline at end of file