如何将地图的application.properties转换为application.yml [英] How does one convert application.properties to application.yml for maps

查看:98
本文介绍了如何将地图的application.properties转换为application.yml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了这个,但是行不通,我要去哪里错了?

application.properties(工作正常)

  document-contact = {名称:'joe',电子邮件:'joe.bloggs@gmail.com'} 

application.yml(不起作用;下面是stacktrace)

 文档联系人:名称:'joe'电子邮件:"joe.bloggs@gmail.com" 

Java:

  @Value(#{$ {document-contact}}")私有Map< String,String>接触; 

Stacktrace:

  org.springframework.beans.factory.BeanCreationException:创建名称为'consolidatedSwaggerDocumentationController'的bean时出错:自动连接依赖项的注入失败;嵌套异常为java.lang.IllegalArgumentException:无法解析值#{$ {document-contact}}"中的占位符"document-contact"在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:403)〜[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1429)〜[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)〜[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] 

解决方案

您的 application.yml 不等同于您正在使用的 application.properties .

除了读取单独的属性外,您只有一个名为 document-contract (= $ {document-contract} )的属性,其中包含以下字符串:

 <代码>"{{:'joe',email:'joe.bloggs@gmail.com'}" 

要将其转换为 Map ,您使用的是


或者,如果您希望像以前一样使用多个YAML属性,则应注意 @Value 不支持 Map 结构.相反,您应该使用 @ConfigurationProperties :

 <代码> @ConfigurationProperties(prefix ="app")公共类ApplicationProperties {私有Map< String,String>documentContact;//Getters + Setters} 

使用 @ConfigurationProperties 时,您将必须使用前缀,因此您应将YAML结构更改为:

  app:文件联络人:名称:乔电子邮件:joe.bloggs@gmail.com 

作为参考,这将是等效的属性文件:

  app.document-contract.name = joeapp.document-contact.email=joe.bloggs@gmail.com 

I tried this, it doesn't work, where am I going wrong?

application.properties (works fine)

document-contact={name:'joe',email:'joe.bloggs@gmail.com'}

application.yml (doesn't work; stacktrace below)

document-contact:
  name: 'joe'
  email: 'joe.bloggs@gmail.com'

Java:

    @Value("#{${document-contact}}")
    private Map<String, String> contact;

Stacktrace:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'consolidatedSwaggerDocumentationController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'document-contact' in value "#{${document-contact}}"
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:403) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1429) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]

解决方案

Your application.yml is not equivalent to the application.properties you're using.

Rather than reading separate properties, you only have a single property called document-contract (= ${document-contract}), which contains the following string:

"{name:'joe',email:'joe.bloggs@gmail.com'}"

To convert it to a Map, you're using Spring Expression Language (SpEL). That's why you need both #{...} and ${...}.

Your application.yml file on the other hand doesn't have a single property called document-contract, and thus, it doesn't work. If you want to do the same kind of thing within your YAML, it should be:

document-contract: "{name: 'joe', email: 'joe.bloggs@gmail.com'}"


Alternatively, if you want to use multiple YAML properties like you did, you should be aware that @Value doesn't support Map structures. In stead, you should be using @ConfigurationProperties:

@ConfigurationProperties(prefix = "app")
public class ApplicationProperties {
    private Map<String, String> documentContact;

    // Getters + Setters
}

With @ConfigurationProperties, you would have to use a prefix though, so you should change your YAML structure to:

app:
  document-contact:
    name: joe
    email: joe.bloggs@gmail.com

For the reference, this would be the equivalent properties file:

app.document-contract.name=joe
app.document-contact.email=joe.bloggs@gmail.com

这篇关于如何将地图的application.properties转换为application.yml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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