简体   繁体   English

SpEL 不解析简单的 ${spring.profiles.active} 表达式

[英]SpEL does not parse simple ${spring.profiles.active} expression

I am trying to learn SpEL, but it is showing behaviours that are not what is shown in the tutorials.我正在尝试学习 SpEL,但它显示了教程中未显示的行为。

I have a method that calls a simple expression below, trying to get the value of ${spring.profiles.active} , but somehow the code keeps failing.我有一个方法调用下面的简单表达式,试图获取${spring.profiles.active}的值,但不知何故代码一直失败。

public void testSpel() {
    ExpressionParser parser = new SpelExpressionParser();
    Expression exp = parser.parseExpression("${spring.profiles.active}");
    String result = exp.getValue(String.class);
    log.info(result);
}

I have tried many variations, such as #{${spring.profiles.active}} , #{'${spring.profiles.active}'} , etc. However it always return an SpEL error:我尝试了许多变体,例如#{${spring.profiles.active}}#{'${spring.profiles.active}'}等。但是它总是返回 SpEL 错误:

For case of ${spring.profiles.active} :对于${spring.profiles.active}

Caused by: org.springframework.expression.spel.SpelParseException: EL1041E: After parsing a valid expression, there is still more data in the expression: 'lcurly({)'
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.doParseExpression(InternalSpelExpressionParser.java:135) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:61) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:33) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpression(TemplateAwareExpressionParser.java:52) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpression(TemplateAwareExpressionParser.java:43) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]

For cases of #{${spring.profiles.active}} , #{'${spring.profiles.active}'}对于#{${spring.profiles.active}}#{'${spring.profiles.active}'}

Caused by: org.springframework.expression.spel.SpelParseException: Expression [#{'${spring.profiles.active}'}] @1: EL1043E: Unexpected token. Expected 'identifier' but was 'lcurly({)'
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.internalException(InternalSpelExpressionParser.java:1044) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatToken(InternalSpelExpressionParser.java:926) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.maybeEatFunctionOrVar(InternalSpelExpressionParser.java:423) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatStartNode(InternalSpelExpressionParser.java:512) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatPrimaryExpression(InternalSpelExpressionParser.java:351) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatUnaryExpression(InternalSpelExpressionParser.java:345) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatPowerIncDecExpression(InternalSpelExpressionParser.java:304) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatProductExpression(InternalSpelExpressionParser.java:282) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatSumExpression(InternalSpelExpressionParser.java:264) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatRelationalExpression(InternalSpelExpressionParser.java:218) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatLogicalAndExpression(InternalSpelExpressionParser.java:205) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatLogicalOrExpression(InternalSpelExpressionParser.java:192) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatExpression(InternalSpelExpressionParser.java:153) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.doParseExpression(InternalSpelExpressionParser.java:131) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:61) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:33) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpression(TemplateAwareExpressionParser.java:52) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpression(TemplateAwareExpressionParser.java:43) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]

You are mixing concerns.您正在混合担忧。

SpEL doesn't resolve property placeholders, a ConfigurableBeanFactory resolves placeholders, SpEL evaluates Spel Expressions. SpEL 不解析属性占位符, ConfigurableBeanFactory解析占位符,SpEL 评估 Spel 表达式。

If you have a SpEL expression that contains a property placeholder, you need to resolve it first.如果您的 SpEL 表达式包含属性占位符,则需要先解析它。

See here for an example.有关示例,请参见此处

private Object resolveExpression(String value) {
    return this.resolver.evaluate(resolve(value), this.expressionContext);
}

private String resolve(String value) {
    if (this.beanFactory != null && this.beanFactory instanceof ConfigurableBeanFactory) {
        return ((ConfigurableBeanFactory) this.beanFactory).resolveEmbeddedValue(value);
    }
    return value;
}

and you will need to quote '...' the property placeholder so SpEL treats it as a literal.并且您需要引用'...'属性占位符,以便 SpEL 将其视为文字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 spring.profiles.active 只读取部分配置文件? - Why does spring.profiles.active only read some profiles? spring.profiles.active 不兑现 - spring.profiles.active not honoured spring.profiles.active 在springboot应用程序中不起作用 - spring.profiles.active is not working in springboot application "无法解析值“classpath:\/ldap-${spring.profiles.active}.properties”中的占位符“spring.profiles.active”" - Could not resolve placeholder 'spring.profiles.active' in value "classpath:/ldap-${spring.profiles.active}.properties" Spring 引导获取 static 块中的 spring.profiles.active 值 - Spring Boot get spring.profiles.active value in a static block 春季启动:spring.profiles.active = dev / test / prod - Spring Boot: spring.profiles.active=dev/test/prod Spring 引导 2.5.6 捕获 22 与 spring.profiles.active - Spring Boot 2.5.6 catch 22 with spring.profiles.active 如何在Websphere Liberty 8.5中设置spring.profiles.active - how to setup spring.profiles.active in websphere liberty 8.5 通过 spring.profiles.active 覆盖默认应用程序属性 - Override default application properties by spring.profiles.active Gradle bootRun 不适用于 SPRING_PROFILES_ACTIVE 但适用于 spring.profiles.active - Gradle bootRun not working with SPRING_PROFILES_ACTIVE but works with spring.profiles.active
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM