Different ways of loading data into databases at spring application start-up?

Different ways of loading data into databases at spring application start-up?

Spring Boot applications can load data at startup using several methods, including SQL scripts, data import files, or by leveraging Spring’s data initialization features. You can also use Spring Batch, CommandLineRunner, or database migration tools like Flyway or Liquibase.

SQL Scripts

  1. schema.sql and data.sql
    1. Place SQL scripts named schema.sql and data.sql in the src/main/resources directory. Spring Boot will automatically execute schema.sql to create tables and data.sql to insert initial data.
  2. Flyway and Liquibase
    1. These tools manage database schema and data migrations. They allow you to define changes in separate scripts and ensure they are applied only once.
  3. Spring Batch
    1. Spring Batch is designed for batch processing, including data loading. You can define jobs to load data from various sources into the database.

Java-based Initialization

  1. Spring Data JPA: Use Spring Data JPA repositories to save data directly from your application code.

  2. JDBC Template: Use the JdbcTemplate to execute SQL queries and insert data.

  3. CommandLineRunner

    1. Implement the CommandLineRunner interface and define a run() method. This method will be executed after the application context is loaded, allowing you to execute custom logic to load data.
      import com.example.beersapi.entities.Beer;
      import java.util.stream.Stream;
      import org.springframework.boot.CommandLineRunner;
      import org.springframework.stereotype.Component;
      
      @Component
      public class BeerCommandLineRunner implements
             CommandLineRunner {
      
         private final BeerRepository repository;
      
         public BeerCommandLineRunner(BeerRepository repository) {
             this.repository = repository;
         }
      
         @Override
         public void run(String... strings) throws Exception {
             // Top beers from https://www.beeradvocate.com/lists/top/
             Stream.of("Kentucky Brunch Brand Stout",
                     "Good Morning", "Very Hazy", "King Julius",
                     "Budweiser", "Coors Light",
                     "PBR").forEach(name
                             -> repository.save(new Beer(name))
                     );
             repository.findAll().forEach(System.out::println);
         }
      }
      
  4. @PostConstruct

    1. Use the @PostConstruct annotation on a method within a Spring bean. This method will be executed after the bean is initialized.
    package com.example.springbootmultipledatabasespoc.initialize;
    
    import com.example.springbootmultipledatabasespoc.entities.user.User;
    import com.example.springbootmultipledatabasespoc.entities.product.Product;
    import com.example.springbootmultipledatabasespoc.repositories.user.UserRepository;
    import com.example.springbootmultipledatabasespoc.repositories.product.ProductRepository;
    import lombok.RequiredArgsConstructor;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.PostConstruct;
    
    @Component
    @RequiredArgsConstructor
    public class InitData {
    
        private final UserRepository userRepository;
    
        private final ProductRepository productRepository;
    
        @PostConstruct
        public void load() {
    
         User user = new User();
         user.setName("John");
         user.setEmail("john@test.com");
         user.setAge(20);
         user = userRepository.save(user);
    
         Product product = new Product();
         product.setName("Book");
         product.setId(2);
         product.setPrice(20);
         product = productRepository.save(product);
        }
    
    }
    
  5. Data Import Files

    1. CSV, JSON, XML: You can load data from files in these formats using various Spring components or custom logic within your initialization methods.

Database Migration Tools

  1. Flyway
    1. Define migration scripts in SQL and place them in the src/main/resources/db/migration directory.
    2. Flyway will automatically apply the migrations.
  2. Liquibase
    1. Similar to Flyway, Liquibase uses changelogs to manage database schema and data migrations

Links to this note