package com.lnjecit.lifecycle; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; /** * @author lnj * createTime 2018-11-07 22:37 **/ @Component public class ApplicationRunnerImpl implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("通过实现ApplicationRunner接口,在spring boot项目启动后打印参数"); String[] sourceArgs = args.getSourceArgs(); for (String arg : sourceArgs) { System.out.print(arg + " "); } System.out.println(); } }
项目启动后,会打印如下信息:
package com.lnjecit.lifecycle; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; /** * @author lnj * createTime 2018-11-07 22:25 **/ @Component public class CommandLineRunnerImpl implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("通过实现CommandLineRunner接口,在spring boot项目启动后打印参数"); for (String arg : args) { System.out.print(arg + " "); } System.out.println(); } }
两种实现方式的不同之处在于run方法中接收的参数类型不一样
当项目中同时实现了ApplicationRunner和CommondLineRunner接口时,可使用Order注解或实现Ordered接口来指定执行顺序,值越小越先执行
https://github.com/linj6/springboot-learn/tree/master/springboot-runner
https://blog.csdn.net/zknxx/article/details/52196427
原文:https://www.cnblogs.com/zuidongfeng/p/9926471.html