Field or property cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext
Solution 1
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.
Solution 2
Because I did not add in the missing apostrophes:
th:if = "${user.name} == null";
The correct way is:
th:if = "'${user.name}' == null";
Aman Deep Gautam
Software developer at Amazon India. Graduated form IIT Hyderabad in 2013 with honors in Computer Science.
Updated on January 19, 2020Comments
-
Aman Deep Gautam over 4 years
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?
-
Aman Deep Gautam over 9 yearsSorry could not understand what you meant by : "and don't use it as a property as you in the rest of your expression". I want this functionality:
if domain has value "prod", then use "Base.prod.US.rpt" as constructor argument otherwise use "Base.test.US" as argument
. Note that${realm}
evaluates to US. -
Aman Deep Gautam over 9 yearsI have edited the answer to be completely correct. jsut that I did not understand that particular portion.
-
mmgross about 7 yearsThis looks like it should be a comment to an answer to a different question.