From 82d99da32a7523f390382ae1bc5de73910bd577f Mon Sep 17 00:00:00 2001 From: cac03 Date: Sun, 18 Nov 2018 23:49:26 +0300 Subject: [PATCH 1/2] 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 --- .../MessageSourceAutoConfiguration.java | 3 +- .../MessageSourceAutoConfigurationTests.java | 64 +++++++++++++------ 2 files changed, 45 insertions(+), 22 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfiguration.java index ec87833c6f..9c1b4d8ade 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfiguration.java @@ -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 diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java index 72a0c9a560..93a088e6bc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java @@ -198,6 +198,48 @@ public class MessageSourceAutoConfigurationTests { .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 @PropertySource("classpath:/switch-messages.properties") protected static class Config { @@ -209,27 +251,7 @@ public class MessageSourceAutoConfigurationTests { @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 CodeReturningMessageSource(); } } From ec678eaa3b74d64c28bdcaba6ef5e011033678b6 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 3 Dec 2018 11:28:26 +0100 Subject: [PATCH 2/2] Polish "Configure MessageSource if no "messageSource" bean defined" Closes gh-15212 --- .../MessageSourceAutoConfigurationTests.java | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java index 93a088e6bc..5683d5f2e6 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java @@ -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")); } @@ -199,16 +199,42 @@ public class MessageSourceAutoConfigurationTests { } @Test - public void messageSourceDeclaredWithNonStandardNameDoesNotBreakAutoConfig() { + public void messageSourceWithNonStandardBeanNameIsIgnored() { this.contextRunner.withPropertyValues("spring.messages.basename:test/messages") - .withUserConfiguration(ConfigWithNonStandardMessageSourceBeanName.class) + .withUserConfiguration(CustomBeanNameMessageSourceConfiguration.class) .run((context) -> { assertThat(context.getMessage("foo", null, Locale.US)) .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 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(); - } - - } - }