1

I'm facing this kind of issue now, that I must give two conditions in one @ConditionalOnProperty which is class level.

I have a bean, say

@RestController
@RequestMapping("/blablabla")
@ConditionalOnProperty(prefix = "my.property.value1", name = "enabled", havingValue = "true")
public class MyBean{}

now there is a requirement that MyBean must be loaded based on two conditions, one is my.property.value1.enabled:true and the second is my.property.value2:true (both coming from application.yml file)

Now my question is there any way of doing this in one @ConditionalOnProperty annotation, if yes, so please paste here the working example. Thanks in advance.

M A
  • 71,713
  • 13
  • 134
  • 174
Hayk Mkhitaryan
  • 388
  • 9
  • 26

3 Answers3

2

For simple boolean properties you could use @ConditionalOnProperty:

@ConditionalOnProperty({"my.property.value1", "my.property.value2"})

application.yml:

my:
  property:
    value1: true
    value2: false
lkatiforis
  • 5,703
  • 2
  • 16
  • 35
1

Yes, you can do the following:

@ConditionalOnExpression("${my.property.value1.enabled} and ${my.property.value2}")
João Dias
  • 16,277
  • 6
  • 33
  • 45
1

The @ConditionalProperty name element can support an array:

@ConditionalOnProperty(prefix = "my.property", name = {"value1.enabled", "value2.enabled"}, havingValue = "true")

If the two properties are compared to different values, then you can leverage a Spring expression:

@ConditionalOnExpression("${my.property.value1.enabled} == true and ${my.property.value1.enabled} == true")
M A
  • 71,713
  • 13
  • 134
  • 174