Skip to content

Commit

Permalink
Ignore parent contexts in message source auto-configuration
Browse files Browse the repository at this point in the history
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 spring-projectsgh-3803
  • Loading branch information
wilkinsona committed Oct 16, 2015
1 parent 35a3f4a commit c236db0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -50,7 +51,7 @@
* @author Phillip Webb
*/
@Configuration
@ConditionalOnMissingBean(MessageSource.class)
@ConditionalOnMissingBean(value = MessageSource.class, search = SearchStrategy.CURRENT)
@Order(Ordered.HIGHEST_PRECEDENCE)
@Conditional(ResourceBundleCondition.class)
@EnableConfigurationProperties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
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;

Expand Down Expand Up @@ -107,9 +112,69 @@ public void testMessageSourceFromPropertySourceAnnotation() throws Exception {
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];
}

};
}

}
}

0 comments on commit c236db0

Please sign in to comment.