在 SpringApplicationBuilder() 上设置运行时属性 [英] Setting the run time properties on SpringApplicationBuilder()

查看:185
本文介绍了在 SpringApplicationBuilder() 上设置运行时属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过创建 Cucumber 集成测试来测试我的 Spring 应用程序代码.我正在使用 SpringApplicationBuilder 在触发实际逻辑之前启动我的应用程序,并使用以下语法来执行此操作:-

I need to test my Spring application code by creating a Cucumber integration test. I am using SpringApplicationBuilder to start up my application before the actual logic is triggered and am using the following syntax to do so:-

    application = new SpringApplicationBuilder()
        .parent(new Object[]{"classpath:file1.xml", "classpath:file2.xml"})
        .profiles("abc")
        .properties("name:value") [It has 5/6 (name:value) pairs here]*
        .showBanner(false)
        .logStartupInfo(true)
        .headless(true)
        .application()
        .run();

我的 Spring 应用程序正确启动.但是,它不会获取我传递给 SpringApplicationBuilder() 的属性(名称、值)对的值.我尝试了以下设置它们:-

My Spring application starts up correctly. However, it does not get the values for the property (name, value) pairs that I am passing to the SpringApplicationBuilder(). I have tried the following to set them :-

  • 使用上述名称值对
  • 使用(名称,值)对的 HashMap
  • 创建一个 ConfigurableEnvironment,检索 MutablePropertySources 并在其中设置我的属性.

这些选项都不起作用,因此当应用程序启动并且代码尝试访问某些系统属性值时,它会中断.

None of these options are working, so when the application starts up and the code tries to access certain System Property values, it breaks.

任何想法如何解决这个问题......非常感谢所有帮助!

Any ideas how this could be fixed.. All the help is greatly appreciated!

推荐答案

您在 SpringApplicationBuilder 上配置的属性在您的应用程序的 Environment 中可用,而不是作为系统属性.如果您的代码需要系统属性,那么您应该在启动 JVM 时使用 System.setProperty(key, value) 或作为 -D 参数设置它们

The properties you configure on SpringApplicationBuilder are made available in your application's Environment, not as system properties. If your code's expecting system properties then you should set them using System.setProperty(key, value) or as -D arguments when you launch the JVM

这篇关于在 SpringApplicationBuilder() 上设置运行时属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆