Spring 表达式语言(Spring EL)
本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL。SpEL是类似于OGNL和JSF EL的表达式语言,能够在运行时构建复杂表达式,存取对象属性、对象方法调用等。所有的SpEL都支持XML和Annotation两种方式,格式:#{ SpEL expression }
一、 第一个Spring EL例子—— HelloWorld Demo
这个例子将展示如何利用SpEL注入String、Integer、Bean到属性中。
1. Spring El的依赖包
首先在Maven的pom.xml中加入依赖包,这样会自动下载SpEL的依赖。
文件:pom.xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
</dependencies>
2. Spring Bean
接下来写两个简单的Bean,稍后会用SpEL注入value到属性中。
Item.java如下:
package com.lei.demo.el;
public class Item {
private String name;
private int total;
//getter and setter...
}
Customer.java如下:
package com.lei.demo.el;
public class Customer {
private Item item;
private String itemName;
@Override
public String toString() {
return "itemName=" +this.itemName+" "+"Item.total="+this.item.getTotal();
}
//getter and setter...
}
3. Spring EL——XML
SpEL格式为#{ SpEL expression },xml配置见下。
文件:Spring-EL.xml
<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.lei.demo.el.Item">
<property name="name" value="itemA" />
<property name="total" value="10" />
</bean>
<bean id="customerBean" class="com.lei.demo.el.Customer">
<property name="item" value="#{itemBean}" />
<property name="itemName" value="#{itemBean.name}" />
</bean>
</beans>
注解:
1. #{itemBean}——将itemBean注入到customerBean的item属性中。
2. #{itemBean.name}——将itemBean 的name属性,注入到customerBean的属性itemName中。
4. Spring EL——Annotation
SpEL的Annotation版本。
注意:要在Annotation中使用SpEL,必须要通过annotation注册组件。如果你在xml中注册了bean和在java class中定义了@Value,@Value在运行时将失败。
Item.java如下:
package com.lei.demo.el;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("itemBean")
public class Item {
@Value("itemA")//直接注入String
private String name;
@Value("10")//直接注入integer
private int total;
//getter and setter...
}
Customer.java如下:
package com.lei.demo.el;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("customerBean")
public class Customer {
@Value("#{itemBean}")
private Item item;
@Value("#{itemBean.name}")
private String itemName;
//getter and setter...
}
Xml中配置组件自动扫描:
<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.lei.demo.el" />
</beans>