yathirigan yathirigan - 1 year ago 1780
Java Question

How to inject a Map using the @Value Spring Annotation?

How can i inject values into a Map from the properties file using the @Value annotation in Spring ?

My Spring Java class is and i tried using the $ but, got the following error message

Could not autowire field: private java.util.Map Test.standard; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'com.test.standard' in string value "${com.test.standard}"

public class Test {

private Map<String,Pattern> standard = new LinkedHashMap<String,Pattern>

private String enabled;


I have the following properties in a .properties file


Answer Source

I believe Spring Boot supports loading properties maps out of the box with @ConfigurationProperties annotation.

According that docs you can load properties:


into bean like this:

public class Config {

    private List<String> servers = new ArrayList<String>();

    public List<String> getServers() {
        return this.servers;

I used @ConfigurationProperties feature before, but without loading into map. You need to use @EnableConfigurationProperties annotation to enable this feature.

Cool stuff about this feature is that you can validate your properties.