欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2021-04-09 09:01:21|栏目:JAVA代码|点击:

我们都知道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搭建访客管理系统的实现示例

栏    目:JAVA代码

下一篇:Java中ArrayList类的源码解析

本文标题:详解springboot设置默认参数Springboot.setDefaultProperties(map)不生效解决

本文地址:http://www.codeinn.net/misctech/97769.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有