美文网首页
CommandLineRunner和ApplicationRun

CommandLineRunner和ApplicationRun

作者: 牛马风情 | 来源:发表于2018-04-27 11:13 被阅读0次

Spring boot会在上下问初始化后,调用所有的Runner 。

主要接口

public interface CommandLineRunner {
    void run(String... args) throws Exception;
}
public interface ApplicationRunner {
    void run(ApplicationArguments args) throws Exception;
}

只要参数不同,在使用的时候我们只需根据需求实现任意一个接口即可

package cn.huanuo;

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

@Component
public class MyApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(args);
    }
}

执行顺序,如果实现了多个Runner 可以使用@Order 执行执行顺序,Spring boot 在遍历所有的Runner后会进行排序处理。

测试结果

image.png

相关文章

网友评论

      本文标题:CommandLineRunner和ApplicationRun

      本文链接:https://www.haomeiwen.com/subject/jkrblftx.html