Skip to content
Advertisement

How to fill HashMap from java property file with Spring @Value

Is it possible to use Spring @Value, to map values from properties file to the HashMap.

Currently I have something like this, and mapping one value is not a problem. But I need to map custom values in HashMap expirations. Is something like this possible?

JavaScript

Property file: ‘my_service.properties’

JavaScript

Is it posible to map like this key:value set

  • name1 = 100

  • name2 = 20

Advertisement

Answer

I make one solution inspired by the previous post.

Register property file in the Spring configuration:

JavaScript

And I create component:

JavaScript

And when I want to map all properties that begin with specifix value to HashMap, with @Value annotation:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement