Spring Boot CommandLineRunner接口详解

x33g5p2x  于2022-09-18 转载在 Spring  
字(3.1k)|赞(0)|评价(0)|浏览(307)

在这篇文章中,我们将学习Spring Boot中的CommandLineRunner,先看一个示例。

典型的Java实现

让我们举一个纯Java的例子。

public class Adder {
    public static void main(String[] args) {
        int a = Integer.parseInt(args[0]);
        int b = Integer.parseInt(args[1]);
        System.out.println(a + b);
    }
}

上面的类是一个命令行应用程序的例子。该应用程序接受两个命令行参数,然后计算它们的总和,并在之后打印出来,应用程序退出。但现实世界中的命令行应用程序可能相当复杂。这就是spring boot的CommandLineRunner接口发挥作用的地方。

实现 CommandLineRunner

在这个例子中,我从Spring Initializer创建了一个没有任何依赖关系的项目。但该代码将与Spring Boot启动器一起工作。如果你做得正确,pom.xml应该只包含spring-boot-starter作为一个依赖项。当我们运行这个空项目时,你会看到spring示例程序在启动后没多久就死了。在这一点上,这个项目是无用的。让我们通过实现CommandLineRunner来使这个应用程序打印hello world,如下所示。

@SpringBootApplication
public class CommandlineApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(CommandlineApplication.class, args);
    }

    @Override
    public void run(String... args) {
        System.out.println("Hello World!");
    }
}

上述代码在日志输出的结尾处打印出Hello World。

下面是它的工作原理。自动配置在classpath中寻找任何CommandLineRunner组件并调用run(String[])方法。这将允许实现CommandLineRunner的组件能够访问应用上下文以及应用参数。例如,你可以打印当前上下文中的所有bean,如下图所示。

@SpringBootApplication
public class CommandlineApplication implements CommandLineRunner {

    Logger logger = LoggerFactory.getLogger(CommandlineApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(CommandlineApplication.class, args);
    }

    @Autowired
    ApplicationContext applicationContext;

    @Override
    public void run(String... args) {
        Map<String, Object> beansOfType = applicationContext.getBeansOfType(Object.class);
        beansOfType.forEach((s, o) -> {
            logger.info("{} - {}", s, o.getClass());
        });
    }
}

试着自己运行上述代码,你会看到结果。

命令行参数

你可能想知道为什么我们不能用@PostConstruct@EventListener(ApplicationReadyEvent.class)注解来实现同样的效果。EventListener和PostConstruct方法并不能访问命令行参数。这就是CommandLineRunner存在的原因。这个例子让你从命令行中列出所有包含搜索文本的bean名称的bean。

@SpringBootApplication
public class CommandlineApplication implements CommandLineRunner {

    Logger logger = LoggerFactory.getLogger(CommandlineApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(CommandlineApplication.class, args);
    }

    @Autowired
    ApplicationContext applicationContext;

    @Override
    public void run(String... args) {
        if (args.length > 0) {
            Map<String, Object> beansOfType = applicationContext.getBeansOfType(Object.class);
            beansOfType.forEach((s, o) -> {
                if (o.getClass().getCanonicalName().contains(args[0])) {
                    logger.info("{} - {}", s, o.getClass());
                }
            });
        }

    }
}

调用带有命令行参数Log的应用程序,在日志中只打印出以下两行。

springBootLoggingSystem - class org.springframework.boot.logging.logback.LogbackLoggingSystem
springBootLoggerGroups - class org.springframework.boot.logging.LoggerGroups

CommandLineRunner组件

所以很清楚为什么CommandLineRunner接口会作为spring boot的一部分。如果我们想启动任何既需要spring context又需要应用程序参数的进程,我们应该使用CommandLineRunner

这里需要注意的一点是,我让我的主类实现了CommandLineRunner。然而,这并不可取,也没有必要。你可以定义你自己的扩展接口组件,如下:

@Component
public class HelloWorldCommandLineRunner  implements CommandLineRunner {
Logger logger = LoggerFactory.getLogger(CommandlineApplication.class);

    @Override
    public void run(String... args) throws Exception {
        logger.info("Hello world from command line runner");
    }
}

多个 CommandLineRunners

你也可以定义多个CommandLineRunner实现。这里唯一的问题是,这些组件中的每一个都将串联运行。如果其中一个要运行很长时间,那么其他的运行就会受到影响。另外,Spring对相同类型的Bean的默认行为是按Bean名称的字母顺序进行排序。所以你可能会看到组件的执行顺序是一样的。

相关文章

微信公众号

最新文章

更多