user
user

Reputation: 4840

How to bind @Value with few properties?

My application.yml contains:

spring: 
   someproperty: value1, value2

How to bind few values to one variable using @Value? Should I bind it to list or how?

Upvotes: 0

Views: 53

Answers (2)

lopushen
lopushen

Reputation: 1147

Use spring expression language this way

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

Use List and split the comma separated values with split() method like it is done here https://www.mkyong.com/spring/spring-value-import-a-list-from-properties-file/

To pass the value to the app add the properties to the startup script:
When launching the app simply add this to the startup script -Dspring.someproperty=value1,value2 , e.g.

java -jar yourapp.jar -Dspring.someproperty=value1,value2

Upvotes: 2

Zubair Nabi
Zubair Nabi

Reputation: 1056

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

Upvotes: 1

Related Questions