CommandLineRunner、ApplicationRunner 接口

       如果我们想在项目启动后做一些事情(如加载定时任务,初始化工作),可以使用spring提供的CommandLineRunner、ApplicationRunner 接口,在容器启动成功后的最后一步回调(类似开机自启动)。

1. CommandLineRunner接口

/**
 * Interface used to indicate that a bean should <em>run</em> when it is contained within
 * a {@link SpringApplication}. Multiple {@link CommandLineRunner} beans can be defined
 * within the same application context and can be ordered using the {@link Ordered}
 * interface or {@link Order @Order} annotation.
 * <p>
 * If you need access to {@link ApplicationArguments} instead of the raw String array
 * consider using {@link ApplicationRunner}.
 *
 * @author Dave Syer
 * @see ApplicationRunner
 */
public interface CommandLineRunner {

	/**
	 * Callback used to run the bean.
	 * @param args incoming main method arguments
	 * @throws Exception on error
	 */
	void run(String... args) throws Exception;

}

多个CommandLineRunner可以被同时执行在同一个spring上下文中并且执行顺序是以order注解的参数顺序一致。

下面看一个demo:

@Order(2)
@Component
public class ServerStartedReport implements CommandLineRunner{
    @Override
    public void run(String... args) throws Exception {
    
        System.out.println("===========ServerStartedReport启动====="+ LocalDateTime.now());
    }
}

配置参数启动项目:
在这里插入图片描述
控制台打印:

[ddd,tyy]
===========ServerStartedReport启动=====2019-02-14T21:31:30.466

2. ApplicationRunner接口

       二者基本一样,区别在于接收的参数不一样。CommandLineRunner的参数是最原始的参数,没有做任何处理,而ApplicationRunner的参数是ApplicationArguments,对原始参数做了进一步的封装。
如我们在这里配置了一些启动参数--foo=hu --log=debug
在这里插入图片描述
CommandLineRunner只是获取--name=value。而ApplicationRunner可以解析--name=value,使得我们可以直接通过name来获取value。

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class MyApplicationRunner implements ApplicationRunner{

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("ApplicationRunner:"+ Arrays.asList(args.getSourceArgs()));
        System.out.println("getOptionNames:"+args.getOptionNames());
        System.out.println("getOptionValues:"+args.getOptionValues("foo"));
        System.out.println("getOptionValues:"+args.getOptionValues("log"));
    }
}

打印结果为:

===========ServerStartedReport启动=====2019-02-14T21:36:23.668
ApplicationRunner:[--foo=hu, --log=debug]
getOptionNames:[log, foo]
getOptionValues:[hu]
getOptionValues:[debug]

注意启动后执行的方法一定要加try catch,因为此处抛出异常会影响项目启动。

posted on 2020-01-11 11:19  spiritt  阅读(161)  评论(0编辑  收藏  举报