如何配置Spring Boot默认属性设置?

2026-05-24 10:111阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计425个文字,预计阅读时间需要2分钟。

如何配置Spring Boot默认属性设置?

我们知道Spring Boot依赖于内置的Tomcat(容器)来直接运行启动类。如果想给程序设置一些默认参数,可以通过`SpringBoot.setDefaultProperties(map)`方法传入一个Map来设定。例如:`SpringApplication.applicate(...)`。

我们都知道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);

应该使用以下

如何配置Spring Boot默认属性设置?

ApplicationContext context = application.run(args);

到此这篇关于详解springboot设置默认参数Springboot.setDefaultProperties(map)不生效解决的文章就介绍到这了,更多相关Springboot.setDefaultProperties 不生效内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计425个文字,预计阅读时间需要2分钟。

如何配置Spring Boot默认属性设置?

我们知道Spring Boot依赖于内置的Tomcat(容器)来直接运行启动类。如果想给程序设置一些默认参数,可以通过`SpringBoot.setDefaultProperties(map)`方法传入一个Map来设定。例如:`SpringApplication.applicate(...)`。

我们都知道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);

应该使用以下

如何配置Spring Boot默认属性设置?

ApplicationContext context = application.run(args);

到此这篇关于详解springboot设置默认参数Springboot.setDefaultProperties(map)不生效解决的文章就介绍到这了,更多相关Springboot.setDefaultProperties 不生效内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!