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@Valuein Java class, the@Valuewill 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表达式} ...
随机推荐
- linux中改变文件权限和属性
Linux中,默认显示所有用户名的文件在/etc/passwd,用户组的信息在/etc/group 密码/etc/shadow chgrp改变文件所属用户组 chgrp [-R] 用户组名 文件或目录 ...
- BZOJ2298: [HAOI2011]problem a
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2298 题解:刚开始思考的方向错了...一直在想LIS什么的,又发现不合法的情况不好判断,真是个 ...
- Asp.net 后台添加Meta标签方法
Asp.net 后台添加Meta标签方法包括keywords,CSS.JS 下面是从Asp.net 后台添加CSS.JS.Meta标签的写法,我们这里写成函数方便以后使用.如果函数放在页面类中, Pa ...
- OK335xS psplash make-image-header.sh hacking
/***************************************************************************** * OK335xS psplash mak ...
- I.MX6 Power off register hacking
/*********************************************************************** * I.MX6 Power off register ...
- 从投影的角度理解pca:向量,投影,基,内积,坐标,维数,分散程度,方差,协方差矩阵,对角化,特征值分解,主成分分析PCA
参考:http://blog.csdn.net/songzitea/article/details/18219237
- 03day2
03day1 不说了,图论题因为没有把加边的过程放到循环里导致只有 10 分.(不要吐槽我啊...) 竞赛排名 排序 [问题描述] [输入] 文件的第一行为参赛总人数 N(1≤N≤1000),从第 ...
- poj 2773 Happy 2006
// 题意 :给你两个数 m(10^6),k(10^8) 求第k个和m互质的数是什么这题主要需要知道这样的结论gcd(x,n)=1 <==> gcd(x+n,n)=1证明 假设 gcd(x ...
- 转载RabbitMQ入门(2)--工作队列
工作队列 (使用Java客户端) 在这第一指南部分,我们写了通过同一命名的队列发送和接受消息.在这一部分,我们将会创建一个工作队列,在多个工作者之间使用分布式时间任务. 工作队列(亦称:任务队列)背后 ...
- 【Mysql】初学命令行指南
MYSQL初学者使用指南与介绍 一.连接MYSQL 格式: mysql -h主机地址 -u用户名 -p用户密码 1.例1:连接到本机上的MYSQL. 首先在打开DOS窗口,然后进入目录 mysqlbi ...