I tried this, it doesn't work, where am I going wrong?
application.properties (works fine)
document-contact={name:'joe',email:'[email protected]'}
application.yml (doesn't work; stacktrace below)
document-contact:
name: 'joe'
email: '[email protected]'
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]
Unlike properties files, YAML supports multi-document files by design, and this way, we can store multiple profiles in the same file no matter which version of Spring Boot we use. Note: We usually don't want to include both the standard application.
How to Inject a Map From a YAML File. Spring Boot has taken data externalization to the next level by providing a handy annotation called @ConfigurationProperties. This annotation is introduced to easily inject external properties from configuration files directly into Java objects.
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:'[email protected]'}"
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: '[email protected]'}"
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: [email protected]
For the reference, this would be the equivalent properties file:
app.document-contract.name=joe
[email protected]
you need use follow:
tes:
maps:
key1: 15
key2: 2
and java code is:
@Data
@Component
@ConfigurationProperties(prefix = "tes")
public class MapTest {
private Map<String, String> maps;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With