Spring Boot 中初始化資源的方式,你知道幾種?
作者:小破孩樓主
來源:cnblogs.com/zouhong/p/14415713.html
CommandLineRunner
定義初始化類 MyCommandLineRunner實現(xiàn) CommandLineRunner接口,并實現(xiàn)它的run()方法,在該方法中編寫初始化邏輯注冊成Bean,添加 @Component注解即可示例代碼如下:
package cn.zh.controller;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Componentpublic class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("項目初始化---------------11");
}
}
package cn.zh;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProcApplication {
public static void main(String[] args) {
System.out.println("...start SpringApplication.run");
SpringApplication.run(ProcApplication.class,args);
System.out.println("....end SpringApplication.run");
}
}

ApplicationRunner
定義初始化類 MyApplicationRunner實現(xiàn) ApplicationRunner接口,并實現(xiàn)它的run()方法,在該方法中編寫初始化邏輯注冊成Bean,添加 @Component注解即可示例代碼如下:
package cn.zh.controller;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class MyApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("項目初始化二---------");
}
}
run() 方法中自帶的形參不相同,在 CommandLineRunner 中只是簡單的String... args形參,而 ApplicationRunner 則是包含了 ApplicationArguments 對象,可以幫助獲得更豐富的項目信息。
@Order
ApplicationRunner 接口的初始化類,又有實現(xiàn)了 CommandLineRunner 接口的初始化類,那么會是哪一個先執(zhí)行呢?測試告訴我們,答案是實現(xiàn)了 ApplicationRunner 接口的初始化類先執(zhí)行,我想這點倒是不需要大家過分去關(guān)注為什么。但如果需要改變兩個初始化類之間的默認(rèn)執(zhí)行順序,那么使用 @Order 注解就可以幫助我們解決這個問題。
@Component
@Order(1)
public class MyCommandLineRunner implements CommandLineRunner {
/**
* Callback used to run the bean.
*
* @param args incoming main method arguments
* @throws Exception on error
*/
@Override
public void run(String... args) throws Exception {
System.out.println("項目初始化---------------11");
}
}
@Component
@Order(2)
public class MyApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("項目初始化二---------");
}
@PostConstruct
public void init(){
System.out.println("@PostConstruct初始化");
}
}



@PostConstruct
@PostConstruct 注解同樣可以幫助我們完成資源的初始化操作,前提是這些初始化操作不需要依賴于其它Spring beans的初始化工作。
@PostConstruct 注解是用在方法上的,寫一個方法測試一下吧。@PostConstruct
public void init(){
System.out.println("@PostConstruct初始化");
}
注意:
只有一個非靜態(tài)方法能使用此注解 被注解的方法不得有任何參數(shù) 被注解的方法返回值必須為void 被注解方法不得拋出已檢查異常 此方法只會被執(zhí)行一次 綜上,使用
@PostConstruct注解進(jìn)行初始化操作的順序是最快的,前提是這些操作不能依賴于其它Bean的初始化完成。通過添加@Order注解,我們可以改變同層級之間不同Bean的加載順序。
InitializingBean
@Component
public class MyListener1 implements InitializingBean {
@Autowired
private ShopInfoMapper shopInfoMapper;
@Override
public void afterPropertiesSet() {
//使用spring容器中的bean
//System.out.println(shopInfoMapper.selectById("1").getShopName());
System.out.println("項目啟動OK");
}
}
ApplicationListener
@Component
public class MyListener1 implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent applicationEvent) {
//打印出每次事件的名稱
System.out.println(applicationEvent.toString());
if (applicationEvent instanceof ApplicationReadyEvent) {
System.out.println("項目啟動OK");
}
}
}
推薦閱讀:
最近面試BAT,整理一份面試資料《Java面試BATJ通關(guān)手冊》,覆蓋了Java核心技術(shù)、JVM、Java并發(fā)、SSM、微服務(wù)、數(shù)據(jù)庫、數(shù)據(jù)結(jié)構(gòu)等等。
朕已閱 

