详解springboot设置默认...
我们都知道springboot 由于内置tomcat(中间件)直接⽤启动类就可以启动了。
⽽且我们有时想代码给程序设置⼀些默认参数,所以使⽤⽅法Springboot.setDefaultProperties(map)
SpringApplication application = new SpringApplication(startClass);
//
Map<String, Object> params = new HashMap<>();
params.put("st","test");
application.setDefaultProperties(params);
springboot其实就是springApplicationContext context = application.run(startClass,args);
于是启动后发现 st 居然是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)不⽣效解决的⽂章就介绍到这了,更多相关Springboot.setDefaultProperties 不⽣效内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。