I have the following problem, I have a certain configuration class in spring boot which contains beans, which I would like to be created only if a certain property which has a list of values, contains a certain value.
now the configuration class looks something like this:
@ConditionalOnExpression("#{'${conditions.values.options}'.contains('ScenarioOne')}")
ConfigurationForScenarioOne{
@Bean
public StudentBean getStudentBean(){
...
}
}
In the properties file I would have something like so:
conditions.values.options=${CONDITIONS_VALUES_OPTIONS}
Then provide the value of CONDITIONS_VALUES_OPTIONS
at runtime like so:
-DCONDITIONS_VALUES_OPTIONS=ScenarioOne,ScenarioTwo,ScenarioFive
The reason why I want to do this is to be able to deploy the app and have different beans be in use depending on the value given at runtime.
I will have several of these Configuration classes that will be created based on which ones of these properties are passed.
TO achieve this I was trying to rely on Spring Expression language to do the following:
1-Read the property conditions.values.options
, then convert it to a list, then verify if the list contained the desired configuration string.
So far I have tried several expressions including:
@ConditionalOnExpression("#{'${conditions.values.options}'.contains('ScenarioOne')}")
and other similar expressions, with no luck. Can someone help me see what I am missing here?
I was able to get the desired behavior using this:
So dissecting this a little bit in the hopes to help others who may come across similar problems: It seems that spring reads comma separated properties as an Array, this is bogus because the following can be done using spring boot:
The following two statements are valid:
OR
now the expression:
will throw an error saying that the Array class does not contain a method called '.contains()' hence this is why I had to resort to use some method execution:
to read the comma separated values, convert the String array into a List and then perform the '.contains(..)' method invocation.
I wish there was a simpler way to do it, to have to do a method invocation in there seems like overkill.
I am open to sugestions