Hel*_*eat 5 java maps yaml spring-boot
我试过了,不行,我哪里出错了?
application.properties(工作正常)
document-contact={name:'joe',email:'joe.bloggs@gmail.com'}
Run Code Online (Sandbox Code Playgroud)
application.yml(不起作用;下面的堆栈跟踪)
document-contact:
name: 'joe'
email: 'joe.bloggs@gmail.com'
Run Code Online (Sandbox Code Playgroud)
爪哇:
@Value("#{${document-contact}}")
private Map<String, String> contact;
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪:
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]
Run Code Online (Sandbox Code Playgroud)
你application.yml
的不等同于application.properties
你正在使用的。
您只有一个名为document-contract
(= ${document-contract}
) 的属性,而不是读取单独的属性,其中包含以下字符串:
"{name:'joe',email:'joe.bloggs@gmail.com'}"
Run Code Online (Sandbox Code Playgroud)
要将其转换为 a Map
,您使用的是Spring Expression Language (SpEL)。这就是为什么你需要#{...}
和${...}
。
application.yml
另一方面,您的文件没有一个名为 的属性document-contract
,因此它不起作用。如果你想在你的 YAML 中做同样的事情,它应该是:
document-contract: "{name: 'joe', email: 'joe.bloggs@gmail.com'}"
Run Code Online (Sandbox Code Playgroud)
或者,如果你想像你一样使用多个 YAML 属性,你应该知道它@Value
不支持Map
结构。相反,您应该使用@ConfigurationProperties
:
document-contract: "{name: 'joe', email: 'joe.bloggs@gmail.com'}"
Run Code Online (Sandbox Code Playgroud)
使用@ConfigurationProperties
,您必须使用前缀,因此您应该将 YAML 结构更改为:
app:
document-contact:
name: joe
email: joe.bloggs@gmail.com
Run Code Online (Sandbox Code Playgroud)
作为参考,这将是等效的属性文件:
app.document-contract.name=joe
app.document-contact.email=joe.bloggs@gmail.com
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6327 次 |
最近记录: |