Different ways of loading data into databases at spring application start-up?
Table of Contents
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
schema.sql
anddata.sql
- Place SQL scripts named
schema.sql
anddata.sql
in thesrc/main/resources
directory. Spring Boot will automatically executeschema.sql
to create tables anddata.sql
to insert initial data.
- Place SQL scripts named
- Flyway and Liquibase
- These tools manage database schema and data migrations. They allow you to define changes in separate scripts and ensure they are applied only once.
- Spring Batch
- 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
-
Spring Data JPA: Use Spring Data JPA repositories to save data directly from your application code.
-
JDBC Template: Use the JdbcTemplate to execute SQL queries and insert data.
-
CommandLineRunner
- Implement the
CommandLineRunner
interface and define arun()
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); } }
- Implement the
-
@PostConstruct
- 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); } }
- Use the
-
Data Import Files
- 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
- Flyway
- Define migration scripts in SQL and place them in the
src/main/resources/db/migration
directory. - Flyway will automatically apply the migrations.
- Define migration scripts in SQL and place them in the
- Liquibase
- Similar to Flyway, Liquibase uses changelogs to manage database schema and data migrations