这篇文章主要讲解了为什么springboot设置默认参数Springboot.setDefaultProperties(map)不生效,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。
我们都知道springboot 由于内置tomcat(中间件)直接用启动类就可以启动了。
而且我们有时想代码给程序设置一些默认参数,所以使用方法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);
于是启动后发现 lai.ws.test 居然是null,也就是参数设置不成功,百思不得其解。为此还断点进入SpringApplication 的源码里。最后发现以下源码
/**
* 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);
}
各位,发现了没,又new 了一个SpringApplication。到此,问题答案找到了。
如果启动类要设置默认参数,不用使用以下方法去启动
ApplicationContext context = application.run(startClass,args);
应该使用以下
ApplicationContext context = application.run(args);
看完上述内容,是不是对为什么springboot设置默认参数Springboot.setDefaultProperties(map)不生效有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。