您好,登錄后才能下訂單哦!
這篇文章主要講解了為什么springboot設置默認參數(shù)Springboot.setDefaultProperties(map)不生效,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
我們都知道springboot 由于內置tomcat(中間件)直接用啟動類就可以啟動了。
而且我們有時想代碼給程序設置一些默認參數(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ù)設置不成功,百思不得其解。為此還斷點進入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ù),不用使用以下方法去啟動
ApplicationContext context = application.run(startClass,args);
應該使用以下
ApplicationContext context = application.run(args);
看完上述內容,是不是對為什么springboot設置默認參數(shù)Springboot.setDefaultProperties(map)不生效有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。