1

I have application.properties file which contains Map values like below,

myMap={key1:'value1',key2:'value2',....}

Now I know I can read this using,

@Value("#{${myMap}}")  
private Map<String,String> myMap;

But I want to read this using Environment API. But I don't see proper method to fetch the Map value as Map. All I see is

import org.springframework.core.env.Environment;

@Autowired
private Environment env;

Map<String,String> myMap = env.getProperty("myMap"); // returns String

How can I get a Map directly from the properties file using the Enviromnet API? Or I need to do conversions on my own?

Any help is appreciated.

WebNoob
  • 237
  • 6
  • 16
  • Why you don't create a custom class property? See https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-loading-yaml – Cristiano Bombazar Aug 23 '18 at 14:36
  • Works with Map too. – Cristiano Bombazar Aug 23 '18 at 14:37
  • Thanks for the response. But that link suggests converting entire properties file into map. I have many properties apart from that myMap property. So dont want to convert entire properties file that way. Any other suggestion? – WebNoob Aug 24 '18 at 05:37
  • @CristianoBombazar Any idea how can I resolve this? – WebNoob Aug 24 '18 at 13:08
  • Sorry, i never done this before. But, see this link and look if one of those answers can help you: https://stackoverflow.com/questions/35531661/using-env-variable-in-spring-boots-application-properties – Cristiano Bombazar Aug 24 '18 at 17:17
  • @CristianoBombazar thanks for the suggestions. It seems I had to do it manually and there is not clear cut way out for what I was trying to achieve. Thanks for the help though. – WebNoob Aug 27 '18 at 13:03

0 Answers0