SpringBoot 中CommandLineRunner 、ApplicationRunner
SpringbBoot架构中有时候数据是需要预加载的,需要提前加载到缓存或类属性中,并且希望执行操作的时间是在容器启动末尾时执行操作。两个接口(CommandLineRunner ApplicationRunner).
ApplicationRunner接口使用
1、新建类实现ApplicationRuner接口,并添加注解@Component让容器可以扫描到。
package com.runner.runner;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("启动预加载数据(ApplicationRunner)...");
args.getOptionNames().forEach(System.out::println);
Arrays.stream(args.getSourceArgs()).forEach(System.out::println);
}
2.2.2、启动类代码:
package com.runner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RunnerApplication {
public static void main(String[] args) {
SpringApplication.run(RunnerApplication.class, args);
System.out.println("RunnerApplication执行完毕");
}
}
2、CommandLineRunner接口使用
1、新建类实现CommandLineRuner接口,并添加注解@Component让容器可以扫描到。
package com.runner.runner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
public class CommandLineRunnerImpl implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("启动预加载数据(CommandLineRunner)...");
Arrays.stream(args).forEach(System.out::println);
}
}
2.2.2、启动类代码:
package com.runner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RunnerApplication {
public static void main(String[] args) {
SpringApplication.run(RunnerApplication.class, args);
System.out.println("RunnerApplication执行完毕");
}
}
三、区别
1、ApplicatonRunner接口的方法参数ApplicationArguments(是个对象)比CommandLineRunner接口的方法参数(是个可以接收多个string的参数)功能更强大。ApplicatonRunner接口的方法参数ApplicationArguments既可以获取参数的字符串,也可以直接获取key;CommandLineRunner接口的方法参数只能获取参数的字符串。
2、ApplicationRunner接口的实现方法比CommandLineRunner接口的实现方法前执行(当然也可以设置@Order的值来决定谁先执行)
package com.runner.runner;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Order(5)
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) {
System.out.println("启动预加载数据(ApplicationRunner)...");
args.getOptionNames().forEach(System.out::println);
Arrays.stream(args.getSourceArgs()).forEach(System.out::println);
}
}
package com.runner.runner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Order(2)
@Component
public class CommandLineRunnerImpl implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("启动预加载数据(CommandLineRunner)...");
Arrays.stream(args).forEach(System.out::println);
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
2021-09-14 Java虚拟机