I want to initialize a class field by using Spring's @Value
Annotation.
The class field has the type Map<String, List<String>>
.
I have tried using Spring's Expression Language, but I must be doing something wrong because it throws the following error, while loading the Spring Application Context:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'myConfig':
Unsatisfied dependency expressed through field 'iWantToBeInitialized';
nested exception is org.springframework.beans.factory.BeanExpressionException:
Expression parsing failed; nested exception is org.springframework.expression.spel.SpelParseException:
EL1041E: After parsing a valid expression, there is still more data in the expression: 'a'
This is the code for MyConfig.java
:
@Configuration
public class MyConfig {
@Value("#{'${foo.bar.mymap}'}")
Map<String, List<String>> iWantToBeInitialized;
}
This is the code for application.properties
:
foo.bar.mymap=${baz.bub.other:{'a-key':{'a-value'},'b-key':{'b1-value','b2-value'},'c-key':{'c1-value'}}}
It looks like it begins parsing the a
of the a-key
, but it isn't able to figure out to parse a map from it.
I have tried searching and stumbeld over a couple of Spring mechanisms including defining a "Custom Property Something", but I could not figure out how to extrapolate the solution of those examples on to my specific usecase.
Can someone point me into the right direction?
Update 1:
CodePudding user response:
follow code code snippet
@Component
@ConfigurationProperties(prefix = "xxx")
public class MyConfig {
private Map<String, List<String>> iWantToBeInitialized;
}
application.yml
==================
xxx:
iWantToBeInitialized:
a-key:
- a-value
b-key:
- b1-value
- b2-value