springboot 控制台输出调试

本贴最后更新于 2490 天前,其中的信息可能已经时异事殊

控制台输出调试

为了方便测试,在启动 springboot 项目后,期望通过控制台输出,查看运行。
此时可以通过让**@SpringBootApplication 类 implements CommandLineRunner 或者ApplicationRunner**接口,并实现 run 方法。

CommandLineRunner

CommandLineRunner 接口的 run 方法会传递 SpringApplication.run()方法中的字符数组参数 args。

@SpringBootApplication
public class Main implements CommandLineRunner {

    public static void main(String[] args) {
        System.out.println("main before run");
        SpringApplication.run(Main.class, "param1");
        System.out.println("main after run");
    }

    public void run(String... args) throws Exception {
        System.out.println("args[0]: " + args[0]);
        System.out.println("CommandLineRunner.run()");
    }

}

如果有多个 @SpringBootApplication 注解类且均实现了 CommandLineRunner 接口,可以通过**@Order 注解指定 run 方法的执行顺序,或者通过实现 Ordered 接口的 getOrder()**方法。
qQ320491

package crick.wang.springbootstudy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

@SpringBootApplication
@Order(2)
public class Main implements CommandLineRunner {

    public static void main(String[] args) {
        System.out.println("main before run");
        SpringApplication.run(Main.class, "param1");
        System.out.println("main after run");
    }


    public void run(String... args) throws Exception {
        System.out.println("first args[0]: " + args[0]);
        System.out.println("first CommandLineRunner.run()");
    }

}

@SpringBootApplication
@Order(1)
class SecondMain implements CommandLineRunner {

    public void run(String... args) throws Exception {
        System.out.println("second args[0]: " + args[0]);
        System.out.println("second CommandLineRunner.run()");
    }

}

@SpringBootApplication
class ThirdMain implements CommandLineRunner, Ordered {

    public void run(String... args) throws Exception {
        System.out.println("third args[0]: " + args[0]);
        System.out.println("third CommandLineRunner.run()");
    }

    public int getOrder() {
        return 3;
    }
}

执行结果为

  1. main before run
  2. second args[0]: param1
  3. second CommandLineRunner.run()
  4. first args[0]: param1
  5. first CommandLineRunner.run()
  6. third args[0]: param1
  7. third CommandLineRunner.run()
  8. main after run

ApplicationRunner

ApplicationRunner 接口和 CommandLineRunner 功能用法一致,唯一不同是对 args 参数进行了封装,run 方法传递参数为 ApplicationArguments,可以通过**ApplicationArguments.getSourceArgs()**获取字符数组参数 args。

  • Spring

    Spring 是一个开源框架,是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 在其著作《Expert One-On-One J2EE Development and Design》中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 JavaEE 应用程序开发提供集成的框架。

    940 引用 • 1458 回帖 • 160 关注
  • Java

    Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的。Java 技术具有卓越的通用性、高效性、平台移植性和安全性。

    3167 引用 • 8207 回帖 • 1 关注
  • 调试
    5 引用 • 35 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...