SpringBoot - CommandLineRunner接口 - 初始化操作
作者:互联网
目录
- End -
﹀
﹀
﹀
梦想是咸鱼
关注一下吧
前言
CommandLineRunner
接口在容器启动成功后最后一步调用,常用于应用程序启动后初始化操作,其在整个应用生命周期内只会执行一次。
具体应用
实现CommandLineRunner接口
- Runner.java
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class Runner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
/**
* TODO:
*/
System.out.println(" Runner run ");
}
}
CommandLineRunner
会在Spring Boot
容器加载之后执行
CommandLineRunner实现类的执行顺序
- 使用
@Order
注解定义CommandLineRunner
的执行顺序
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(1)
public class RunnerOne implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
/**
* TODO:
*/
System.out.println(" RunnerOne run ");
}
}
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(2)
public class RunnerTwo implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
/**
* TODO:
*/
System.out.println(" RunnerTwo run ");
}
}
标签:初始化,CommandLineRunner,SpringBoot,Component,springframework,import,org,public 来源: https://www.cnblogs.com/maggieq8324/p/15188997.html