Annotation @Configurationproperty

We use @ConfigurationProperty to match a bean to a property in the file.

For example:

@Component  
@Getter  
@Setter  
@ConfigurationProperties(prefix="cloud")  
public class CloudConfiguration {  
  private String platform;  
  private Account account;  
  private Environment environment;  
  private String myParameter;
}

Will match with:

cloud:  
  platform: AWS  
  account:  
    name: Austin  
    id: 1143210  
  environment:  
    name: dev
  my-parameter: test

Relaxed Binding

By default the parameter will be relaxed binding. Which means the bean field of myParameter will match with

  • my-parameter
  • myParameter
  • MY_PARAMETER
  • my_parameter

Mapping to Java Map

To map to java map, we can do the following:

cloud:  
  customMap:  
    key1: value of key$1  
    key2: value of key$2

And in our class:

@Component  
@Getter  
@Setter  
@ConfigurationProperties(prefix="cloud")  
public class CloudConfiguration {  
  private Map<String, String> customMap;  
}

However, in our key name, we cannot have non alphabetical values (except for . and -).

So if we have something like

customMap:  
  key$1: value of key$1  
  key$2: value of key$2

It will not work. In order to have the key name to be key$1, we have to use "[key$1]" like this:

cloud:
  customMap:  
    "[key$1]": value of key$1  
    "[key$2]": value of key$2