Spring 3.0 introduced a powerful expression language known as Spring expression language, or Spring EL.
The Spring Expression Language, available via XML or annotation, is evaluated or executed during the bean creation time.
In the following code shows how to use Spring Expression Language to inject String, integer and bean into property, both in XML and annotation.
In order to use Spring Expression Language we need to add the following jars dependency to the pom.xml file.
... <properties> <spring.version>3.0.5.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependencies> ...
The following code defines two Java beans, later we will use Spring Expression Language to inject values into property in XML and annotation.
Server Java Bean.
package com.java2s.common; public class Server { private Item item; private String itemName; public Item getItem() { return item; } public void setItem(Item item) { this.item = item; } public String getItemName() { return itemName; } public void setItemName(String itemName) { this.itemName = itemName; } @Override public String toString() { return "Server [item=" + item + ", itemName=" + itemName + "]"; } }
Item Java Bean.
package com.java2s.common; public class Item { private String name; private int qty; public String getName() { return name; } @Override public String toString() { return "Item [name=" + name + ", qty=" + qty + "]"; } public int getQty() { return qty; } public void setQty(int qty) { this.qty = qty; } public void setName(String name) { this.name = name; } }
The following code shows how to use Spring EL in XML.
First, we defined a Java Bean Item in xml and set its property value. We set the name to have itemA value and set 10 as the qty value.
<bean id="itemBean" class="com.java2s.common.Item"> <property name="name" value="itemA" /> <property name="qty" value="10" /> </bean>
Then, we created the Server bean in XML by reusing the value from Item Bean.
The Spring Expression Language is enclosed with #{ expression }.
The following code references the value from Item bean.
It assigns the itemBean to item and itemBean.name to itemName.
Value of itemBean.name
is itemA.
<bean id="myServer" class="com.java2s.common.Server"> <property name="item" value="#{itemBean}" /> <property name="itemName" value="#{itemBean.name}" /> </bean>
Full configuration file.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="itemBean" class="com.java2s.common.Item"> <property name="name" value="itemA" /> <property name="qty" value="10" /> </bean> <bean id="myServer" class="com.java2s.common.Server"> <property name="item" value="#{itemBean}" /> <property name="itemName" value="#{itemBean.name}" /> </bean> </beans>
The following example shows how to use Spring Expression Language in annotation.
First, we define a Java Bean Item and mark it with Component
annotation.
For its properties we use the Value
annotation to assign them values
package com.java2s.common; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("itemBean") public class Item { @Value("itemA") //inject String directly private String name; @Value("10") //inject interger directly private int qty; public String getName() { return name; } @Override public String toString() { return "Item [name=" + name + ", qty=" + qty + "]"; } public int getQty() { return qty; } public void setQty(int qty) { this.qty = qty; } public void setName(String name) { this.name = name; } }
Then, we define a Server Java bean and also mark it with Component
annotation.
When defining Server's properties we use the values defined Value
annotations from Item bean.
package com.java2s.common; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("myServer") public class Server { @Value("#{itemBean}") private Item item; @Value("#{itemBean.name}") private String itemName; public Item getItem() { return item; } public void setItem(Item item) { this.item = item; } public String getItemName() { return itemName; } public void setItemName(String itemName) { this.itemName = itemName; } @Override public String toString() { return "Server [item=" + item + ", itemName=" + itemName + "]"; } }
Finally, we have to enable auto component scanning in the xml file.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.java2s.common" /> </beans>
The following code shows how to run the code above.
package com.java2s.common; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml"); Server obj = (Server) context.getBean("myServer"); System.out.println(obj); } }
The code above generates the following result.
The following code shows how to use Collections in Spring Expression Language.
First, define a Java bean with collections in it.
package com.java2s.common; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.stereotype.Component; @Component("testBean") public class Test { private Map<String, String> map; private List<String> list; public Test() { map = new HashMap<String, String>(); map.put("MapA", "This is A"); map.put("MapB", "This is B"); map.put("MapC", "This is C"); list = new ArrayList<String>(); list.add("List0"); list.add("List1"); list.add("List2"); } }
Then, use the collection in the expression language.
package com.java2s.common; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("customerBean") public class Customer { @Value("#{testBean.map['MapA']}") private String mapA; @Value("#{testBean.list[0]}") private String list; }
Use the same settings in the xml configuration file.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="customerBean" class="com.java2s.common.Customer"> <property name="mapA" value="#{testBean.map['MapA']}" /> <property name="list" value="#{testBean.list[0]}" /> </bean> <bean id="testBean" class="com.java2s.common.Test" /> </beans>