Springboot perform a task at startup Commandlinerunner

Source: Internet
Author: User

# The role of Commandlinerunner in Springboot
> in the normal development may need to implement after the project launch function, Springboot provides a simple implementation is to add a model and implement the Commandlinerunner interface, implement the function of the code in the implementation of the Run method
# Simple Example
"' Java
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 (">>>>>>>>>>>>>>> services start execution, perform loading data, and so on <<< <<<<<<<<<< ");
}

}
```
# If there are multiple classes implementing the Commandlinerunner interface, how to guarantee the order
> Springboot will traverse all entity classes that implement Commandlinerunner after the project is started and execute the Run method, if necessary in a certain order. Then 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 (">>>>>>>>>>>>>>> service start execution 2222 <<<<< <<<<<<<< ");
}

}
```
```
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 (">>>>>>>>>>>>>>> service start execution 111111 <<<< <<<<<<<<< ");
}

}
```
> Console Display
```
>>>>>>>>>>>>>>> services start Execution 11111111 <<<<<<<<< <<<<
>>>>>>>>>>>>>>> service Start Execution 22222222## Title # # <<<<<<< <<<<<<
```
> According to the console results, the execution priority of @Order annotations is from small to large in order of value values.

Springboot perform a task at startup Commandlinerunner

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.