Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using the Spring Boot 2.1.1.RELEASE where in I am not able to access the configured Message Source.

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> 
 </parent>

Folder Structure

@SpringBootApplication
public class RestfulWebservicesApplication {

    //......... some code

    //......... some code
@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver resolver = new SessionLocaleResolver();
    resolver.setDefaultLocale(Locale.US);
    return resolver;
}

 @Bean
 public MessageSource resourceBundleMessageSource() {
    ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource ();
    resourceBundleMessageSource.setBasename("classpath:messages");
    return resourceBundleMessageSource;
}

Above approach is from another SO question

Below is the my restcontroller class method:

@GetMapping("/hello-world-I18N")
public String helloWorldI18N(@RequestHeader(name="Accept-Language",required = false) Locale locale) {
    return messageSource.getMessage("good.morning.message",null,locale);
}

Get Request from POSTMAN: enter image description here

In Debug log auto configuration did not match.

MessageSourceAutoConfiguration: Did not match: - ResourceBundle did not find bundle with basename messages (MessageSourceAutoConfiguration.ResourceBundleCondition)

I did tried the other way where in the configuration have been detected but the error was same as highlighted.

 @Bean
 public MessageSource resourceBundleMessageSource() {
    ReloadableResourceBundleMessageSource resourceBundleMessageSource = new ReloadableResourceBundleMessageSource ();
    resourceBundleMessageSource.setBasename("messages");
    return resourceBundleMessageSource;
}

Other Approach without message folder

MessageSourceAutoConfiguration:
  Did not match:
     - @ConditionalOnMissingBean (types: org.springframework.context.MessageSource; SearchStrategy: current) found beans of type 'org.springframework.context.MessageSource' resourceBundleMessageSource (OnBeanCondition)
  Matched:
     - ResourceBundle found bundle URL [file:/C:/Users/gaura/IdeaProjects/restful-webservices/target/classes/messages.properties] (MessageSourceAutoConfiguration.ResourceBundleCondition)

content of messages.properties: good.morning.message=Good Morning

Does it is the issue with current Spring Boot 2.1.1.RELEASE just like the other version???

like image 505
Gaurav Pathak Avatar asked Dec 29 '18 10:12

Gaurav Pathak


1 Answers

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:

@Bean
public 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)

like image 144
Denis Zavedeev Avatar answered Sep 30 '22 11:09

Denis Zavedeev