zoukankan      html  css  js  c++  java
  • SpringBoot中的CommandLineRunner、 ApplicationRunner

    SpringBoot中提供了两个Runner----->CommandLineRunner、 ApplicationRunner

    这两个是接口,接口中的方法在系统启动时会执行,可以用于加载配置文件,数据库设置等。

    不过监听器也可以实现。

    只需实现接口,并注入容器,SpringBoot在启动时就会调用run方法。

    @Order(2)//设置顺序
    @Component
    public class AppRunner implements ApplicationRunner {
    
    	@Override
    	public void run(ApplicationArguments args) throws Exception {
    		// TODO Auto-generated method stub
    		System.out.println("启动参数"+args);
    		
    	}
    
    }
    
    @Order(1)
    @Component
    public class ComLine implements CommandLineRunner {
    
    	@Override
    	public void run(String... args) throws Exception {
    		System.out.println("命令行...输出"+args);
    		
    	}
    }
    
    
    
    
    命令行...输出[Ljava.lang.String;@4b8cf5e2
    启动参数org.springframework.boot.DefaultApplicationArguments@73b454e9
             
     两个接口使用方式一样,方法中参数分别是不定长字符串、字符串数组,可以接收启动时的参数,如args.getNonOptionArgs(arg_name)。
     具体调用时间在容器刷新完成之后,略迟与刷新监听器。
    
  • 相关阅读:
    明星球队的傲慢
    VS2010测试方面的文章重点
    项目管理有感
    团队从小到大,再到体验团队
    css实现文字渐变
    echarts坐标轴文字过长省略
    mustache+mock
    你有选择的权利
    呵呵,初学者小编
    Centos7升级到OpenSSH_8.8p1、OpenSSL 1.1.1l版本
  • 原文地址:https://www.cnblogs.com/cgl-dong/p/13820856.html
Copyright © 2011-2022 走看看