在我们实际工作中,会遇到这样需求,在项目启动的时候需要做一些初始化的操作,比如初始化数据库连接、Redis缓存等等。今天就给大家介绍 CommandLineRunner接口,帮助大家解决项目启动初始化资源操作。
CommandLineRunner 接口的 Component 会在所有 Spring Beans 都初始化之后,SpringApplication.run() 之前执行,非常适合在应用程序启动之初进行一些数据初始化的工作。
一、CommandLineRunner 接口
Spring boot的CommandLineRunner接口主要用于实现在应用初始化后,去执行一段代码块逻辑,这段初始化代码在整个应用生命周期内只会执行一次。
1. 编写一个简单的Spring Boot应用
创建一个Spring Boot项目,实现一个简单的Spring Boot应用测试一下。在应用启动类中,添加2行控制台输出提示,方便了解CommandLineRunner的执行时间点。
应用启动类CommandLineRunnerApp:
package com.rickie.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class CommandLineRunnerApp { public static void main( String[] args ) { System.out.println("开始启动服务 ..."); SpringApplication.run(CommandLineRunnerApp.class, args); System.out.println("服务启动完成 ..."); } }
2. 创建类实现CommandLineRunner接口
接下来我们直接创建一个类Initializer 实现 CommandLineRunner 接口,并实现它的 run() 方法。
package com.rickie.springboot;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class Initializer implements CommandLineRunner {
@ Override
public void run(String... args) throws Exception {
System.out.println("开始初始化 ...");
}
}
3. 启动应用
启动应用,查看Console控制台输出信息。
如下图所示,重点看看其中标识1、2、3的地方。我们发现initializer.run() 方法在SpringApplication.run() 之前执行了,并输出了“开始初始化 …”的信息。
二、如何使用CommandLineRunner接口?
我们可以用以下三种方式去使用CommandLineRunner接口:
1. 和@Component注解一起使用
就是上面示例代码。
2. 和@SpringBootApplication注解一起使用
更新应用启动类代码,如下所示:
package com.rickie.springboot; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class CommandLineRunnerApp implements CommandLineRunner { public static void main( String[] args ) { System.out.println("开始启动服务 ..."); SpringApplication.run(CommandLineRunnerApp.class, args); System.out.println("服务启动完成 ..."); } @Override public void run(String... args) throws Exception { System.out.println("开始初始化@SpringBootApplication ... "); } }
启动应用,Console控制台输出结果:
3. 声明一个实现了CommandLineRunner接口的 Bean
这种方式和第一种方式比较类似,就是在SpringBootApplication里定义一个Bean,该Bean实现了CommandLineRunner接口。
package com.rickie.springboot; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication public class CommandLineRunnerApp // implements CommandLineRunner { public static void main( String[] args ) { System.out.println("开始启动服务 ..."); SpringApplication.run(CommandLineRunnerApp.class, args); System.out.println("服务启动完成 ..."); } // @Override // public void run(String... args) throws Exception { // System.out.println("开始初始化@SpringBootApplication ... "); // } @Bean public Initializer initializer() { return new Initializer(); } }
记得将之前创建的Initializer类的注解@Component 注释掉,作为一个普通的Java类。
三、run()方法中出现异常会导致应用启动终止
在实现CommandLineRunner接口时,run(String… args)方法内部如果抛异常的话,会直接导致应用启动失败,所以,一定要记得将危险的代码放在try-catch代码块里。
如下所示,模拟run() 方法出现异常,会导致spring boot应用启动终止。
四、@Order注解来标识执行顺序
一个应用可能存在多个CommandLineRunner接口实现类,如果我们想设置它们的执行顺序,可以使用 @Order实现。
用@Order注解去设置多个CommandLineRunner实现类的执行顺序。
启动应用,查看console 输出结果: