View Full Version : How to supply rootcontext and dictionary with the expression='<anExpression>' syntax
john.hui
02-05-2007, 10:44 AM
Hi,
I am exploring the use of the expression='<anExpression>' syntax (eg. expression='@(someContextName:anObjectName)').
I have three questions on this.
1) How do I reference an object declared on the current xml file and retrieve a property from it knowing that while I am retrieving the property, the current xml file is not yet registered with the Spring Context Registry? Is using the PropertyRetrievingFactoryObject the only way to do this (ie cannot be done via expressions).
2) How do I supply a rootcontext to this expression? For example, can I have a syntax such as this?
expression='#root[0]'
3) Finally, how do I supply a variable dictionary to this expression? For example, can I have this?
expression='#function(#root[0])'
Thanks for your advice.
John Hui
Erich Eichinger
02-05-2007, 04:49 PM
Hi John,
How do I reference an object declared on the current xml file and retrieve a property from it knowing that while I am retrieving the property, the current xml file is not yet registered with the Spring Context Registry?
You are right: It is impossible to reference another object within an expression of a singleton-definition. There's only one way I can think of now: Marking the <object>-definition as lazy-init="true"
How do I supply a rootcontext to this expression?
...
Finally, how do I supply a variable dictionary to this expression?
I'm note sure, if I understand what you mean. A rootcontext as passed to an Expression by e.g. calling ExpressionEvaluator.Evaluate( myObject, "expression") or parsedExpression.GetValue( myObject) . In this case "myObject" will be the rootContext.
cheers,
Erich
Aleks Seovic
02-06-2007, 02:56 PM
Hi John,
It seems like you are determined to make me work on the expression language improvements and bug fixes for a forseeable future ;)
This was on my to-do list for a while, and I finally managed to get to it. I committed the code that allows you to supply both the root context and variables to an expression within object definition. In order to do so, you need to use expression element -- it is not possible to specify context and variables using expression attribute for obvious reasons.
Unfortunately, I had to make a breaking change to support this: expression string is now specified using value attribute of the expression element, instead of as element text.
In order to specify context, you need to set Context property of the expression element:
<expression value="ToString('m')">
<property name="Context" expression="date('1974-08-24')"/>
</expression>
As you can see, you can use another expression to specify property value, or you can specify literal value or an object reference (which solves reference problem you were having without having to use lazy-init). Basically, you set Context property of the expression just like any other property within Spring-managed objects.
Similarily, you can provide variables by setting Variables property:
<expression value="@[WebMethod(Description = #desc)]">
<property name="Variables">
<dictionary>
<entry key="desc" value="My Second Web Method"/>
</dictionary>
</property>
</expression>
Again, all you need to do is configure a Variables property as a dictionary, and you can use references, expressions or literal values to configure dictionary elements, as usual.
You can take a look at expressions.xml test context within Spring.Core.Tests and related tests for more details and usage examples.
Enjoy,
Aleks
john.hui
02-07-2007, 04:35 PM
Hi Aleks,
That is simply wonderful! I will take a look at it.
Thanks again.
John
vBulletin® v3.7.3, Copyright ©2000-2009, Jelsoft Enterprises Ltd.