I've a scenario to solve, where I need to maintain some key-value pairs in the code. The key names are to be read from properties file. Which I'm going to do like this in spring boot.
@Data
@ConfigurationProperties( prefix = "project.keysList" )
public class KeyNames {
private String key1Name;
private String key2Name;
...
}
I'll get the corresponding values during the run-time. How can I store the values elegantly ? These are the options I'm considering.
- Define one more model object called
KeyValuesand store them. - Define a hashmap called
KeyValuesand store thekeyNamesand corresponding values.
Is there a better way other than these two, where I don't need to define a new object for storing the values, which I get during the run time.?
Appreciate any help in this.. Pls ask for clarifications in case my qn is not too clear.
CodePudding user response:
Define your key and value in resources folder of application.properties file and define @Value annotation in the above your Configuration class of field name.
For example:
application.properties file
key1.name=valuename
key2.name=valuename
============================
@ConfigurationProperties( prefix = "project.keysList" )
public class KeyNames {
@Value("${key1.name}")
private String key1Name;
@Value("${key2.name}")
private String key2Name;
}
CodePudding user response:
@Data @ConfigurationProperties(prefix = "custom")
public class CustomProperties {
private Long id;
private String name;
private String phone;
}

