4

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?

Kapil
  • 817
  • 2
  • 13
  • 25
Sameer
  • 101
  • 1
  • 12

4 Answers4

5

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;
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • `test.product.release.version="#{'${test.product.release.version}'.split('[.]')[1]}" ` is still gives me value as `"#{'V2003.1'.split('[.]')[0]}"` I am fetching it through [AbstractEnvironment](https://docs.spring.io/spring/docs/4.3.21.RELEASE/javadoc-api/org/springframework/core/env/AbstractEnvironment.html) #getProperty – Sameer Jul 30 '19 at 06:36
  • why don't you use `@Value` ? – Ryuzaki L Jul 30 '19 at 06:38
  • because you mentioned in answer _In the same way you can specify this expression in properties file also_ – Sameer Jul 30 '19 at 06:40
  • check my updated answer it works for me on both `yml` and `properties` file @Sameer – Ryuzaki L Jul 30 '19 at 06:42
  • and also i tested by using `env.getProperty("test.product.release.year"` it gives me `"#{'2003.1'.split('[.]')[0]}"`, so just use `@Value` instead of `env` – Ryuzaki L Jul 30 '19 at 06:45
  • And also you can ask different question why it is returning expression instead of value while using `Env` sorry i don't have that information @Sameer – Ryuzaki L Jul 30 '19 at 06:59
1

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}
jpmottin
  • 2,717
  • 28
  • 25
  • I can not use two properties because value for property `test.product.release.version.year` is not controlled by installer which I don't want to touch – Sameer Jul 30 '19 at 06:12
1

Use the following expression. This will work.

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

// this will give you year part.
@Value("#{\"${test.product.release.version}\".split(\"\\.\")[0]}")
private String year;
YoManTaMero
  • 391
  • 4
  • 10
1

Since you are going to have a single property and derive another out of the first one, it would be better to do it as below:

application.properties

test.product.release.version=2003.1

Config class

@Value("${test.product.release.version}")
private String version;
@Value("#{'${test.product.release.version}'.split('\\.')[0]}")
private String year;
Madhu Bhat
  • 13,559
  • 2
  • 38
  • 54
  • If the version is not configured `{'${test.product.release.version}'.split('\\.')[0]}` will break. – Jobin Jul 30 '19 at 06:18
  • @JobinJoseph having `test.product.release.version` defined is a prerequisite as part of the question itself. It is obvious that if it is not defined and used with @Value, the app wouldn't start at all. – Madhu Bhat Jul 30 '19 at 06:21
  • So it is better to use `@ConfigurationProperties` see here https://stackoverflow.com/a/47178928/2893693 – Jobin Jul 30 '19 at 06:24
  • @JobinJoseph `@ConfigurationProperties` is just another way of using properties, but that is not the point of the question. – Madhu Bhat Jul 30 '19 at 06:28