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 = ""
)
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.
Recommended Posts