Configure MessageSource if no "messageSource" bean defined

Enable MessageSourceAutoConfiguration OnMissingBeanCondition by name
rather than class since AbstractApplicationContext expects MessageSource
to be defined only with "messageSource" name.

See gh-15212
pull/15385/head
cac03 6 years ago committed by Stephane Nicoll
parent 2ac76b368d
commit 82d99da32a

@ -33,6 +33,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
@ -49,7 +50,7 @@ import org.springframework.util.StringUtils;
* @author Eddú Meléndez * @author Eddú Meléndez
*/ */
@Configuration @Configuration
@ConditionalOnMissingBean(value = MessageSource.class, search = SearchStrategy.CURRENT) @ConditionalOnMissingBean(name = AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME, search = SearchStrategy.CURRENT)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Conditional(ResourceBundleCondition.class) @Conditional(ResourceBundleCondition.class)
@EnableConfigurationProperties @EnableConfigurationProperties

@ -198,22 +198,21 @@ public class MessageSourceAutoConfigurationTests {
.isEqualTo("bar"))); .isEqualTo("bar")));
} }
@Configuration @Test
@PropertySource("classpath:/switch-messages.properties") public void messageSourceDeclaredWithNonStandardNameDoesNotBreakAutoConfig() {
protected static class Config { this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
.withUserConfiguration(ConfigWithNonStandardMessageSourceBeanName.class)
.run((context) -> {
assertThat(context.getMessage("foo", null, Locale.US))
.isEqualTo("bar");
});
} }
@Configuration private static class CodeReturningMessageSource implements MessageSource {
protected static class CustomMessageSource {
@Bean
public MessageSource messageSource() {
return new MessageSource() {
@Override @Override
public String getMessage(String code, Object[] args, public String getMessage(String code, Object[] args, String defaultMessage,
String defaultMessage, Locale locale) { Locale locale) {
return code; return code;
} }
@ -224,12 +223,35 @@ public class MessageSourceAutoConfigurationTests {
} }
@Override @Override
public String getMessage(MessageSourceResolvable resolvable, public String getMessage(MessageSourceResolvable resolvable, Locale locale)
Locale locale) throws NoSuchMessageException { throws NoSuchMessageException {
return resolvable.getCodes()[0]; return resolvable.getCodes()[0];
} }
}; }
@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…
Cancel
Save