我想使用 SpEL
评估 Spring 轮廓是否是本地的
我尝试了以下但无法正确获取
@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?
我也尝试了以下方法,但没有运气:
@Value("#{spring.profiles.active.equals('local')}")
private boolean isLocal;
@Value("#{spring.profiles.active eq 'local'}")
private boolean isLocal;
不可能做这样的事情吗?还是我做错了?
最佳答案
您需要引用属性占位符解析的结果:
@Value("#{'${spring.profiles.active}' == 'local'}")
否则,SpEL 会尝试解析它;因此
Property or field 'local' cannot be found
关于spring-boot - Spring Boot使用SpEL比较@Value中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57321758/