1)Injecting Simple values (non spel and spel version)
non-spel version:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
">
<bean id="myCar" class="di.spel.Car">
<property name="year" value="2013"/>
<property name="make" value="Honda"/>
</bean>
</beans>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
">
<bean id="myCar" class="di.spel.Car">
<property name="year" value="2013"/>
<property name="make" value="Honda"/>
</bean>
</beans>
spel version:
<bean id="myCar" class="di.spel.Car">
<property name="year" value="#{2013}"/>
<property name="make" value="#{'Honda'}"/>
</bean>
<property name="year" value="#{2013}"/>
<property name="make" value="#{'Honda'}"/>
</bean>
Note that string value honda is in qoutes.
2)Injecting Reference values (non spel and spel version)
non-spel version:
<beans...>
<bean id="myCar" class="di.spel.Car">
<property name="year" value="2013"/>
<property name="make" value="Honda"/>
<property name="wheel" ref="myCarWheels"/>
</bean>
<bean id="myCarWheels" class="di.spel.Wheel">
<property name="company" value="Ceat"/>
</bean>
</beans>
<bean id="myCar" class="di.spel.Car">
<property name="year" value="2013"/>
<property name="make" value="Honda"/>
<property name="wheel" ref="myCarWheels"/>
</bean>
<bean id="myCarWheels" class="di.spel.Wheel">
<property name="company" value="Ceat"/>
</bean>
</beans>
spel version:
<beans ...>
<bean id="myCar" class="di.spel.Car">
<property name="year" value="#{2013}"/>
<property name="make" value="#{'Honda'}"/>
<property name="wheel" value="#{myCarWheels}"/>
</bean>
<bean id="myCarWheels" class="di.spel.Wheel">
<property name="company" value="#{'Ceat'}"/>
</bean>
</beans>
<bean id="myCar" class="di.spel.Car">
<property name="year" value="#{2013}"/>
<property name="make" value="#{'Honda'}"/>
<property name="wheel" value="#{myCarWheels}"/>
</bean>
<bean id="myCarWheels" class="di.spel.Wheel">
<property name="company" value="#{'Ceat'}"/>
</bean>
</beans>
Note that in
<property name="wheel" value="#{myCarWheels}"/>
we have used value attribute instead of ref attribute to refer id of another bean.
3)Setting value to property of some other bean (spel)
<beans...>
<bean id="myFirstCar" class="di.spel.Car">
<property name="year" value="#{2013}"/>
<property name="make" value="#{'Honda'}"/>
<property name="wheel" value="#{myCarWheels}"/>
</bean>
<bean id="mySecondCar" class="di.spel.Car">
<property name="year" value="#{myFirstCar.year}"/>
<property name="make" value="#{'BMW'}"/>
<property name="wheel" value="#{myCarWheels}"/>
</bean>
<bean id="myCarWheels" class="di.spel.Wheel">
<property name="company" value="#{'Ceat'}"/>
</bean>
</beans>
<bean id="myFirstCar" class="di.spel.Car">
<property name="year" value="#{2013}"/>
<property name="make" value="#{'Honda'}"/>
<property name="wheel" value="#{myCarWheels}"/>
</bean>
<bean id="mySecondCar" class="di.spel.Car">
<property name="year" value="#{myFirstCar.year}"/>
<property name="make" value="#{'BMW'}"/>
<property name="wheel" value="#{myCarWheels}"/>
</bean>
<bean id="myCarWheels" class="di.spel.Wheel">
<property name="company" value="#{'Ceat'}"/>
</bean>
</beans>
4)Predefined System Variables
Spring provides a predefined variable named systemProperties by which we can access environment Properties. For example,
<property name="userName" value="#{systemProperties['user.name']}"/>
5) T Operator()
Earlier in section 3 we have seen how to use a bean name in value attribute of property tag to provide value as:
<property name="year" value="#{myFirstCar.year}"/>
Here myFirstCar is the name of some another bean.
But suppose you want to use some other class name (say java.lang.Math class) to provide value for a property then we will write it as:
<property name="some_prop" value="#{java.lang.Math.PI}"/>
or
<property name="some_prop" value="#{Math.PI}"/>
In above statements we are setting a property named some_prop to the value of PI constant defined in java.lang.Math class.
But above statements will give errors. We cannot provide a class name directly in #{} notation.
For this purpose we have to use T() operator as:
<property name="some_prop" value="#{T(java.lang.Math).PI}"/>
or
<property name="some_prop" value="#{T(Math).PI}"/>
The T() operator in spel is used to represent an object of type java.lang.Class. In above example,
T(java.lang.Math) represents an object of type java.lang.Class which represents Math class.
Like constants we can also call static methods of a class using T() operator as:
<property name="some_prop" value="#{T(Math).abs(10)}"/>
Similarly we can use user defined classes in T() operator. Suppose we have a class which have some static variable like:
package example.spel;
public class A{
public static int y=10;
public int z=18;
...
}
public class A{
public static int y=10;
public int z=18;
...
}
then we can write it as:
<property name="some_prop" value="#{T(example.spel.A).y}"/>
But we cannot write as:
<property name="some_prop" value="#{T(example.spel.A).z}"/>
as z is not static.It will give error.
Also, if a class lies in java.lang package then we do not need to fully qualified it. But if it lies outside of it or it is a user defined class then we need to fully qualified it.
I would like to know your comments and if you liked the article then please share it on social networking buttons.
No comments:
Post a Comment