From c236db04ef4c66342f22e44441432cd36b509990 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 16 Oct 2015 11:07:39 +0100 Subject: [PATCH] Ignore parent contexts in message source auto-configuration This commit applies the changes made in 68b55ad to 1.2.x (it was originally only made in 1.0.x and master). It also adds some tests. Closes gh-3803 --- .../MessageSourceAutoConfiguration.java | 3 +- .../MessageSourceAutoConfigurationTests.java | 65 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) 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 096a9c75f2a..14398e0db7c 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 @@ -23,6 +23,7 @@ import java.util.Set; import org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration.ResourceBundleCondition; import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.SearchStrategy; import org.springframework.boot.autoconfigure.condition.SpringBootCondition; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -50,7 +51,7 @@ import static org.springframework.util.StringUtils.trimAllWhitespace; * @author Phillip Webb */ @Configuration -@ConditionalOnMissingBean(MessageSource.class) +@ConditionalOnMissingBean(value = MessageSource.class, search = SearchStrategy.CURRENT) @Order(Ordered.HIGHEST_PRECEDENCE) @Conditional(ResourceBundleCondition.class) @EnableConfigurationProperties 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 c1f2a38a4a4..238d44b7e27 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 @@ -21,7 +21,12 @@ import java.util.Locale; import org.junit.Ignore; import org.junit.Test; import org.springframework.boot.test.EnvironmentTestUtils; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.MessageSource; +import org.springframework.context.MessageSourceResolvable; +import org.springframework.context.NoSuchMessageException; import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @@ -107,9 +112,69 @@ public class MessageSourceAutoConfigurationTests { this.context.getMessage("foo", null, "Foo message", Locale.UK)); } + @Test + public void existingMessageSourceIsPreferred() { + this.context = new AnnotationConfigApplicationContext(); + this.context.register(CustomMessageSource.class, + MessageSourceAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + assertEquals("foo", this.context.getMessage("foo", null, null, null)); + } + + @Test + public void existingMessageSourceInParentIsIgnored() { + ConfigurableApplicationContext parent = new AnnotationConfigApplicationContext(); + parent.refresh(); + try { + this.context = new AnnotationConfigApplicationContext(); + this.context.setParent(parent); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.messages.basename:test/messages"); + this.context.register(MessageSourceAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + assertEquals("bar", + this.context.getMessage("foo", null, "Foo message", Locale.UK)); + } + finally { + parent.close(); + } + } + @Configuration @PropertySource("classpath:/switch-messages.properties") protected static class Config { } + + @Configuration + protected static class CustomMessageSource { + + @Bean + public MessageSource messageSource() { + return new 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]; + } + + }; + } + + } }