hades
hades

Reputation: 4696

Spring Boot Compare String in @Value using SpEL

I want to evaluate if a spring profile is local using SpEL

I tried the following but can't get it correctly

@Value("#{spring.profiles.active == 'local'}") 
private boolean isLocal;
//Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'spring' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?

@Value("#{${spring.profiles.active} == 'local'}") 
private boolean isLocal;
//Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'local' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?

I also tried the following, but no luck:

@Value("#{spring.profiles.active.equals('local')}") 
private boolean isLocal; 

@Value("#{spring.profiles.active eq 'local'}") 
private boolean isLocal; 

Is it not possible to do something like this? Or i did it wrongly?

Upvotes: 7

Views: 8353

Answers (1)

Gary Russell
Gary Russell

Reputation: 174504

You need to quote the result of the property placeholder resolution:

@Value("#{'${spring.profiles.active}' == 'local'}") 

Otherwise, SpEL tries to parse it; hence

Property or field 'local' cannot be found

Upvotes: 19

Related Questions