springboot 02 -ApplicationRunner,CommandLineRunner

    在实际的业务场景中,有时候我们需要在服务启动的时候,执行一些初始化的工作,比如读取配置文件信息,向注册中心注册服务等。springboot提供了ApplicationRunner,CommandLineRunner这两个接口来帮助我们执行初始化工作。当存在多个接口的时候,用@Order(number)  表示执行顺序,数字越小优先级越高。

@Component
@Order(2)
public class MyCommandLinerRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
System.out.println(args[0]); //--app.name=apple System.
out.println("MyCommandLinerRunner......."); } } @Component @Order(1) public class MyCommandLinerRunner1 implements CommandLineRunner { @Override public void run(String... args) throws Exception {
     System.out.printin(args[0]); //--app.name=apple System.
out.println("MyCommandLinerRunner1......."); } } @Component @Order(3) public class MyApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println(args.getOptionNames()); //[app.name] System.out.println(args.getOptionValues("app.name")); //[apple] System.out.println(args.getSourceArgs()); } } /** * Created by IntelliJ IDEA. * User: chenzhubing * Date: 2019/7/25 * <p> * 1.CommandLineRunner : 应用初始化完成后执行 * 2.CommandLineRunner实现类只要注册到spring中,即可触发run方法 * 3.多个CommandLineRunner存在时,通过@Order 标注先后顺序 */ @SpringBootApplication(scanBasePackages = "com.example.commandLineRunner") public class SpringbootApplication_runner { public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(SpringbootApplication_runner.class); ConfigurableApplicationContext context = springApplication.run(args); context.close(); } }

 

 

 

ApplicationRunner和CommandLineRunner两个接口的区别在于run方法的参数不同:

   ApplicationRunner--run:ApplicationArguments,除了可以获取原生参数(main方法里面 的String[] args),还可以获取指定格式的命令行参数信息,

/**
* Return the raw unprocessed arguments that were passed to the application.
* @return the arguments
*/
String[] getSourceArgs();
/**
* Return the names of all option arguments. For example, if the arguments were
* "--foo=bar --debug" would return the values {@code ["foo", "debug"]}.
* @return the option names or an empty set
*/
Set<String> getOptionNames();

/**
* Return the collection of values associated with the arguments option having the
* given name.
* <ul>
* <li>if the option is present and has no argument (e.g.: "--foo"), return an empty
* collection ({@code []})</li>
* <li>if the option is present and has a single value (e.g. "--foo=bar"), return a
* collection having one element ({@code ["bar"]})</li>
* <li>if the option is present and has multiple values (e.g. "--foo=bar --foo=baz"),
* return a collection having elements for each value ({@code ["bar", "baz"]})</li>
* <li>if the option is not present, return {@code null}</li>
* </ul>
* @param name the name of the option
* @return a list of option values for the given name
*/
List<String> getOptionValues(String name);

  CommandLineRunner--run:String[] args ,来自于main方法里面的String[] args

 

posted @ 2019-08-05 10:14  兵哥无敌  阅读(232)  评论(0编辑  收藏  举报