title: How to place @Bean factory method in the bean class itself?
body: <p>I have a class that shall contain data which is de-serialized from a JSON file. This data shall be available in my application, so I want to bind it as bean.</p>
  
  <p>To keep the de-serialization logic and and the data structure together, I wanted to put the <code>@Bean</code> annotated factory method into the data class itself – like this:
  </p>
  
  <pre class="lang-java prettyprint-override"><code>@Configuration
  public class MyData {
  
      // factory method
      @Bean
      public static MyData loadMyData(ResourceLoader resourceLoader) throws IOException {
          try (InputStream input = resourceLoader.getResource("classpath:data.json").getInputStream()) {
              return new ObjectMapper().readValue(input, MyData.class);
          } 
      }
  
      // data structure
      private Map&lt;String, DataDetail&gt; details;
  
      // ...
  }
  </code></pre>
  
  <p>However this fails because <code>@ComponentScan</code> finds two bean definitions now:</p>
  
  <blockquote>
    <p>org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.example.MyData' available: expected single matching bean but found 2: myData,loadMyData</p>
  </blockquote>
  
  <p>I also tried to replace the <code>@Configuration</code> with <code>@Component</code>, but the result is the same.</p>
  
  <p>Am I just missing the right annotation on the class, or is it just not possible to place the <code>@Bean</code> method in the bean class itself?</p>