Polish "Configure MessageSource if no "messageSource" bean defined"
Closes gh-15212
This commit is contained in:
parent
82d99da32a
commit
ec678eaa3b
|
|
@ -184,7 +184,7 @@ public class MessageSourceAutoConfigurationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void existingMessageSourceIsPreferred() {
|
public void existingMessageSourceIsPreferred() {
|
||||||
this.contextRunner.withUserConfiguration(CustomMessageSource.class)
|
this.contextRunner.withUserConfiguration(CustomMessageSourceConfiguration.class)
|
||||||
.run((context) -> assertThat(context.getMessage("foo", null, null, null))
|
.run((context) -> assertThat(context.getMessage("foo", null, null, null))
|
||||||
.isEqualTo("foo"));
|
.isEqualTo("foo"));
|
||||||
}
|
}
|
||||||
|
|
@ -199,16 +199,42 @@ public class MessageSourceAutoConfigurationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void messageSourceDeclaredWithNonStandardNameDoesNotBreakAutoConfig() {
|
public void messageSourceWithNonStandardBeanNameIsIgnored() {
|
||||||
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
|
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
|
||||||
.withUserConfiguration(ConfigWithNonStandardMessageSourceBeanName.class)
|
.withUserConfiguration(CustomBeanNameMessageSourceConfiguration.class)
|
||||||
.run((context) -> {
|
.run((context) -> {
|
||||||
assertThat(context.getMessage("foo", null, Locale.US))
|
assertThat(context.getMessage("foo", null, Locale.US))
|
||||||
.isEqualTo("bar");
|
.isEqualTo("bar");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class CodeReturningMessageSource implements MessageSource {
|
@Configuration
|
||||||
|
@PropertySource("classpath:/switch-messages.properties")
|
||||||
|
protected static class Config {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
protected static class CustomMessageSourceConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public MessageSource messageSource() {
|
||||||
|
return new TestMessageSource();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
protected static class CustomBeanNameMessageSourceConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public MessageSource codeReturningMessageSource() {
|
||||||
|
return new TestMessageSource();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class TestMessageSource implements MessageSource {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getMessage(String code, Object[] args, String defaultMessage,
|
public String getMessage(String code, Object[] args, String defaultMessage,
|
||||||
|
|
@ -230,30 +256,4 @@ public class MessageSourceAutoConfigurationTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
|
||||||
protected static class ConfigWithNonStandardMessageSourceBeanName {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public MessageSource codeReturningMessageSource() {
|
|
||||||
return new CodeReturningMessageSource();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@PropertySource("classpath:/switch-messages.properties")
|
|
||||||
protected static class Config {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
protected static class CustomMessageSource {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public MessageSource messageSource() {
|
|
||||||
return new CodeReturningMessageSource();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue