详解springboot设置默认参数Springboot.setDefaultProperties(map)不生效解决

这篇文章主要介绍了详解springboot设置认参数Springboot.setDefaultProperties(map)不生效解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

我们都知道springboot 由于内置tomcat(中间件)直接用启动类就可以启动了。

而且我们有时想代码给程序设置一些认参数,所以使用方法Springboot.setDefaultProperties(map)

SpringApplication application = new SpringApplication(startClass); // Map 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);

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...