简体   繁体   中英

Spring Boot: Read list of values from properties file

I have properties file with below list of values

prop.myVariable=v1,v2,v3

i tried to read them using spring boot as below:

@Value("#{'${prop.myVariable}'.split(',')}")
public static List<String> allowList;

When i was trying to execute it, it's not able to read and getting java.lang.NullPointerException

Static members are initialized before loading the properties. To workaround this issue, use setter injection:

public static List<String> allowList;

@Value("#{'${prop.myVariable}'.split(',')}")
public void setAllowList(List<String> list) {
    allowList = list;
}

I ended up doing this:

@Value("#{'${prop.myVariable}'.split(',')}")
private List<String> allowedCharacteristics;

As I can see you are using the following code, why would you even want to save the properties to a "list"

List<String> allowList;
@Value("#{'${prop.myVariable}'.split(',')}") 
public List<String> setAllowList(List<String> list) { 
  this.list= list;
 } 
String Chars = myProperties.getConfigValue("prop.myVariable"); 
List<String> allowedCharacteristics = setCharacteristics(Chars); 

You have to store the properties to "allowlist", use the following code below

@Value("#{'${prop.myVariable}'.split(',')}")
public void setAllowList(List<String> list) {
    allowList = list;
}

please follow this thread -> How to access a value defined in the application.properties file in Spring Boot if you have to specifically use "getConfigValue()".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM