kanni
kanni

Reputation: 73

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

Upvotes: 0

Views: 1228

Answers (3)

kanni
kanni

Reputation: 73

I ended up doing this:

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

Upvotes: 0

Bhanu Prakash Alapati
Bhanu Prakash Alapati

Reputation: 146

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()".

Upvotes: 0

Aniket Sahrawat
Aniket Sahrawat

Reputation: 12937

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;
}

Upvotes: 1

Related Questions