Allow multiple MessageSources that are comma separated.
Fixes gh-532, Fixes gh-506
This commit is contained in:
parent
632af6b1ab
commit
7be2d97d49
|
@ -16,6 +16,9 @@
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure;
|
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.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
||||||
import org.springframework.context.EnvironmentAware;
|
import org.springframework.context.EnvironmentAware;
|
||||||
|
@ -26,6 +29,7 @@ import org.springframework.context.support.ResourceBundleMessageSource;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link EnableAutoConfiguration Auto-configuration} for {@link MessageSource}.
|
* {@link EnableAutoConfiguration Auto-configuration} for {@link MessageSource}.
|
||||||
|
@ -48,7 +52,9 @@ public class MessageSourceAutoConfiguration implements EnvironmentAware {
|
||||||
public MessageSource messageSource() {
|
public MessageSource messageSource() {
|
||||||
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||||
String basename = this.environment.getProperty("basename", "messages");
|
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");
|
String encoding = this.environment.getProperty("encoding", "utf-8");
|
||||||
messageSource.setDefaultEncoding(encoding);
|
messageSource.setDefaultEncoding(encoding);
|
||||||
return messageSource;
|
return messageSource;
|
||||||
|
|
|
@ -16,14 +16,14 @@
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure;
|
package org.springframework.boot.autoconfigure;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link MessageSourceAutoConfiguration}.
|
* Tests for {@link MessageSourceAutoConfiguration}.
|
||||||
*
|
*
|
||||||
|
@ -55,6 +55,20 @@ public class MessageSourceAutoConfigurationTests {
|
||||||
this.context.getMessage("foo", null, "Foo message", Locale.UK));
|
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
|
@Test
|
||||||
public void testBadEncoding() throws Exception {
|
public void testBadEncoding() throws Exception {
|
||||||
this.context = new AnnotationConfigApplicationContext();
|
this.context = new AnnotationConfigApplicationContext();
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
foo-foo=bar-bar
|
Loading…
Reference in New Issue