轉自:
http://www.java265.com/JavaFramework/Spring/202207/3975.html
下文筆者講述initPropertySource()方法的功能簡介說明,如下所示
initPropertySource()方法簡介
initPropertySource()方法簡介:
主要用于Environment后
為系統提供良好的擴展
initPropertySource()方法啟動點
public ClassPathXmlApplicationContext(String[] configLocations,
boolean refresh,
ApplicationContext parent) throws BeansException{
super(parent);
setConfigLocationins(configurations);
if(refresh){
refresh();
}
}
refresh()中的第一個方法prepareRefresh()
呼叫initPropertySources()方法
protected void prepareRefresh(){
initPropertySources();
}
從以上的原始碼中
我們得知prepareRefresh()中
initPropertySources()方法并沒有任何引數
當進入initPropertySources()方法內部,也會發現該方法是空的,沒有任何邏輯,
Spring采用此種方式設計的目的
為了讓用戶根據自己的需要
重寫initPropertySources()方法
并在其中實作自己的邏輯
例
定義專案啟動時,必須擁有屬性 fileCodingType
那么我們就可以通過擴展initPropertySources()方法的方式
使系統必須擁有此屬性
public class MyClassPathXmlApplicationContext extends ClassPathXmlApplicationContext{
protected void initPropertySources(){
//添加驗證要求
getEnvironment().setRequiredProperties("fileCodingType");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/499255.html
標籤:其他
上一篇:邏輯運算子
