0

it should be easy. But I have not some idea. A file 'props.properties' on disk. Where are any properties in the file. I have a class of configuration:

@Configuration
@PropertySource(value = "file:props.properties", encoding = "utf8")
public class AppConfig {
...
  @Some_spring_annotation  <-- that is qestion
  private Map map;
...
}

How to load all properties from 'props.properties' to 'map' by Spring?

Evgeniy Egorov
  • 223
  • 2
  • 7
  • 1
    https://stackoverflow.com/questions/30691949/how-to-inject-a-map-using-the-value-spring-annotation Use annotation @Value – R.G Oct 21 '19 at 10:13
  • Possible duplicate of [Spring Boot - inject map from properties file](https://stackoverflow.com/questions/43098009/spring-boot-inject-map-from-properties-file) – Vinay Prajapati Oct 21 '19 at 11:42

3 Answers3

1

You can load whole porperties file as Map by defining it as PropertiesFactoryBean and then using it with @Resource annotation.

@Configuration
@PropertySource(value = "file:src/main/resources/test.properties", encoding = "utf8")
public class AppConfig {

@Value("${propertyname}")
String prop;

@Resource(name = "propertyBean")
private Map<String, String> propMap;

@Bean(name = "propertyBean")
public static PropertiesFactoryBean mapper() {
        PropertiesFactoryBean bean = new PropertiesFactoryBean();
        bean.setLocation(new FileSystemResource("src/main/resources/test.properties"));
        return bean;
}

public Map<String, String> getPropMap() {
    return propMap;
}
}

And access any key present in the properties file using key like below:-

@RestController
public class Test {

@Autowired
private AppConfig appConfig;

@RequestMapping(value = "/test", method = RequestMethod.GET)
public String  login(HttpServletRequest request){
    return appConfig.getPropMap().get("application.name");
}

}



test.propeties:-
server.port1=8099
application.name=edge-service
propertyname=dddd

In this you don't need to write @Value annotation everytime, you can access values using propMap. If you want to read single key use

@Value("${propertyname}")
String prop;

Even you can define map data into property file

propertyname={key1:'value1',key2:'value2'}

There can be many implementation to achieve it based on requirement.

Himanshu Sharma
  • 560
  • 3
  • 12
0

Properties file entry

valMap ={key1: '1', key2: '2', key3: '3'}

Spring annotation

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

More on @Value annotation here : https://www.baeldung.com/spring-value-annotation

R.G
  • 6,436
  • 3
  • 19
  • 28
0

You can have values from properties file read in a single map

Your application.properties file should look like

app.properties.map.key1 = value1
app.properties.map.key2 = value2
app.properties.map.key3 = value3
app.properties.map.key4 = value4
app.properties.map.key5 = value5

And, your @ConfigurationProperties annotated class should look like

@ConfigurationProperties(prefix = "app.properties")
public class AppProperties {

    private Map<String, String> map;

    // No Arguments Constructor

    // Getters & Setters
}

Also, starting from Spring Boot 2.2.0, your @ConfigurationProperties classes can be immutable

@ConfigurationProperties(prefix = "app.properties")
public class AppProperties {

    private final Map<String, String> map;

    @ConstructorBinding
    public AppProperties(Map<String, String> map) {

      this.map = map;
    }
}
isank-a
  • 1,545
  • 2
  • 17
  • 22