Merge pull request #15212 from cac03

* pr/15212:
  Polish "Configure MessageSource if no "messageSource" bean defined"
  Configure MessageSource if no "messageSource" bean defined
This commit is contained in:
Stephane Nicoll
2018-12-03 11:32:23 +01:00
2 changed files with 43 additions and 20 deletions

View File

@@ -33,6 +33,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.Ordered;
import org.springframework.core.io.Resource;
@@ -49,7 +50,7 @@ import org.springframework.util.StringUtils;
* @author Eddú Meléndez
*/
@Configuration
@ConditionalOnMissingBean(value = MessageSource.class, search = SearchStrategy.CURRENT)
@ConditionalOnMissingBean(name = AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME, search = SearchStrategy.CURRENT)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Conditional(ResourceBundleCondition.class)
@EnableConfigurationProperties

View File

@@ -184,7 +184,7 @@ public class MessageSourceAutoConfigurationTests {
@Test
public void existingMessageSourceIsPreferred() {
this.contextRunner.withUserConfiguration(CustomMessageSource.class)
this.contextRunner.withUserConfiguration(CustomMessageSourceConfiguration.class)
.run((context) -> assertThat(context.getMessage("foo", null, null, null))
.isEqualTo("foo"));
}
@@ -198,6 +198,16 @@ public class MessageSourceAutoConfigurationTests {
.isEqualTo("bar")));
}
@Test
public void messageSourceWithNonStandardBeanNameIsIgnored() {
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
.withUserConfiguration(CustomBeanNameMessageSourceConfiguration.class)
.run((context) -> {
assertThat(context.getMessage("foo", null, Locale.US))
.isEqualTo("bar");
});
}
@Configuration
@PropertySource("classpath:/switch-messages.properties")
protected static class Config {
@@ -205,31 +215,43 @@ public class MessageSourceAutoConfigurationTests {
}
@Configuration
protected static class CustomMessageSource {
protected static class CustomMessageSourceConfiguration {
@Bean
public MessageSource messageSource() {
return new MessageSource() {
return new TestMessageSource();
}
@Override
public String getMessage(String code, Object[] args,
String defaultMessage, Locale locale) {
return code;
}
}
@Override
public String getMessage(String code, Object[] args, Locale locale)
throws NoSuchMessageException {
return code;
}
@Configuration
protected static class CustomBeanNameMessageSourceConfiguration {
@Override
public String getMessage(MessageSourceResolvable resolvable,
Locale locale) throws NoSuchMessageException {
return resolvable.getCodes()[0];
}
@Bean
public MessageSource codeReturningMessageSource() {
return new TestMessageSource();
}
};
}
private static class TestMessageSource implements MessageSource {
@Override
public String getMessage(String code, Object[] args, String defaultMessage,
Locale locale) {
return code;
}
@Override
public String getMessage(String code, Object[] args, Locale locale)
throws NoSuchMessageException {
return code;
}
@Override
public String getMessage(MessageSourceResolvable resolvable, Locale locale)
throws NoSuchMessageException {
return resolvable.getCodes()[0];
}
}