Execution of initial processing using Spring Boot Command Line Runner

Creation of initialization process

In Spring Boot, after all beans are registered in the DI container, the beans that implement the CommandLineRunner interface are executed. By using this, it is possible to implement initial processing such as registering data in the DB at startup. The method is simple, just implement the CommandLineRunner interface and register the class that describes the process you want to execute in the run method in the DI container. Here, @Component is used as an example. In the example below, when started in the development environment (Profile = "dev"), the repository class is used and the initial data is saved in the DB.

@Component
@Profile(value = ["dev"]) //Execute for development environment profile
class DevDataInitializer(@Autowired private val greetingRepository: GreetingRepository) : CommandLineRunner {

    private val logger = LoggerFactory.getLogger(javaClass)

    override fun run(vararg args: String?) {

        logger.info("${this.javaClass.simpleName} Run")
        greetingRepository.save(Greeting(value = "Dev!!!"))
    }
}

The following class is an example, so there is no particular meaning in the process. Repository class

import com.example.springsecuritydemo.domain.Greeting
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository

@Repository
interface GreetingRepository: JpaRepository<Greeting, Long>

Entity class

import org.springframework.data.jpa.domain.support.AuditingEntityListener
import javax.persistence.*

@EntityListeners(AuditingEntityListener::class)
@Entity
@Table(name = "greeting")
data class Greeting(
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        var id: Long = 0,

        @Column(name = "value")
        val value: String = ""
)

Specifying the execution order

When registering classes that implement multiple CommandLineRunner in the DI container, the execution order of each class can be specified by @Order. Specify the execution order with the argument of @Order as shown below.

import com.example.springsecuritydemo.domain.Greeting
import com.example.springsecuritydemo.repository.GreetingRepository
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.CommandLineRunner
import org.springframework.context.annotation.Profile
import org.springframework.core.annotation.Order
import org.springframework.stereotype.Component

@Component
@Order(1)
@Profile(value = ["dev"])
class DevDataInitializer(@Autowired private val greetingRepository: GreetingRepository) : CommandLineRunner {

    private val logger = LoggerFactory.getLogger(javaClass)

    override fun run(vararg args: String?) {

        logger.info("${this.javaClass.simpleName} Run")
        greetingRepository.save(Greeting(value = "Dev!!!"))
    }
}
import com.example.springsecuritydemo.domain.Greeting
import com.example.springsecuritydemo.repository.GreetingRepository
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.CommandLineRunner
import org.springframework.context.annotation.Profile
import org.springframework.core.annotation.Order
import org.springframework.stereotype.Component

@Component
@Order(2)
@Profile(value = ["dev"])
class DevDataInitializer2(@Autowired private val greetingRepository: GreetingRepository): CommandLineRunner {

    private val logger = LoggerFactory.getLogger(javaClass)

    override fun run(vararg args: String?) {


        logger.info("${this.javaClass.simpleName} Run")
        greetingRepository.save(Greeting(value = "Dev2!!!"))
    }
}

If you look at the log at startup, you can see that they are executed in the specified order. pic2.png

Recommended Posts

Execution of initial processing using Spring Boot Command Line Runner
Unknown error in line 1 of pom.xml when using Spring Boot in Eclipse
Asynchronous processing with Spring Boot using @Async
Workaround for Command Line Runner to work with JUnit in Spring Boot
Asynchronous processing with regular execution in Spring Boot
[FCM] Implementation of message transmission using FCM + Spring boot
[Java / Spring Boot] Spring security ⑤ --Implementation of logout processing
I checked asynchronous execution of queries in Spring Boot 1.5.9
Try using Spring Boot Security
Memorandum of understanding when Spring Boot 1.5.10 → Spring Boot 2.0.0
Spring Boot Tutorial Using Spring Security Authentication
Implement declarative retry processing using Spring Retry
Going out of message (Spring boot)
[Spring Boot] Role of each class
[Java] LINE integration with Spring Boot
[Kotlin] Example of processing using Enum
Processing at application startup with Spring Boot
Try using Spring Boot with VS Code
About the initial display of Spring Framework
WebMvcConfigurer Memorandum of Understanding for Spring Boot 2.0 (Spring 5)
Spring Boot, Doma2, Gradle initial setting summary