Use PropertyPlaceholderConfigurer with a list

I use PropertyPlaceholderConfigurer to match String values ​​from a properties file and it works fine.

My question is, can I set something in my properties file: MyList = A, B, C

And then match it to the list

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

When I try, it puts all the values ​​in one place in the list. Is there any way to tell this to break it into a list on ","?

+6
source share
2 answers

Using Spring Expression Language:

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

If myList=A,B,C in the properties file this will result in myList (in code) with values A , B and C

+12
source

Take a look at sections 6.5.3 (Inline Lists) and 6.5.4 (Array Construction) for this link to Spring Expression Language Features .

From the link:

Lists can be expressed directly in an expression using {} notation.

 // evaluates to a Java list containing the four numbers List numbers = (List) parser.parseExpression("{1,2,3,4}").getValue(context); List listOfLists = (List) parser.parseExpression("{{'a','b'},{'x','y'}}").getValue(context); 

{} in itself means an empty list. For performance reasons, if the list itself consists of fixed literals, then a constant list is created to represent the expression, and not to create a new list at each evaluation.

I'm not sure if this will work exactly the way you would like with the @Value annotation combined with the PropertyPlaceholderConfigurer , but it's worth a look.

0
source

Source: https://habr.com/ru/post/910075/


All Articles