Not able to access Message Source in Spring Boot 2.1.1.RELEASE Not able to access Message Source in Spring Boot 2.1.1.RELEASE spring spring

Not able to access Message Source in Spring Boot 2.1.1.RELEASE


From Spring docs:

When an ApplicationContext is loaded, it automatically searches for a MessageSource bean defined in the context. The bean must have the name messageSource.

So the ApplicationContext expects MessageSource bean defined only with the messageSource name.

Currently Spring boot's MessageSourceAutoConfiguration is inconsistent with this policy: it does not expose messageSource bean if it finds a bean with MessageSource class.

So solution to your problem is:

Either change method name to messageSource:

@Beanpublic MessageSource messageSource()

Or add name to the bean explicitly:

@Bean("messageSource")public MessageSource resourceBundleMessageSource()

The behaviour in Spring Boot will change in 2.2.x. The MessageSourceAutoConfiguration will expose MessageSource if it won't find messageSource bean by its name rather than class.

See this pull request. (disclaimer: I'm the author of the PR)