Skip to content

Commit 5674a53

Browse files
committed
Merge pull request #15212 from cac03
* pr/15212: Polish "Configure MessageSource if no "messageSource" bean defined" Configure MessageSource if no "messageSource" bean defined
2 parents 2ac76b3 + ec678ea commit 5674a53

File tree

2 files changed

+47
-24
lines changed

2 files changed

+47
-24
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.context.annotation.ConditionContext;
3434
import org.springframework.context.annotation.Conditional;
3535
import org.springframework.context.annotation.Configuration;
36+
import org.springframework.context.support.AbstractApplicationContext;
3637
import org.springframework.context.support.ResourceBundleMessageSource;
3738
import org.springframework.core.Ordered;
3839
import org.springframework.core.io.Resource;
@@ -49,7 +50,7 @@
4950
* @author Eddú Meléndez
5051
*/
5152
@Configuration
52-
@ConditionalOnMissingBean(value = MessageSource.class, search = SearchStrategy.CURRENT)
53+
@ConditionalOnMissingBean(name = AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME, search = SearchStrategy.CURRENT)
5354
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
5455
@Conditional(ResourceBundleCondition.class)
5556
@EnableConfigurationProperties

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public void testUseCodeAsDefaultMessageOn() {
184184

185185
@Test
186186
public void existingMessageSourceIsPreferred() {
187-
this.contextRunner.withUserConfiguration(CustomMessageSource.class)
187+
this.contextRunner.withUserConfiguration(CustomMessageSourceConfiguration.class)
188188
.run((context) -> assertThat(context.getMessage("foo", null, null, null))
189189
.isEqualTo("foo"));
190190
}
@@ -198,38 +198,60 @@ public void existingMessageSourceInParentIsIgnored() {
198198
.isEqualTo("bar")));
199199
}
200200

201+
@Test
202+
public void messageSourceWithNonStandardBeanNameIsIgnored() {
203+
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
204+
.withUserConfiguration(CustomBeanNameMessageSourceConfiguration.class)
205+
.run((context) -> {
206+
assertThat(context.getMessage("foo", null, Locale.US))
207+
.isEqualTo("bar");
208+
});
209+
}
210+
201211
@Configuration
202212
@PropertySource("classpath:/switch-messages.properties")
203213
protected static class Config {
204214

205215
}
206216

207217
@Configuration
208-
protected static class CustomMessageSource {
218+
protected static class CustomMessageSourceConfiguration {
209219

210220
@Bean
211221
public MessageSource messageSource() {
212-
return new MessageSource() {
213-
214-
@Override
215-
public String getMessage(String code, Object[] args,
216-
String defaultMessage, Locale locale) {
217-
return code;
218-
}
219-
220-
@Override
221-
public String getMessage(String code, Object[] args, Locale locale)
222-
throws NoSuchMessageException {
223-
return code;
224-
}
225-
226-
@Override
227-
public String getMessage(MessageSourceResolvable resolvable,
228-
Locale locale) throws NoSuchMessageException {
229-
return resolvable.getCodes()[0];
230-
}
231-
232-
};
222+
return new TestMessageSource();
223+
}
224+
225+
}
226+
227+
@Configuration
228+
protected static class CustomBeanNameMessageSourceConfiguration {
229+
230+
@Bean
231+
public MessageSource codeReturningMessageSource() {
232+
return new TestMessageSource();
233+
}
234+
235+
}
236+
237+
private static class TestMessageSource implements MessageSource {
238+
239+
@Override
240+
public String getMessage(String code, Object[] args, String defaultMessage,
241+
Locale locale) {
242+
return code;
243+
}
244+
245+
@Override
246+
public String getMessage(String code, Object[] args, Locale locale)
247+
throws NoSuchMessageException {
248+
return code;
249+
}
250+
251+
@Override
252+
public String getMessage(MessageSourceResolvable resolvable, Locale locale)
253+
throws NoSuchMessageException {
254+
return resolvable.getCodes()[0];
233255
}
234256

235257
}

0 commit comments

Comments
 (0)