Commit c236db04 authored by Andy Wilkinson's avatar Andy Wilkinson

Ignore parent contexts in message source auto-configuration

This commit applies the changes made in 68b55ada to 1.2.x (it was
originally only made in 1.0.x and master). It also adds some tests.

Closes gh-3803
parent 35a3f4a1
...@@ -23,6 +23,7 @@ import java.util.Set; ...@@ -23,6 +23,7 @@ import java.util.Set;
import org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration.ResourceBundleCondition; import org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration.ResourceBundleCondition;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 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.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
...@@ -50,7 +51,7 @@ import static org.springframework.util.StringUtils.trimAllWhitespace; ...@@ -50,7 +51,7 @@ import static org.springframework.util.StringUtils.trimAllWhitespace;
* @author Phillip Webb * @author Phillip Webb
*/ */
@Configuration @Configuration
@ConditionalOnMissingBean(MessageSource.class) @ConditionalOnMissingBean(value = MessageSource.class, search = SearchStrategy.CURRENT)
@Order(Ordered.HIGHEST_PRECEDENCE) @Order(Ordered.HIGHEST_PRECEDENCE)
@Conditional(ResourceBundleCondition.class) @Conditional(ResourceBundleCondition.class)
@EnableConfigurationProperties @EnableConfigurationProperties
......
...@@ -21,7 +21,12 @@ import java.util.Locale; ...@@ -21,7 +21,12 @@ import java.util.Locale;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.test.EnvironmentTestUtils; 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.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
...@@ -107,9 +112,69 @@ public class MessageSourceAutoConfigurationTests { ...@@ -107,9 +112,69 @@ public class MessageSourceAutoConfigurationTests {
this.context.getMessage("foo", null, "Foo message", Locale.UK)); 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 @Configuration
@PropertySource("classpath:/switch-messages.properties") @PropertySource("classpath:/switch-messages.properties")
protected static class Config { 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];
}
};
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment