0

I have a application.properties file that needs some dynamic keys, which allow at least one level of nesting. In technical terms, the application starts and I am able to read those values, but the metadata doesn't seem to work correctly, because IntelliJ Ultimate is giving me some errors:
Cannot resolve property 'foo' in java.util.Map

# Static property
com.company.version=1.0

# Dynamic property, starting after products
com.company.products.first.foo=firstFoo
com.company.products.first.bar=firstBar
com.company.products.second.foo=SecondFoo
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Configuration
@ConfigurationProperties(prefix = "com.company")
public class Properties {
    private String version;
    private Map<String, Map<String, ArrayList<String>>> products = new HashMap<>();

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public Map<String, Map<String, ArrayList<String>>> getProducts() {
        return products;
    }

    public void setProducts(Map<String, Map<String, ArrayList<String>>> products) {
        this.products = products;
    }
}

In additional I would like to know what the best practice would be to read those values. For "normal" static properties I used the Environment that I autowire in the constructor, but that is – as far as I know – unable to read the products map. Thats why I autowire my Properties class and use the getProducts function.

Thanks a lot for your help!

1 Answer 1

1

Might this work?

# Dynamic property, starting after products
com.company.products.first[foo]=firstFoo
com.company.products.first[bar]=firstBar
com.company.products.second[foo]=SecondFoo

If you want more than item in your array list do:

com.company.products.first[foo]=firstFoo,secondFoo

However, I've come massively unstuck in the past using deeply nested maps of maps. Maybe if you can declare a concrete class instead? Also, I'd suggest using interfaces rather than concrete collections (e.g. List rather than ArrayList, or maybe even a Set to prevent duplicates?)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.