Search code examples
javaspringspring-el

Logical operators not working as expected in SpEL


I have a usecase where I have two boolean properties, based on which I need to derive a value of instance variable. Below is the code snippet:

@Value("${propFlag1:false}")
private Boolean flag1;

@Value("${propFlag2:false}")
private Boolean flag2;

@Value("${propFlag2:false && propFlag1:false}")
private Boolean flag3;

@Value("${propFlag1:false && propFlag2:false}")
private Boolean flag4;

Here, propFlag1 and propFlag2 are flags from properties file, having values true and false respectively.
Values are being injected correctly for flag1 and flag2, but for flag3 and flag4, the values injected are false and true respectively instead of false for both.
So, when I try using &&, it is not ANDing two values, rather it is injecting the value of first variable itself. What am I doing wrong here?

PS : I am new to SpEL


Solution

  • According to SpEL documentation, this should work:

    @Value("#{${propFlag2:false} and ${propFlag1:false}}")
    private Boolean flag3;
    
    @Value("#{${propFlag1:false} and ${propFlag2:false}}")
    private Boolean flag4;