将application.yml文件转换为application.properties

时间:2018-08-28 13:54:27

标签: java spring-boot netflix-eureka application.properties

我有我的application.yml文件,如下所示。如何将其转换为application.properties 我正在尝试,但是如何在同一个文件中写入多个属性。它给了我重复的kery错误。

 ---
  spring:
    profiles: peer1
  eureka:
     instance:
        hostname: peer1
     client:
        serviceUrl:
           defaultZone: http://peer2/eureka/

 ---
 spring:
    profiles: peer2
 eureka:
    instance:
      hostname: peer2
    client:
      serviceUrl:
         defaultZone: http://peer1/eureka/

4 个答案:

答案 0 :(得分:2)

IntelliJ和其他IDE都提供了相同的插件。

例如-https://plugins.jetbrains.com/plugin/13804-convert-yaml-and-properties-file

安装插件,右键单击您的Yaml或属性文件,然后选择-“ 转换yaml和属性文件”。

答案 1 :(得分:0)

使用属性文件时,在同一文件中每个配置文件不能有多个“节”,这是仅Yaml可用的功能。 您将必须创建多个属性文件,每个配置文件一个,如下所示:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-change-configuration-depending-on-the-environment

  

要对属性文件执行相同的操作,可以使用application-$ {profile} .properties指定特定于配置文件的值

您将拥有一个包含通用值的主application.properties文件,然后每个应用程序有一个application-$ {profile} .properties文件,其中包含与环境/配置文件相关的值。

最后,您必须在运行应用程序时将活动配置文件设置为系统属性,或者直接在主application.properties文件中进行设置,如下所述:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-set-active-spring-profiles

答案 2 :(得分:0)

您将需要创建差异文件,例如:

  • application-dev.properties
  • application-prod.properties
  • application-test.properties

然后,您可以在 application.properties 中使用以下方法定义活动配置文件:

 spring.profiles.active=dev

答案 3 :(得分:0)

在 Spring Boot 2.4 中,有可能为此使用开关 spring.config.activate.on-profilespring.config.activate.on-profile=myprofile 之后定义的所有内容只会在活动配置文件设置为 myprofile 时设置。在给定的示例中,您将执行以下操作:

#-- Peer1 Config
spring.config.activate.on-profile=peer1
eureka.instance.hostname=peer1
eureka.client.serviceUrl.defaultZone=http://peer2/eureka/
#-- Peer2 Config
spring.config.activate.on-profile=peer2
eureka.instance.hostname=peer2
eureka.client.serviceUrl.defaultZone=http://peer1/eureka/

有关详细信息,请参阅 https://spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4

相关问题