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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25593897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:43:29  来源:igfitidea点击:

Field or property cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext

javaspringspring-el

提问by Aman Deep Gautam

The following bean definition:

以下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}}" />

fails with the following error:

失败并出现以下错误:

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'

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}should evaluate to 'test'. What is wrong with the configuration?

${domain}应评估为“测试”。配置有什么问题?

采纳答案by Artem Bilan

your property-placeholder result has to be wrapped to literal, if you are going to test it against another literal and don't use it as a property as you in the rest of your expression:

您的属性占位符结果必须包装为literal,如果您要针对另一个文字对其进行测试并且不要像在表达式的其余部分那样将其用作属性:

value="#{ '${domain}' == 'prod' ? 
            'Base.${domain}.${realm}.rpt' : 'Base.${domain}.${realm}'}"

I've accepted your edit. Thanks.

我已接受您的编辑。谢谢。

The property-placeholder works before SpEL, so, any property-placeholder result becomes some SpEL part and it really has to be valid one.

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

I understand the first part '${domain}' == 'prod', when you really must have a literal for the PP result to compare it with another literal. The rest of your SpEL hasn't been clear for me from the beginning, but now I see that it should be a Stringtoo for ctor arg.

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

Otherwise SpEL tries to treat testas some evaluation context property, that we see in the exception.

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

Try to imagine your SpEL without property-placeholders.

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

回答by jalon

Because I did not add in the missing apostrophes:

因为我没有添加缺少的撇号:

th:if = "${user.name} == null";

The correct way is:

正确的方法是:

th:if = "'${user.name}' == null";

回答by Nirbhay Rana

For boolean field use '' like below

对于布尔字段使用 '' 如下所示

${'offer.isActive'}