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,因为此处抛出异常会影响项目启动。

  • 相关阅读:
    绝对路径和相对路径的问题
    get请求中的中文乱码问题的解决方法
    jsp中的另一种分页实现方法
    jsp中退出功能实现代码
    jsp中完整的分页显示和页面跳转功能实现的源代码
    jsp中未登录用户也可以浏览页面的功能实现代码
    date和calendar对象的转化,使用,以及插入数据库中的总结
    jsp中向数据库中插入当前时间的方法精确到秒
    硬盘方式安装 Windows 7
    HP笔记本中CQ4x系列,在XP下的未知设备与声卡设备驱动
  • 原文地址:https://www.cnblogs.com/seasail/p/12179364.html
Copyright © 2011-2022 走看看