View RSS Feed

Spring Framework

More Simple SpEL Expressions

Rate this Entry
by , 11-16-2011 at 04:48 PM (1885 Views)
Another basic thing that a SpEL expression can do is to reference another bean by its ID. With this functionality it is possible to use SpEL to wire one bean into another beanís property by using the bean ID as the SpEL expression:

Java Code:
<property name="engine" value="#{hybrid}"/>
As you can see, weíre using SpEL to wire the bean whose ID is "hybrid" into an engine property. Now it is possible to do this without SpEL, and you would get the same outcome. Now letís look at some of the techniques you can use to take advantage of being able to wire bean references with SpEL. Consider how you can use a bean reference to access the properties of the bean in a SpEL expression.
If you want to configure a new Engine bean whose ID is priusEngine. And in this case, depending on the location, the car will use either gas or electricity to recharge/refuel. When you configure the priusEngine bean, you can use SpEL to determine what type of fueling is used in the following manner:

Java Code:
<bean id="priusEngine" class="com.acme.springexamples.domain.Engine">
<property name="fuel" value="#{prius.location()}" /> 
</bean>
The expression passed into priusEngineís fuel property is made up of two parts.
The first part refers to the prius bean by its ID and the second part refers to the location attribute of the prius bean. By wiring the priusEngineís beanís fuel property this way, itís effectively as if you programmatically performed the following Java code:

Java Code:
PriusEngine priusEngine = new PriusEngine(); priusEngine.setFuel(prius.getLocation());
So in this case, we are using SpEL to create an expression using the beanís properties. Referencing a beanís properties isnít the only thing you can do with a bean. You could also invoke a method. For example, imagine that you have a GPSSelector bean which has a selectGPS() method on it that returns the the GPS provider to be used. In that case, PriusEngine could swap to whatever GPS provider it believes provides the most accurate or cost effective GPS locations:

Java Code:
<property name="gps" value="#{gpsSelector.selectGPS()}"/>
If selectGPS() were to return null, then youíd get a NullPointerException as the SpEL expression is being evaluated. The means to avoid a NullPointerException in SpEL is to use the null-safe accessor:

Java Code:
<property name="gps" value="#{gpsSelector.selectGPS()?.getGPS()}"/>
Note how by using ?. operator, before the dot (.) in accessing the getGPS() method, ensures that the item to its left isnít null before accessing the thing to its right. Therefore after the selectGPS() were to return a null, then SpEL wouldnít invoke getGPS() on it. In the next tip, weíll need to see how to work with types in SpEL.

Submit "More Simple SpEL Expressions" to Facebook Submit "More Simple SpEL Expressions" to Digg Submit "More Simple SpEL Expressions" to del.icio.us Submit "More Simple SpEL Expressions" to StumbleUpon Submit "More Simple SpEL Expressions" to Google

Comments