【发布时间】:2015-12-21 18:17:54
【问题描述】:
我正在关注 Spring Boot CommandLineRunner : filter option argument 为我的 SpringBoot 应用程序创建 CommandLineRunner。我不明白的是如何在 ma app 中运行这个特定的命令?在这个特定的示例中,有一个 FileProcessingCommandLine 类实现了“运行”方法。现在如何从命令提示符运行它?
【问题讨论】:
我正在关注 Spring Boot CommandLineRunner : filter option argument 为我的 SpringBoot 应用程序创建 CommandLineRunner。我不明白的是如何在 ma app 中运行这个特定的命令?在这个特定的示例中,有一个 FileProcessingCommandLine 类实现了“运行”方法。现在如何从命令提示符运行它?
【问题讨论】:
无法手动运行的想法。 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");
}
}
【讨论】:
CommandLineRunner,当应用程序加载时 - 进行初始数据导入。