SpringBoot学习记录----如何启动时执行任务(CommandLineRunner和ApplicationRunner)

SpringBoot提供了CommandLineRunner和ApplicationRunner接口,让我们可以在启动项目时自动运行某些特定代码。例如一些数据的初始化,或者提前获取一些第三方接口的token。这两个接口都需要实现run方法,并以相同的方式工作。这个run方法在SpringApplication.run()完成之前调用。

1、准备工作

首先,创建一个maven项目,如何创建maven项目请参考:https://blog.csdn.net/chenzz2560/article/details/83270232。在创建完maven项目后,在pom.xml中导入SpringBoot运行需要的依赖包。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>czz.study</groupId>
    <artifactId>springboot-runner</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>springboot-event</name>
    <description>SpringBoot Runner学习代码</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、创建要运行的程序

创建一个class类,里面包含了你要启动运行的方法。@Component 注解将这个类声明为一个Bean对象,交给Spring进行管理。实现CommandLineRunner接口,实现接口中的run方法。这样,只要程序一运行,就会自动运行run中的代码。

2.1创建两个实现CommandLineRunner接口方法

package czz.study.springbootrunner.runner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 通过实现CommandLineRunner接口运行初始化代码1
 *
 * @author chenzhengzhou
 * @version 1.0
 * @date 2019/4/19 14:22
 */
@Component
@Order(value = 1)
public class CommandLineRunnerTest implements CommandLineRunner {

    @Override
    public void run(String... args) {
        System.out.println("CommandLineRunnerTest初始化代码1运行");
    }
}
package czz.study.springbootrunner.runner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 通过实现CommandLineRunner接口运行初始化代码2
 *
 * @author chenzhengzhou
 * @version 1.0
 * @date 2019/4/19 14:22
 */
@Component
@Order(value = 2)
public class CommandLineRunnerTwoTest implements CommandLineRunner {

    @Override
    public void run(String... args) {
        System.out.println("CommandLineRunnerTwoTest初始化代码2运行");
    }

}

 

2.2创建两个实现ApplicationRunner接口方法

package czz.study.springbootrunner.runner;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 通过实现ApplicationRunner接口运行初始化代码1
 *
 * @author chenzhengzhou
 * @version 1.0
 * @date 2019/4/19 14:22
 */
@Component
@Order(value = 1)
public class ApplicationRunnerTest implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) {
        System.out.println("ApplicationRunnerTest初始化代码1运行");
    }
}
package czz.study.springbootrunner.runner;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
 * 通过实现ApplicationRunner接口运行初始化代码2
 *
 * @author chenzhengzhou
 * @version 1.0
 * @date 2019/4/19 14:22
 */
@Component
@Order(value = 2)
public class ApplicationRunnerTwoTest implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) {
        System.out.println("ApplicationRunnerTwoTest初始化代码2运行");
    }
}

 

3、运行程序

创建一个启动类,@SpringBootApplication 功能相当于@Configuration,@EnableAutoConfiguration,@ComponentScan这三个注解,SpringApplication.run(Main.class, args)这行代码用来声明项目入口,没有这行代码,使用的Spring注解都会失效(等于没有注解)。

package czz.study.springbootrunner;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootRunnerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootRunnerApplication.class, args);
    }
}

4、运行结果

一个项目中可以有多个实现CommandLineRunner和ApplicationRunner接口的程序,Spring一个个执行它们,我们可以通过@Order注解来指定运行顺序,value值越小优先度越高。当优先度相同时,实现ApplicationRunner的方法会优先于实现CommandLineRunner接口的方法运行。

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值