3

I am trying to inject a map like this from yml

redisDbIndex:
   key1: 1
   key2: 2
   key3: 3

into a Map<String, String> map = new HashMap<>();

And in my configuration class ( I don't want to use @ConfigurationProperties(prefix="...") annotation as I already know that approach.

I want to know how to do the same using @Value("${placeholder}") annotation. Is is possible? If yes, how? I am just exploring the internals. If anyone can give me a hint that would be great.

I am using spring boot 1.4.3.RELEASE(does it have the feature even?)

Mukul Anand
  • 606
  • 6
  • 24
  • Possible duplicate of [How to inject a Map using the @Value Spring Annotation?](https://stackoverflow.com/questions/30691949/how-to-inject-a-map-using-the-value-spring-annotation) – Pospolita Nikita Jun 25 '18 at 14:00
  • Of course it is possible. Are your indentations correct? Have you got any errors? – Urosh T. Jun 25 '18 at 15:07
  • This might be helpful answer you are looking for [enter link description here](https://stackoverflow.com/questions/24917194/spring-boot-inject-map-from-application-yml) – Ryuzaki L Jun 25 '18 at 16:00
  • yaa, I am getting an error like this: java.lang.IllegalArgumentException: Could not resolve placeholder 'redisDbIndex' in string value "${redisDbIndex}". Doubts: Is the difference between `@Value` and `@ConfigurationProperties` the way they do type-safe injections? Does `@Value` always consider string values to inject? If i try to insert a list using `@Value` it works but map doesn't. Also, is there any standard for the indentation. (I am using a 2 spaces as indentation for the key-value pairs) – Mukul Anand Jun 26 '18 at 08:25
  • @PospolitaNikita I tried using the "#{${redisDbIndex}}" placeholder but still it does not work. There in the example, they are using properties file syntax. I do doubt that it might not create a difference whether yml or properties file is used but it does not work for me atleast. – Mukul Anand Jun 26 '18 at 10:14
  • The same problem on Spring Boot 2.4.4 – AlexElin Apr 04 '21 at 12:33

0 Answers0