Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split value of property in application.properties

I want to split a value of property defined application.properties and use it as a value for another property.

Following are my properties in application.properties file

test.product.release.version=2003.1
test.product.release.year=${test.product.release.version}.split('.')[0]

I want value for property test.product.release.year as 2003

I tried split using ${test.product.release.version}.split('.')[0] but when I gets the property in my controller I still gets value as 2003.1.split('.')[0]

How can I get it as 2003 only?

like image 478
Sameer Avatar asked Oct 16 '25 02:10

Sameer


2 Answers

You can get the year in controller directly

@Value("#{'${test.product.release.version}'.split('[.]')[0]}")
private String year;

In the same way for version

@Value("#{'${test.product.release.version}'.split('[.]')[1]}")
private String version;

In the same way you can specify this expression in properties file also

test.product.release.version=2003.1
test.product.release.year="#{'${test.product.release.version}'.split('[.]')[0]}"
test.product.release.version="#{'${test.product.release.version}'.split('[.]')[1]}"

And then use @Value to read test.product.release.year

@Value("${test.product.release.year}")
private String value;
like image 194
Deadpool Avatar answered Oct 18 '25 15:10

Deadpool


For my perspective, you should look through a different point of view :

test.product.release.version.year=2003
test.product.release.version.sub=1
test.product.release.version=${test.product.release.version.year}.${test.product.release.version.sub}
like image 37
jpmottin Avatar answered Oct 18 '25 15:10

jpmottin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!