【推荐】Spring Boot 程序初始化实战: CommandLineRunner 和 ApplicationRunner

我们在开发过程中会有这样的场景:需要在容器启动的时候执行一些内容,比如:读取配置文件信息,数据库连接,删除临时文件,清除缓存信息。在Spring Boot中给我们提供了两个接口来帮助我们实现这样的需求。这两个接口就是我们今天要讲的

  1. CommandLineRunner
  2. ApplicationRunner,

他们的执行时机为容器启动完成的时候。使用起来非常简单。只需要实现CommandLineRunner或者ApplicationRunner接口。

如果是Spring 环境下,则可以参看本博的下面的文章讲如何在Spring环境做初始化操作的。

https://blog.csdn.net/bigtree_3721/article/details/103735767

如果在SpringBoot应用启动的时候需要执行特定的动作,可以利用CommandLineRunner或者ApplicationRunner。实现了CommandLineRunner或者ApplicationRunner接口的Component会在 所有Spring Beans都初始化之后,SpringApplication.run()之前执行,非常适合在应用程序启动之初进行一些数据初始化的工作。

共同点:

其一 执行时机都是在容器启动完成的时候进行执行;其二 这两个接口中都有一个run()方法;

如果程序里既有ApplicationRunner ,也有CommandLineRunner代码 ,

则ApplicationRunner 先运行,而CommandLineRunner 后运行。

不同点:

ApplicationRunner中run方法的参数为ApplicationArguments,

而CommandLineRunner接口中run方法的参数为String数组。

问题提出: 如果有多个实现类,而我们需要按照一定的顺序执行的话,那么应该怎么办呢?

解决方案:

方法1)可以在实现类上加上@Order注解指定执行的顺序;

方法2)可以在实现类上实现Ordered接口来标识。

需要注意:数字越小,优先级越高,也就是@Order(1)注解的类会在@Order(2)注解的类之前执行。

 

下面展示了如何在程序中使用CommandLineRunnerApplicationRunner

Maven配置

pom.xml

<?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>com.concretepage</groupId>
    <artifactId>spring-boot-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>spring-demo</name>
    <description>Spring Boot Demo Project</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <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> 

CommandLineRunner

CommandLineRunner是个接口,有一个run()方法。为了使用CommandLineRunner我们需要创建一个类实现该接口并覆盖run()方法。使用@Component注解实现类。当SpringApplication.run()启动spring boot程序时,启动完成之前,CommandLineRunner.run()会被执行。CommandLineRunnerrun()方法接收启动服务时传过来的参数。

run(String... args)

参数为String数组。

CommandLineRunnerBean.java

package com.concretepage.bean;

import java.util.Arrays;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class CommandLineRunnerBean implements CommandLineRunner {
    private static final Logger logger = LoggerFactory.getLogger(CommandLineRunnerBean.class);  
    public void run(String... args) {
        String strArgs = Arrays.stream(args).collect(Collectors.joining("|"));
        logger.info("Application started with arguments:" + strArgs);
    }
} 

MyApplication.java

package com.concretepage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import com.concretepage.service.HelloService;

@SpringBootApplication
public class MyApplication {
    private static final Logger logger = LoggerFactory.getLogger(MyApplication.class);
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MyApplication.class, args);
        HelloService service =  context.getBean(HelloService.class);
        logger.info(service.getMessage());
        }       
} 

我们也可以创建一个service。一旦Spring boot启动完成service就会执行。

这意味着SpringApplication.run()执行完成后service的方法就会执行。
HelloService.java

package com.concretepage.service;

import org.springframework.stereotype.Service;

@Service
public class HelloService {
    public String getMessage(){
        return "Hello World!";
    }
}

现在使用带有参数的可执行jar运行程序。spring-boot-demo-0.0.1-SNAPSHOT.jar为生成的jar文件。执行命令如下:

java -jar spring-boot-demo-0.0.1-SNAPSHOT.jar data1 data2 data3 

输出结果为:

2017-03-19 13:38:38.909  INFO 1036 --- [main] c.c.bean.CommandLineRunnerBean  : Application started with arguments:data1|data2|data3
2017-03-19 13:38:38.914  INFO 1036 --- [main] com.concretepage.MyApplication  : Started MyApplication in 1.398 seconds (JVM running for 1.82)
2017-03-19 13:38:38.915  INFO 1036 --- [main] com.concretepage.MyApplication  : Hello World! 

ApplicationRunner

ApplicationRunnerCommandLineRunner的作用相同。在SpringApplication.run()完成spring boot启动之前,ApplicationRunnerrun()方法会被执行。

run(ApplicationArguments args) 

可以看到,CommandLineRunner.run()接收String数组作为参数,而ApplicationRunner.run()接收ApplicationArguments作为参数。这些参数是启动spring boot程序时传给main()方法的。

ApplicationRunnerBean.java

package com.concretepage.bean;

import java.util.Arrays;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class ApplicationRunnerBean implements ApplicationRunner {
    private static final Logger logger = LoggerFactory.getLogger(ApplicationRunnerBean.class);  
    @Override
    public void run(ApplicationArguments arg0) throws Exception {
            String strArgs = Arrays.stream(arg0.getSourceArgs()).collect(Collectors.joining("|"));
            logger.info("Application started with arguments:" + strArgs);
    }
} 

创建可执行jar包并使用如下参数运行:

java -jar spring-boot-demo-0.0.1-SNAPSHOT.jar data1 data2 data3 

输出结果为:

2017-03-19 16:26:06.952  INFO 5004 --- [           main] c.c.bean.ApplicationRunnerBean           : Application started with arguments:data1|data2|data3
2017-03-19 16:26:06.956  INFO 5004 --- [           main] com.concretepage.MyApplication           : Started MyApplication in 1.334 seconds (JVM running for 1.797)
2017-03-19 16:26:06.957  INFO 5004 --- [           main] com.concretepage.MyApplication           : Hello World! 

CommandLineRunner和ApplicationRunner的执行顺序

在spring boot程序中,我们可以使用不止一个实现CommandLineRunnerApplicationRunner的bean。为了有序执行这些bean的run()方法,可以使用@Order注解或Ordered接口。例子中我们创建了两个实现CommandLineRunner接口的bean和两个实现ApplicationRunner接口的bean。我们使用@Order注解按顺序执行这四个bean。

CommandLineRunnerBean1.java

@Component
@Order(1)
public class CommandLineRunnerBean1 implements CommandLineRunner {
    public void run(String... args) {
        System.out.println("CommandLineRunnerBean 1");
    }
}

ApplicationRunnerBean1.java

@Component
@Order(2)
public class ApplicationRunnerBean1 implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments arg0) throws Exception {
        System.out.println("ApplicationRunnerBean 1");
    }
}

CommandLineRunnerBean2.java

@Component
@Order(3)
public class CommandLineRunnerBean2 implements CommandLineRunner {
    public void run(String... args) {
        System.out.println("CommandLineRunnerBean 2");
    }
}

ApplicationRunnerBean2.java

@Component
@Order(4)
public class ApplicationRunnerBean2 implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments arg0) throws Exception {
        System.out.println("ApplicationRunnerBean 2");
    }
} 

输出结果为:

CommandLineRunnerBean 1
ApplicationRunnerBean 1
CommandLineRunnerBean 2
ApplicationRunnerBean 2 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值