Get the App
SLTechnology News&Howtos  ›  Servers  › 

What is CommandLineRunner like in SpringBoot?

Shulou Source: shulou.com Published: 2022-06-01 07:10:11 10月06日 Update

SpringBoot CommandLineRunner is how, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

The role of CommandLineRunner in SpringBoot

In ordinary development, you may need to implement the functions that are executed after the start of the project. A simple implementation scheme provided by SpringBoot is to add a model and implement the CommandLineRunner interface, and the code to implement the function is placed in the run method of the implementation.

That is, the action that needs to be performed as soon as the project is started.

We only need a simple configuration in the project to achieve this function.

Simple example package org.springboot.sample.runner

Import org.springframework.boot.CommandLineRunner

Import org.springframework.stereotype.Component

@ Component

Public class MyStartupRunner implements CommandLineRunner {

@ Override

Public void run (String... Args) throws Exception {

System.out.println ("Project already started")

}

}

If there are multiple classes that implement the CommandLineRunner interface, how to ensure the order

SpringBoot traverses all entity classes that implement CommandLineRunner and executes run methods after the project starts. If you need to execute them in a certain order, you need to use a @ Order annotation on the entity class (or implement the Order interface) to indicate the order.

Package org.springboot.sample.runner

Import org.springframework.boot.CommandLineRunner

Import org.springframework.core.annotation.Order

Import org.springframework.stereotype.Component

@ Component

@ Order (value=2)

Public class MyStartupRunner1 implements CommandLineRunner {

@ Override

Public void run (String... Args) throws Exception {

System.out.println ("Executive 2")

}

}

Package org.springboot.sample.runner

Import org.springframework.boot.CommandLineRunner

Import org.springframework.core.annotation.Order

Import org.springframework.stereotype.Component

@ Component

@ Order (value=1)

Public class MyStartupRunner2 implements CommandLineRunner {

@ Override

Public void run (String... Args) throws Exception {

System.out.println ("Executive 1")

}

}

Console display

Execute 1

Executive 2

According to the results of the console, the priority of @ Order annotation is in the order of value value.

@ Order effect

That is, after the project is started, there are more actions to be performed, so which one should be executed first, then you can use this annotation to define the priority.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Project sequence function interface annotation priority action entity that is console method help control normal clear from small to big that is from small to big code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Redmi Shulou Tech Info MariaDB NVidia