Commit 82d99da3 authored by cac03's avatar cac03 Committed by Stephane Nicoll

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
parent 2ac76b36
...@@ -33,6 +33,7 @@ import org.springframework.context.annotation.Bean; ...@@ -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; ...@@ -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,6 +198,48 @@ public class MessageSourceAutoConfigurationTests { ...@@ -198,6 +198,48 @@ public class MessageSourceAutoConfigurationTests {
.isEqualTo("bar"))); .isEqualTo("bar")));
} }
@Test
public void messageSourceDeclaredWithNonStandardNameDoesNotBreakAutoConfig() {
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
.withUserConfiguration(ConfigWithNonStandardMessageSourceBeanName.class)
.run((context) -> {
assertThat(context.getMessage("foo", null, Locale.US))
.isEqualTo("bar");
});
}
private static class CodeReturningMessageSource 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];
}
}
@Configuration
protected static class ConfigWithNonStandardMessageSourceBeanName {
@Bean
public MessageSource codeReturningMessageSource() {
return new CodeReturningMessageSource();
}
}
@Configuration @Configuration
@PropertySource("classpath:/switch-messages.properties") @PropertySource("classpath:/switch-messages.properties")
protected static class Config { protected static class Config {
...@@ -209,27 +251,7 @@ public class MessageSourceAutoConfigurationTests { ...@@ -209,27 +251,7 @@ public class MessageSourceAutoConfigurationTests {
@Bean @Bean
public MessageSource messageSource() { public MessageSource messageSource() {
return new MessageSource() { return new CodeReturningMessageSource();
@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