作为spring的基础模块之一,spring表达式语言模块(Spring Expression Language,简称SpEL)在运行时提供了查询和操作一个对象图的强大的表达式语言。
Spring EL既可以通过XML被配置,也可以通过注解来进行配置。下面通过简单的例子来示范如何通过两种不同的方式配置SpEL从而注入String、整型和javabean数据。
Spring EL依赖
项目通过maven进行管理,在maven的pom.xml配置文件中声明spring的核心依赖,maven将自动下载依赖包。(若不采用maven进行项目管理,可直接下载并添加jar包至buildpath)
pom.xml:
<properties>
<spring.version>3.1.4.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring 3 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>2. Spring Beans
定义两个简单的bean,后面将分别通过XML方式和注解方式用SpEL为bean的属性注入值
public class Customer {
private Item item;
private String itemName;
}
public class Item {
private String name;
private int qty;
}3. 通过XML方式配置Spring EL
SpEL表达式的格式如下#{SpEL表达式},XML bean定义文件中配置SpEL,示例如下
<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.mkyong.core.Item">
<property name="name" value="itemA" />
<property name="qty" value="10" />
</bean>
<bean id="customerBean" class="com.mkyong.core.Customer">
<property name="item" value="#{itemBean}" />
<property name="itemName" value="#{itemBean.name}" />
</bean>
</beans>#{itemBean}-将itemBean注入到customerBean的item属性中
#{itemBean.name}-将itemBean的name属性注入到customerBean的itemName属性中
4. 通过注解配置Spring EL
注意:要通过注解配置SpEL,必须将bean通过注解方式注册。如果在XML配置文件中注册bean,同时在java类中定义@Value,@Value将会失效。
注解方式配置bean如下:
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;
//...}
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;
}
//...}在配置文件中允许自动扫描:
<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/> </beans>
注解方式中,通过@Value定义springEL。通过上述的配置,string值和整型值直接被注入到itemBean的属性中,然后itemBean被注入到customerBean的属性中。
5. 输出
运行下述代码,无论是XML方式还是注解方式都会输出同样的结果
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");
Customer obj = (Customer) context.getBean("customerBean");
System.out.println(obj);
}}输出:
Customer [item=Item [name=itemA, qty=10], itemName=itemA]
类似JSF EL,除了直接设置值之外,SpEL还支持其他多种表达式,包括关系表达式、正则表达式、数组、列表等等。
参考
http://www.mkyong.com/spring3/spring-el-hello-world-example/
http://examples.javacodegeeks.com/enterprise-java/spring/spel/spring-expression-language-example/
本文出自 “木木在路上” 博客,请务必保留此出处http://mumufairy.blog.51cto.com/6184508/1694958
spring表达式语言(SpEL)简述及Hello World示例
原文:http://mumufairy.blog.51cto.com/6184508/1694958