SpringBoot中的CommandLineRunner、 ApplicationRunner

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)。
 具体调用时间在容器刷新完成之后,略迟与刷新监听器。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值