Spring Boot 2 - 使用CommandLineRunner与ApplicationRunner
本篇文章我们将探讨CommandLineRunner和ApplicationRunner的使用。 在阅读本篇文章之前,你可以新建一个工程,写一些关于本篇内容代码,这样会加深你对本文内容的理解,关于如何快速创建新工程,可以参考我的这篇博客: Spring Boot 2 - 创建新工程 概述 CommandLineRunner和ApplicationRunner是Spring Boot所提供的接口,他们都有一个run()方法。所有实现他们的Bean都会在Spring Boot服务启动之后自动地被调用。 由于这个特性,它们是一个理想地方去做一些初始化的工作,或者写一些测试代码。 CommandLineRunner 使用Application实现 在我们新建好工程后,为了简单我们直接使用Application类实现CommandLineRunner接口,这个类的注解@SpringBootApplication会为我们自动配置。 package cn.examplecode.sb2runner; import org.slf4j.Logger; import org.slf4j.LoggerFa...