无涯教程-Spring Boot - 运行器

41 阅读1分钟

Application Runner和Command Line Runner接口,您可以在启动Spring Boot应用程序后执行代码。

应用程序运行

Application Runner是Spring Boot应用程序启动后用于执行代码的接口。下面给出的示例显示了如何在主类文件上实现Application Runner接口。

package com.learnfk.demo;

import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication public class DemoApplication implements ApplicationRunner { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override public void run(ApplicationArguments arg0) throws Exception { System.out.println("Hello World from Application Runner"); } }

现在,如果您从Application Runner观察Hello World下的控制台窗口,则在Tomcat启动后执行println语句。

Hello World From 应用程序运行器

命令行运行

命令行运行程序是一个界面。在Spring Boot应用程序启动后,用于执行代码。下面示例显示了如何在主类文件上实现Command Line Runner接口。

package com.learnfk.demo;

import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication public class DemoApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override public void run(String... arg0) throws Exception { System.out.println("Hello world from Command Line Runner"); } }

查看Tomcat启动后执行“Hello would from Command Line Runner”下面的控制台窗口。

命令行运行器

参考链接

www.learnfk.com/spring-boot…