SpringBoot 启动时执行:CommandLineRunner、ApplicationRunner 和 ApplicationListener 零、前置知识:SpringBoot run() 源码解析 SpringApplication.run 方法源码 /** * Run the Spring application, creating and refreshing a new * {@link ApplicationContext}. * @param args the application arguments (usually passed from a Java main method) * @return a running {@link ApplicationContext} */ public ConfigurableApplicationContext run(String... args) { // 计时器 StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>(); // 配置系统属性:默认缺失外部显示屏等允许启动 configureHeadlessProperty(); // 获取并启动事件监听器,如果项目中没有其他监听器,则默认只有 EventPublishingRunListener SpringApplicationRunListeners listeners = getRunListeners(args); // 将事件广播给 listeners listeners.starting(); try { // application 启动参数列表 ApplicationArguments applicationArguments = new DefaultApplicationArguments( args); // 配置运行环境:例如激活应用***.yml配置文件 ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); configureIgnoreBeanInfo(environment); // 加载配置的banner(gif,txt...),即控制台图样 Banner printedBanner = printBanner(environment); // 创建上下文对象,并实例化 context = createApplicationContext(); exceptionReporters =