Commit 5674a53d authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #15212 from cac03

* pr/15212:
  Polish "Configure MessageSource if no "messageSource" bean defined"
  Configure MessageSource if no "messageSource" bean defined
parents 2ac76b36 ec678eaa
......@@ -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
......
......@@ -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() {
@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];
}
};
return new TestMessageSource();
}
}
@Configuration
protected static class CustomBeanNameMessageSourceConfiguration {
@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];
}
}
......
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