Browse Source

Allow multiple MessageSources that are comma separated.

Fixes gh-532, Fixes gh-506
pull/534/merge
Chris Savory 12 years ago committed by Dave Syer
parent
commit
7be2d97d49
  1. 10
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfiguration.java
  2. 20
      spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfigurationTests.java
  3. 1
      spring-boot-autoconfigure/src/test/resources/test/messages2.properties

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

@ -16,6 +16,9 @@ @@ -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; @@ -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 { @@ -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;

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

@ -16,17 +16,17 @@ @@ -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 { @@ -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();

1
spring-boot-autoconfigure/src/test/resources/test/messages2.properties

@ -0,0 +1 @@ @@ -0,0 +1 @@
foo-foo=bar-bar
Loading…
Cancel
Save