Skip to content

Instantly share code, notes, and snippets.

@kruttik-lab49
Last active August 3, 2021 17:47
Show Gist options
  • Select an option

  • Save kruttik-lab49/db9e0292ceed1e4cd1fb67901774b033 to your computer and use it in GitHub Desktop.

Select an option

Save kruttik-lab49/db9e0292ceed1e4cd1fb67901774b033 to your computer and use it in GitHub Desktop.
TWL-Lab49/Spring - Injecting properties as a list with default value using value annotation

Suppose you want to inject a list of values in your bean. You can do-

#prop file
mylist=x,y,z

#bean
@Value("${myList}")
String[] myList;

But what if you want a List<String> instead of String[]. Sure, you can use the following-

#prop file
mylist=x,y,z

#bean
@Value("#{'${myList}'.split(',')}")
List<String> myList;

or event better/cleaner-

#prop file
mylist={'x','y','z'}

#bean
@Value("#{${myList}}")
List<String> myList;

But what if you want to add a default value? Here's how-

#prop file
mylist={'x','y','z'}

#bean
@Value("#{${myList:''}}")
List<String> myList;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment