SpringBoot項目啟動后初始化執(zhí)行方法
在項目開發(fā)中某些場景必須要用到啟動項目后立即執(zhí)行方式的功能,如我們需要去初始化數(shù)據(jù)到
redis
緩存、設(shè)置策略工廠,或者啟動后讀取相應(yīng)的配置等,主要聊聊實現(xiàn)立即執(zhí)行的幾種方法。
一、CommandLineRunner
二、 ApplicationRunner
這兩者的實現(xiàn)方法一樣,都是去繼承相應(yīng)的接口然后重寫run
方法即可,也都是SpringBoot
框架所提供給我們的接口,也是項目中最常用的,比較靈活,有多個CommandLineRunner
或ApplicationRunner
實現(xiàn)類時可以通過@Order
注解或?qū)崿F(xiàn)Ordered
接口重寫getOrder
方法實現(xiàn)按指定順序執(zhí)行。
@Order(1) //通過order注解指定執(zhí)行順序
@Component
public class CommandLineRunnerInit implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("CommandLineRunner 啟動后執(zhí)行方法...");
}
}
ApplicationRunner 通過order注解指定執(zhí)行順序
@Order(2) //通過order注解指定執(zhí)行順序
@Component
public class ApplicationRunnerInit implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunner 啟動后執(zhí)行方法...");
}
}
ApplicationRunner 通過實現(xiàn) Ordered 指定執(zhí)行順序
@Component
// 通過實現(xiàn) Ordered 指定執(zhí)行順序
public class ApplicationRunnerInit implements ApplicationRunner, Ordered {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunner 啟動后執(zhí)行方法...");
}
@Override
public int getOrder() {
return 0;
}
}
這兩者的不同其實就是run
方法中所接收的參數(shù)類型不同,CommandLineRunner
是對我們啟動jar
包時所傳參數(shù)不進(jìn)行處理,原樣返回String
類型給你,ApplicationRunner
是封裝成了ApplicationArguments
參數(shù),通過這個類型可以更方便的判斷某些參數(shù)是否存在之類的。
二、JDK提供的@PostConstruct
@PostConstruct是JDK所提供的注解,使用該注解的方法會在服務(wù)器加載Servlet的時候運行。也可以在一個類中寫多個方法并添加這個注解。
@Component
public class PostConstructTest {
@PostConstruct
public void inti1() {
System.out.println("@PostConstruct 方法1執(zhí)行...");
}
@PostConstruct
public void inti2() {
System.out.println("@PostConstruct 方法2執(zhí)行...");
}
}
三、其他方法
3.1 ApplicationContextAware
ApplicationContextAware 一般被用來獲取applicationContext上下文信息,并且也可以啟動時被執(zhí)行
@Component
public class ApplicationContextAwareTest implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
System.out.println("ApplicationContextAware 方法執(zhí)行...");
}
}
3.2 ApplicationListener
ApplicationListener 事件監(jiān)聽,啟動會執(zhí)行多個監(jiān)聽器【會多次執(zhí)行】,具體可了解Springboot服務(wù)啟動過程。
@Component
public class ApplicationListenerTest implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
System.out.println("ApplicationListener 方法執(zhí)行...");
}
}
3.3 InitializingBean
InitializingBean 初始化啟動后方法執(zhí)行
@Component
public class InitializingBeanInit implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean 方法執(zhí)行...");
}
}
四、總結(jié)
以上多種初始執(zhí)行方法日志輸出為:文章來源:http://www.zghlxwxcb.cn/news/detail-672034.html
ApplicationContextAware 方法執(zhí)行...
InitializingBean 方法執(zhí)行...
@PostConstruct 方法1執(zhí)行...
@PostConstruct 方法2執(zhí)行...
ApplicationListener 方法執(zhí)行...
ApplicationListener 方法執(zhí)行...
ApplicationListener 方法執(zhí)行...
ApplicationListener 方法執(zhí)行...
ApplicationRunner 啟動后執(zhí)行方法...
CommandLineRunner 啟動后執(zhí)行方法...
ApplicationListener 方法執(zhí)行...
ApplicationListener 方法執(zhí)行...
存在多個ApplicationRunner、CommandLineRunner可以通過order指定其執(zhí)行順序,但其執(zhí)行順序沒有ApplicationContextAware等高。但一般生產(chǎn)中使用ApplicationRunner、CommandLineRunner,因為其更加靈活。文章來源地址http://www.zghlxwxcb.cn/news/detail-672034.html
到了這里,關(guān)于SpringBoot 啟動項目后執(zhí)行方法的幾種方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!