Search code examples
springspring-bootapplication.properties

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?


Solution

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