1、引入如下依赖:

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter</artifactId>
    4. </dependency>

    2、SpringBoot的主配置类实现 CommandLineRunner 接口

    1. @SpringBootApplication
    2. public class DemoApplication implements CommandLineRunner {
    3. public static void main(String[] args) {
    4. new SpringApplicationBuilder()
    5. .sources(DemoApplication.class) // SpringBoot主配置类
    6. .bannerMode(Banner.Mode.CONSOLE) // 在启动之前作相关配置
    7. .run(args);// 启动
    8. }
    9. @Override
    10. public void run(String... args) throws Exception {
    11. System.out.println("Hello");
    12. }
    13. }

    3、覆写 CommandLineRunner 接口的 run() 方法,在 run 方法中编写具体的处理逻辑