SpringBoot的 CommandLineRunner的使用
用途
Spring boot的CommandLineRunner接口主要用于实现在应用初始化后,去执行一段代码块逻辑,这段初始化代码在整个应用生命周期内只会执行一次。
使用方式:
1: 和@Component 注解配合
@Component
public class InitCompetition implements CommandLineRunner {
private static Log log = LogFactory.get();
@Override
public void run(String... args) throws Exception {
Date date = new Date();
//初始化活动
String sql = "update competition set status = ? where status != ?";
Db.use().execute(sql, CompetitionStatus.FINISHED.name(),CompetitionStatus.FINISHED.name());
log.info("数据库初始化完成");
}
}
2.和@SpringBootApplication注解一起使用
————————————————
版权声明:本文为CSDN博主「小凡0907」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_45677590/article/details/121304437
在这篇文章中,我们将通过一个示例来了解 Spring Boot 中的命令行运行器以及如何正确实现它们。
典型的 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 Starter 一起使用。如果你正确地做到了这一点, 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。
下面是它的工作原理。自动配置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());
});
}
}
只需尝试自己运行上面的代码,您就会看到结果。
使用命令行参数
您可能想知道为什么我们不能使用 a @PostConstruct 或 @EventListener(ApplicationReadyEvent.class) 注释来实现相同的效果。事件和 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
命令行运行程序作为组件
所以很清楚为什么这个命令行运行器接口作为 Spring Boot 的一部分存在。CommandLineRunner 如果我们想要启动任何需要 spring 上下文的自动装配功能和应用程序参数的过程,我们应该使用 它。
这里要注意的一件事是,我让我的主类来实现 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 实现。这里唯一的问题是这些组件中的每一个都将串联运行。如果他们中的一个要跑很长时间,那么其他跑步者就会受苦。此外,相同类型的 bean 的默认 Spring 行为是它们按 bean 名称按字母顺序排序。因此,您可能会看到组件的执行顺序相同。
————————————————
版权声明:本文为CSDN博主「allway2」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/allway2/article/details/122685028
如果有多个命令行运行器,可以进行排序:
@Component
@Order(1)
public class AnotherDatabaseLoader implements CommandLineRunner {
@Component
@Order(2)
public class DataLoader implements CommandLineRunner {
另外一种在主应用的写法:
@SpringBootApplication
public class UnsplashApplication {
public static void main(String args) {
SpringApplication.run(UnsplashApplication.class, args);
}
@Bean
CommandLineRunner runner(){
return args -> {
System.out.println("CommandLineRunner running in the UnsplashApplication class...");
};
}
}
posted on 2022-06-15 19:58 ExplorerMan 阅读(519) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2021-06-15 kafka消费者如何才能从头开始消费某个topic的全量数据
2021-06-15 kafka consumer消费者 offset groupID详解