0

for example:

@Value("${a*}")
private Map<String, String> complexMap;

and on my application.yml:

a*:
  "a": "a"
  "b": "b"
  "c": "c"

I'm getting Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'a*' in value "${a*}"

rado
  • 5,720
  • 5
  • 29
  • 51

1 Answers1

2

Firstly, @Value is used to bind to one single key. However, if that key has any asterisk, it's still valid & read properly.

For example: we can read a property key test*: hello using @Value.

@Value("${test1*}")
String greet; //hello

Note: We should use @ConfigurationProperties annotation for reading multiple keys, here in your case, to read a Map<String, String>, we have to use @ConfigurationProperties annotation on a class that binds its fields to bunch of properties. So here, @Value is not a correct usage for binding to a Map, regardless of whether it has asterisk character or not. Example For reading a Map

Even with asterisk it's possible to read Map<String,String>

Example:

application.yaml

test:
  comp*:
     a: a
     b: b

MapProperties.java

@Component
@ConfigurationProperties(prefix = "test")
public class MapProperties {
    
Map<String, String> comp;

    public Map<String, String> getComp() {
        return comp;
    }

    public void setComp(Map<String, String> comp) {
        this.comp = comp;
    }
}

Here, comp* property is bound to this comp field in MapProperties class.

Now, you can autowire this MapProperties class, wherever its required

@Autowired
MapProperties mapProperties;

You can get the property value by calling its getter method like:

mapProperties.getComp()

Note: It does not work without this prefix, in our example, test. Without some prefix, we have to specify like @ConfigurationProperties(value= "comp*")

It throws an error:

  Configuration property name 'comp*' is not valid:

    Invalid characters: '*'
    Bean: mapProperties
    Reason: Canonical names should be kebab-case ('-' separated), lowercase alpha-numeric characters and must start with a letter
Dinesh Dontha
  • 537
  • 5
  • 17