spring-boot - Spring Boot使用SpEL比较@Value中的字符串

标签 spring-boot spring-el

我想使用 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/

相关文章:

java - 无法使用配置的公钥验证 RSA 签名。签名长度不正确: got 255 but was expecting 256

java - Spring 集成 SLA 选项

java - org.springframework.beans.NotReadablePropertyException : Invalid property of bean class. Thymeleaf + SpringBoot

java - @Value 在 SpEL 中如何工作?

java - Spring SpEL 不适用于 <mvc :resources> location parameter

java - 找不到 ID 为 'org.springframework.boot' 的插件

Spring OAuth2 不会在表单登录时重定向回客户端

spring-security - @预授权: reference property in implementing class

spring - 将xml id的值注入(inject)spring bean

@Preauthorize 中的 Spring Boot 属性