zoukankan      html  css  js  c++  java
  • 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,因为此处抛出异常会影响项目启动。

  • 相关阅读:
    Asp.net2.0 中自定义过滤器对Response内容进行处理 dodo
    自动化测试工具 dodo
    TestDriven.NET 2.0——单元测试的好助手(转) dodo
    JS弹出窗口的运用与技巧 dodo
    ElasticSearch 简介 规格严格
    修改PostgreSQL字段长度导致cached plan must not change result type错误 规格严格
    Linux系统更改时区(转) 规格严格
    mvn编译“Cannot find matching toolchain definitions for the following toolchain types“报错解决方法 规格严格
    ElasticSearch 集群 & 数据备份 & 优化 规格严格
    Elasticsearch黑鸟教程22:索引模板的详细介绍 规格严格
  • 原文地址:https://www.cnblogs.com/seasail/p/12179364.html
Copyright © 2011-2022 走看看