詳解springboot設(shè)置默認參數(shù)Springboot.setDefaultProperties(map)不生效解決
我們都知道springboot 由于內(nèi)置tomcat(中間件)直接用啟動類就可以啟動了。
而且我們有時想代碼給程序設(shè)置一些默認參數(shù),所以使用方法Springboot.setDefaultProperties(map)
SpringApplication application = new SpringApplication(startClass);
//
Map<String, Object> params = new HashMap<>();
params.put("lai.ws.test","test");
application.setDefaultProperties(params);
ApplicationContext context = application.run(startClass,args);
于是啟動后發(fā)現(xiàn) lai.ws.test 居然是null,也就是參數(shù)設(shè)置不成功,百思不得其解。為此還斷點進入SpringApplication 的源碼里。最后發(fā)現(xiàn)以下源碼
/**
* Static helper that can be used to run a {@link SpringApplication} from the
* specified sources using default settings and user supplied arguments.
* @param primarySources the primary sources to load
* @param args the application arguments (usually passed from a Java main method)
* @return the running {@link ApplicationContext}
*/
public static ConfigurableApplicationContext run(Class<?>[] primarySources,
String[] args) {
return new SpringApplication(primarySources).run(args);
}
各位,發(fā)現(xiàn)了沒,又new 了一個SpringApplication。到此,問題答案找到了。
如果啟動類要設(shè)置默認參數(shù),不用使用以下方法去啟動
ApplicationContext context = application.run(startClass,args);
應(yīng)該使用以下
ApplicationContext context = application.run(args);
到此這篇關(guān)于詳解springboot設(shè)置默認參數(shù)Springboot.setDefaultProperties(map)不生效解決的文章就介紹到這了,更多相關(guān)Springboot.setDefaultProperties 不生效內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用MappingJackson2XmlView實現(xiàn)JSON到XML的視圖轉(zhuǎn)換
MappingJackson2XmlView來實現(xiàn)從JSON到XML格式的響應(yīng)轉(zhuǎn)換,本文將通過案例,將展示如何將JSON格式的數(shù)據(jù)轉(zhuǎn)換為XML格式,以滿足不同客戶端的數(shù)據(jù)交換需求,需要的朋友可以參考下2024-07-07
Java多線程使用阻塞隊列實現(xiàn)生產(chǎn)者消費者模型詳解
這篇文章主要介紹了Java多線程使用阻塞隊列實現(xiàn)生產(chǎn)者消費者模型詳解,主要講解阻塞隊列的特性、實際開發(fā)中常用的到的生產(chǎn)者消費者模型,以及生產(chǎn)者消費者模型解耦合、削峰填谷的好處,需要的朋友可以參考下2023-07-07
GC調(diào)優(yōu)實戰(zhàn)之高分配速率High?Allocation?Rate
這篇文章主要為大家介紹了GC調(diào)優(yōu)之高分配速率High?Allocation?Rate的實戰(zhàn)示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-01-01
LambdaQueryWrapper的實現(xiàn)原理分析和lambda的序列化問題
這篇文章主要介紹了LambdaQueryWrapper的實現(xiàn)原理分析和lambda的序列化問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教。2022-01-01

