title: Spring boot application and MessageSource
body: <p>I have been trying to create a demo spring boot app with MessageSource, but i couldn't figure out what is the problem. I tried in couple of ways:</p>
  
  <ul>
  <li>MessageSourceAutoConfiguration</li>
  <li>Creating my own bean within a @configuration file and autowiring it</li>
  </ul>
  
  <p>Below is the <strong>MessageSourceAutoConfiguration</strong> way:</p>
  
  <p>I am using spring-boot-starter-parent <strong>1.5.7.RELEASE</strong>.</p>
  
  <p><strong>My folder structure:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/jH8rd.png" rel="noreferrer"><img src="https://i.stack.imgur.com/jH8rd.png" alt="enter image description here"></a></p>
  
  <p><strong>DemoApplication.java</strong></p>
  
  <pre><code>@SpringBootApplication
  public class DemoApplication
  {
    public static void main(String[] args)
    {
      SpringApplication.run(DemoApplication.class, args);
    }
  }
  </code></pre>
  
  <p><strong>DemoController.java</strong></p>
  
  <pre><code>@RestController
  public class DemoController implements MessageSourceAware
  {
    private MessageSource messageSource;
  
    @Override
    public void setMessageSource(MessageSource messageSource)
    {
      this.messageSource = messageSource;
    }
  
    @RequestMapping("demo")
    public String getLocalisedText()
    {
      return messageSource.getMessage("test", new Object[0], new Locale("el"));
    }
  }
  </code></pre>
  
  <p><strong>Application.yml</strong></p>
  
  <pre><code>spring:
    messages:
      basename: messages
  </code></pre>
  
  <p><strong>messages.properties</strong></p>
  
  <pre><code>test=This is a demo app!
  </code></pre>
  
  <p><strong>messages_el.properties</strong></p>
  
  <pre><code>test=This is a greek demo app!
  </code></pre>
  
  <p><strong>pom.xml</strong></p>
  
  <pre><code>&lt;groupId&gt;com.example.i18n.demo&lt;/groupId&gt;
  &lt;artifactId&gt;demo&lt;/artifactId&gt;
  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  &lt;packaging&gt;jar&lt;/packaging&gt;
  
  &lt;name&gt;demo&lt;/name&gt;
  &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
  
  &lt;parent&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
      &lt;version&gt;1.5.7.RELEASE&lt;/version&gt;
      &lt;relativePath /&gt;
      &lt;!-- lookup parent from repository --&gt;
  &lt;/parent&gt;
  
  &lt;properties&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;project.reporting.outputEncoding&gt;UTF-
      8&lt;/project.reporting.outputEncoding&gt;
      &lt;java.version&gt;1.8&lt;/java.version&gt;
  &lt;/properties&gt;
  
  &lt;dependencies&gt;      
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  </code></pre>
  
  <p><strong>While starting up the server, i can see that the AutoConfiguration works, but cannot find a bean:</strong></p>
  
  <pre><code>MessageSourceAutoConfiguration matched:
        - ResourceBundle found bundle URL [file:/C:/Users/{user}/Downloads/demo/demo/target/classes/messages.properties] (MessageSourceAutoConfiguration.ResourceBundleCondition)
        - @ConditionalOnMissingBean (types: org.springframework.context.MessageSource; SearchStrategy: current) did not find any beans (OnBeanCondition)
  </code></pre>
  
  <p>I tried to also explicitly declare my own bean, but didn't work.</p>
  
  <p>While invoking the endpoint i get the following error:</p>
  
  <pre><code>{
      "status": 500,
      "error": "Internal Server Error",
      "exception": "org.springframework.context.NoSuchMessageException",
      "message": "No message found under code 'test' for locale 'el'.",
      "path": "/demo"
  }
  </code></pre>
  
  <p>The closest SO question i found was this one (2 years old) about a bug in spring which was fixed couple of versions ago:
  <a href="https://stackoverflow.com/questions/30663513/spring-boot-messagesourceautoconfiguration">Spring Boot MessageSourceAutoConfiguration</a></p>