如何将地图的application.properties转换为application.yml [英] How does one convert application.properties to application.yml for maps
问题描述
我尝试了这个,但是行不通,我要去哪里错了?
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'}"
要将其转换为 或者,如果您希望像以前一样使用多个YAML属性,则应注意 使用 作为参考,这将是等效的属性文件: I tried this, it doesn't work, where am I going wrong? application.properties (works fine) application.yml (doesn't work; stacktrace below) Java: Stacktrace:
Your Rather than reading separate properties, you only have a single property called To convert it to a Your
Alternatively, if you want to use multiple YAML properties like you did, you should be aware that With For the reference, this would be the equivalent properties file:
这篇关于如何将地图的application.properties转换为application.yml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! Map
,您使用的是 @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
document-contact={name:'joe',email:'joe.bloggs@gmail.com'}
document-contact:
name: 'joe'
email: 'joe.bloggs@gmail.com'
@Value("#{${document-contact}}")
private Map<String, String> contact;
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]
application.yml
is not equivalent to the application.properties
you're using.document-contract
(= ${document-contract}
), which contains the following string:"{name:'joe',email:'joe.bloggs@gmail.com'}"
Map
, you're using Spring Expression Language (SpEL). That's why you need both #{...}
and ${...}
.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'}"
@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
}
@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
app.document-contract.name=joe
app.document-contact.email=joe.bloggs@gmail.com