在'org.springframework.beans.factory.config.BeanExpressionContext类型的对象上找不到字段或属性

Ama*_*tam 4 java spring spring-el

以下bean定义:

<bean id="client" factory-bean="builder"
    factory-method="withConfiguration">
    <constructor-arg type="java.lang.String"
        value="#{ ${domain} == 'prod' ? 
                Base.${domain}.${realm}.rpt : Base.${domain}.${realm}}" />
Run Code Online (Sandbox Code Playgroud)

失败,出现以下错误:

org.springframework.web.context.ContextLoader: Context initialization failed { org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'test' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'

$ {domain}应该评估为'test'.配置有什么问题?

Art*_*lan 7

你的property-placeholder结果必须被包装literal,如果你打算用另一个文字测试它,并且不要像你在表达式的其余部分那样使用它作为属性:

value="#{ '${domain}' == 'prod' ? 
            'Base.${domain}.${realm}.rpt' : 'Base.${domain}.${realm}'}"
Run Code Online (Sandbox Code Playgroud)

我接受了你的编辑.谢谢.

属性占位符在SpEL之前工作,因此,任何属性占位符结果都会成为一些SpEL部分,并且它必须是有效的.

我理解第一部分'${domain}' == 'prod',当你真的必须有一个PP结果的文字来比较它与另一个文字.你的SpEL的其余部分从一开始就不清楚,但是现在我发现它String对于ctor arg 也应该是一个.

否则,SpEL会尝试将其test视为一些评估上下文属性,我们在异常中看到.

试着想象你的SpEL没有属性占位符.