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

Aman Deep Gautam picture Aman Deep Gautam · Aug 31, 2014 · Viewed 41k times · Source

The following bean definition:

<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'

${domain} should evaluate to 'test'. What is wrong with the configuration?

Answer

Artem Bilan picture Artem Bilan · Aug 31, 2014

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:

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.

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 String too for ctor arg.

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

Try to imagine your SpEL without property-placeholders.