Home > Software design >  Reading configuration properties in SpEL
Reading configuration properties in SpEL

Time:03-04

I have a com.mycompany.condition property in my application.yml or dev-tools. At the moment I'm injecting it on some services with

@Value("${com.mycompany.condition: false}") 
boolean condition;

and then using as a condition for @Cacheable(condition = "#root.target.isCondition()"). Is it possible to not inject the value, but using directly a SpEL to read the property? Using @Cacheable(condition = "${com.mycompany.condition}") gives the error

2022-03-03 15:17:11.681  WARN [...] 3044 --- [...] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.expression.spel.SpelParseException: EL1041E: After parsing a valid expression, there is still more data in the expression: 'lcurly({)']

while using @Cacheable(condition = "#{${com.mycompany.condition}}") gives

2022-03-03 15:24:13.194  WARN [...] 19196 --- [...] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.expression.spel.SpelParseException: Expression [#{${com.mycompany.condition}}] @1: EL1043E: Unexpected token. Expected 'identifier' but was 'lcurly({)']

CodePudding user response:

Try this:

@Cacheable(condition = "@environment.getProperty('com.mycompany.condition')")
  • Related