Spring EL hello world example
The Spring EL is similar with OGNL and JSF EL, and evaluated or executed during the bean creation time. In addition, all Spring expressions are available via XML or annotation.
In this tutorial, we show you how to use Spring Expression Language(SpEL), to inject String
, integer
and bean into property, both in XML and annotation.
1. Spring EL Dependency
Declares the core Spring jars in Maven pom.xml
file, it will download the Spring EL dependencies automatically.
File : pom.xml
<properties>
<spring.version>3.0.5.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
Two simple beans, later use SpEL to inject values into property, in XML and annotation.
package com.mkyong.core;
public class Customer {
private Item item;
private String itemName;
}
package com.mkyong.core;
public class Item {
private String name;
private int qty;
}
3. Spring EL in XML
The SpEL are enclosed with #{ SpEL expression }
, see following example in XML bean definition 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.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}
– inject “itemBean
” into “customerBean
” bean’s “item
” property.#{itemBean.name}
– inject “itemBean
”‘s “name
” property into “customerBean
” bean’s “itemName
” property.
4. Spring EL in Annotation
See equivalent version in annotation mode.
Note
To use SpEL in annotation, you must register your component via annotation. If you register your bean in XML and define@Value
in Java class, the@Value
will failed to execute.
package com.mkyong.core;
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;
//...
}
package com.mkyong.core;
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;
}
//...
}
Enable auto component scanning.
<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.mkyong.core" />
</beans>
In annotation mode, you use @Value
to define Spring EL. In this case, you inject a String
and Integer
value directly into the “itemBean
“, and later inject the “itemBean
” into “customerBean
” property.
5. Output
Run it, both SpEL in XML and annotation are display the same result :
package com.mkyong.core;
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);
}
}
Output
Customer [item=Item [name=itemA, qty=10], itemName=itemA]
Spring EL hello world example的更多相关文章
- Spring3系列6 - Spring 表达式语言(Spring EL)
Spring3系列6-Spring 表达式语言(Spring EL) 本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.S ...
- Test Spring el with ExpressionParser
Spring expression language (SpEL) supports many functionality, and you can test those expression fea ...
- Spring EL regular expression example
Spring EL supports regular expression using a simple keyword "matches", which is really aw ...
- Spring EL Lists, Maps example
In this article, we show you how to use Spring EL to get value from Map and List. Actually, the way ...
- Spring EL ternary operator (if-then-else) example
Spring EL supports ternary operator , perform "if then else" conditional checking. For exa ...
- Spring EL Operators example
Spring EL supports most of the standard mathematical, logical or relational operators. For example, ...
- Spring EL method invocation example
In Spring EL, you can reference a bean, and nested properties using a 'dot (.)' symbol. For example, ...
- Spring Boot实战笔记(二)-- Spring常用配置(Scope、Spring EL和资源调用)
一.Bean的Scope Scope描述的是Spring容器如何新建Bean实例的.Spring的Scope有以下几种,通过@Scope注解来实现. (1)Singleton:一个Spring容器中只 ...
- Spring 在 xml配置文件 或 annotation 注解中 运用Spring EL表达式
Spring EL 一:在Spring xml 配置文件中运用 Spring EL Spring EL 采用 #{Sp Expression Language} 即 #{spring表达式} ...
随机推荐
- OLAP、OLTP的介绍和比较
OLTP与OLAP的介绍 数据处理大致可以分成两大类:联机事务处理OLTP(on-line transaction processing).联机分析处理OLAP(On-Line Analytical ...
- 国内顺利使用Google的另类技巧
在特殊的地方和特殊的时间,流畅顺利使用Google的方法也会变得很特殊.本文不定期保持维护更新,删除不能用的,增加新的网址. 分享一些奇葩的Google使用方法,通过下列网址也可以使用Google来搜 ...
- 函数 xdes_get_descriptor_with_space_hdr
获得区描述符 xdes entry 的offset /********************************************************************//** ...
- Codeforces Round #271 (Div. 2)
A. Keyboard 题意:一个人打字,可能会左偏一位,可能会右偏一位,给出一串字符,求它本来的串 和紫书的破损的键盘一样 #include<iostream> #include< ...
- HDU 1150 Machine Schedule (最小覆盖,匈牙利算法)
题意: 有两台不同机器A和B,他们分别拥有各种运行模式1~n和1~m.现有一些job,需要在某模式下才能完成,job1在A和B上需要的工作模式又可能会不一样.两台机器一开始处于0模式,可以切换模式,但 ...
- H.264中NAL、Slice与frame意思及相互关系
H.264中NAL.Slice与frame意思及相互关系 NAL nal_unit_type中的1(非IDR图像的编码条带).2(编码条带数据分割块A).3(编码条带数据分割块B).4(编码条带数据分 ...
- 修改dbwr后台进程数量
批量执行脚本时,批量数据写回到数据库:从EM中查看到有较多的dbwr的IO请求 查看后台dbwr的进程数量 select * from v$bgprocess 在查询结果中paddr的字段为非'0 ...
- windows配置jdk
一.JDK1.6下载 目前JDK最新版本是JDK1.6,到http://java.sun.com/javase/downloads/index.jsp可以下载JDK1.6. 二.JDK1.6安装 JD ...
- Oracle 跟踪事件 set event
一.Oracle跟踪文件 Oracle跟踪文件分为三种类型,一种是后台报警日志文件,记录数据库在启动.关闭和运行期间后台进程的活动情况,如表空间创建.回滚段创建.某些alter命令.日志切换.错误消息 ...
- Storm入门教程 第三章Storm集群安装部署步骤、storm开发环境
一. Storm集群组件 Storm集群中包含两类节点:主控节点(Master Node)和工作节点(Work Node).其分别对应的角色如下: 主控节点(Master Node)上运行一个被称为N ...