title: Spring Boot - Loading Initial Data
body: <p>I'm wondering what the best way to load initial database data before the application starts? What I'm looking for is something that will fill my H2 database with data.</p>
  
  <p>For example, I have a domain model "User" I can access users by going to /users but initially there won't be any users in the database so I have to create them. Is there anyway to fill the database with data automatically?</p>
  
  <p>At the moment I have a Bean that gets instantiated by the container and creates users for me.</p>
  
  <p>Example:</p>
  
  <pre><code>@Component
  public class DataLoader {
  
      private UserRepository userRepository;
  
      @Autowired
      public DataLoader(UserRepository userRepository) {
          this.userRepository = userRepository;
          LoadUsers();
      }
  
      private void LoadUsers() {
          userRepository.save(new User("lala", "lala", "lala"));
      }
  }
  </code></pre>
  
  <p>But I very much doubt that is the best way of doing it. Or is it?</p>