title: Spring boot preloading data from database in bean
body: <p>I'm asking for elements that have a recursive relationship in one table:</p>
  
  <p><strong>child id | parent id</strong></p>
  
  <p>1 | null</p>
  
  <p>2 | 1</p>
  
  <p>3 | 1</p>
  
  <p>4 | 2</p>
  
  <p>And so on...To ask for this whole datastructure it takes about 10 secondsAt the moment I can't redesign this table because it cost too much time (for more information: <a href="https://stackoverflow.com/questions/35372916/spring-repository-performance-issues-with-recursive-orm-class">Spring Repository performance issues with recursive ORM Class</a>)</p>
  
  <p>Now, I am thinking about preloading all data during spring startup in a bean, so that the client "communicates" with the bean and I update the data in bean and database. I think it doesn't matter how long this startup takes, but it matters how long the user has to wait for its answer.</p>
  
  <p><strong>So far, I didn't manage it to preload it. I was trying to create a bean like this:</strong></p>
  
  <pre><code>public class AppConfig extends WebMvcConfigurerAdapter {
  ...
  @Autowired
  SkillDAO skillDAO
  ...
  @Bean(name="allSkills")
  public List&lt;Skill&gt; allSkills(){
      return skillDAO.findBySkill(null);
  }
  ...
  </code></pre>
  
  <p>It doesn't work cause I get an error:</p>
  
  <blockquote>
    <p>Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'dataSource': Requested bean is currently in creation: Is there an unresolvable circular reference?</p>
  </blockquote>
  
  <p>By the way, I create all my beans in AppConfig. When I delete this "allSkills" bean, it works again.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <pre><code>@Component
  public class MyListener {
  
  @Autowired
  private SkillDAO skillDAO;
  
  @EventListener
  public void onApplicationReady(ApplicationReadyEvent ready) {
      System.out.println("++++++++++++++ HALLO +++++++++++++");
      skillDAO.findBySkill(null);
  }
  }
  </code></pre>
  
  <p>...</p>
  
  <pre><code>@Bean(name="skillBean")
  public MyListener myListener() {
      return new MyListener();
  }
  </code></pre>