Spring Boot 启动加载数据实现
文章目录
我们在使用SpringBoot搭建项目的时候,如果希望在项目启动完成之前,能够初始化一些操作,针对这种需求,可以考虑实现如下两个接口(任一个都可以)
org.springframework.boot.CommandLineRunner
org.springframework.boot.ApplicationRunner
例子:
CommandLineRunner
@Component
public class TestCommandLineRunner implements CommandLineRunner {
@Override
// 实现该接口之后,实现其run方法,可以在run方法中自定义实现任务
public void run(String... args) throws Exception {
System.out.println("服务启动,TestCommandLineRunner执行启动加载任务...");
}
}
ApplicationRunner
@Component
public class TestApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("服务启动,TestApplicationRunner执行启动加载任务...");
}
}

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论