【问题标题】:How to run specific SpringBoot CommandLineRunner runner in my app?如何在我的应用程序中运行特定的 SpringBoot CommandLineRunner runner?
【发布时间】:2015-12-21 18:17:54
【问题描述】:

我正在关注 Spring Boot CommandLineRunner : filter option argument 为我的 SpringBoot 应用程序创建 CommandLineRunner。我不明白的是如何在 ma app 中运行这个特定的命令?在这个特定的示例中,有一个 FileProcessingCommandLine 类实现了“运行”方法。现在如何从命令提示符运行它?

【问题讨论】:

    标签: spring-mvc spring-boot


    【解决方案1】:

    无法手动运行的想法。 Spring框架负责运行这个方法!

    您应该使用 Spring 的 @Component 注释标记您的类,以便 @SpringBootApplication 自动获取。

    如果您的类实现了 Spring Boot 的 CommandLineRunner,它将在所有 bean 创建和注册后运行。

    @Component
    public class FirstCommandLineRunner implements CommandLineRunner {
    
    
        @Override
        public void run(String... strings) throws Exception {
            System.out.println("hello world");
        }
    }
    

    【讨论】:

    • 我明白了,这可能是我的误解。我正在尝试找到一种方法,如何从控制台手动运行一些命令,该命令可以查看所有 spring 环境,用于“数据导入”、一些维护任务等目的。
    • 如果您想检查应用程序的运行状况,您可以查看此docs.spring.io/spring-boot/docs/current/reference/html/…,它非常适合维护。对于数据导入,您可以使用CommandLineRunner,当应用程序加载时 - 进行初始数据导入。
    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 2018-10-31
    • 2020-06-03
    • 1970-01-01
    相关资源
    最近更新 更多