Spring SpEL 各种写法示例
项目路径

先说一下三个bean都有哪些属性
Address.java
private String city;//城市
private String street;//街道
Car.java
private String brand;//品牌
private double price;//价格
private double tyrePerimeter;//轮胎周长
Person.java
private String name;//姓名
private Car car;
private String city;//引用Address的city属性
private String info;//若car的价格>30000?金领:白领
以上bean都有对应的get/set方法和重写的toString方法
一 使用SpEL表达式写字符串
applicationContext.xml
<bean id="address" class="com.tse.beans.Address">
<!-- 使用SpEl表达式写字符串*意义不大 -->
<property name="city" value="#{'北京'}"></property>
<property name="street" value="王府井"></property>
</bean>
Main方法中测试
public static void main(String[] args) {
ApplicationContext actx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
Address address = (Address) actx.getBean("address");
System.out.println(address);
}
执行结果
Address [city=北京, street=王府井]
二 使用SpEl表达式引用静态变量
applicationContext.xml中新增bean
<bean id="car" class="com.tse.beans.Car">
<property name="brand" value="Audi"></property>
<property name="price" value="200000"></property>
<!-- 使用SpEl表达式引用静态变量 -->
<property name="tyrePerimeter" value="#{T(java.lang.Math).PI * 80}"></property>
</bean>
Main中测试(基于上一条一起测试)
public static void main(String[] args) {
ApplicationContext actx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
Address address = (Address) actx.getBean("address");
System.out.println(address);
Car car = (Car) actx.getBean("car");
System.out.println(car);
}
测试结果
Address [city=北京, street=王府井]
Car [brand=Audi, price=200000.0, tyrePerimeter=251.32741228718345]
三 使用SpEl表达式引用其他bean
使用SpEl表达式引用其他bean 的属性
使用SpEl表达式使用运算符
applicationContext.xml
<bean id="person" class="com.tse.beans.Person">
<property name="name" value="Tom"></property>
<!-- 使用SpEl表达式引用其他bean -->
<property name="car" value="#{car}"></property>
<!-- 使用SpEl表达式引用其他bean 的属性-->
<property name="city" value="#{address.city}"></property>
<!-- 使用SpEl表达式使用运算符 -->
<property name="info" value="#{car.price > 300000 ? '金领':'白领'}"></property>
</bean>
Main
public static void main(String[] args) {
ApplicationContext actx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
Address address = (Address) actx.getBean("address");
System.out.println(address);
Car car = (Car) actx.getBean("car");
System.out.println(car);
Person person = (Person) actx.getBean("person");
System.out.println(person);
}
执行结果
Address [city=北京, street=王府井]
Car [brand=Audi, price=200000.0, tyrePerimeter=251.32741228718345]
Person [name=Tom, car=Car [brand=Audi, price=200000.0, tyrePerimeter=251.32741228718345], city=北京, info=白领]
源码下载链接
https://download.csdn.net/download/lijian0420/10664813
Spring SpEL 各种写法示例的更多相关文章
- jsp的三种自定义标签 写法示例
1.自定义方法标签 引入方式示例: <%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %> 写 ...
- Spring @Transactional使用的示例
Spring @Transactional使用的示例: 参考: http://blog.csdn.net/seng3018/article/details/6690527 http://blog.si ...
- spring原理案例-基本项目搭建 03 创建工程运行测试 spring ioc原理实例示例
下面开始项目的搭建 使用 Java EE - Eclipse 新建一 Dynamic Web Project Target Runtime 选 Apache Tomcat 7.0(不要选 Apache ...
- Spring中的IOC示例
Spring中的IOC示例 工程的大概内容是: 一个人在中国时用中国话问候大家,在国外时用英语问候大家. 其中, IHelloMessage是接口,用来定义输出问候信息 public interfac ...
- Spring SpEL in JSP and Assign SpEL value to Java variable in JSP
Spring SpEL in JSP and Assign SpEL value to Java variable in JSP method 1 use----ServletContextAttri ...
- 手写Mybatis和Spring整合简单版示例窥探Spring的强大扩展能力
Spring 扩展点 **本人博客网站 **IT小神 www.itxiaoshen.com 官网地址****:https://spring.io/projects/spring-framework T ...
- Spring JDBC常用方法详细示例
Spring JDBC使用简单,代码简洁明了,非常适合快速开发的小型项目.下面对开发中常用的增删改查等方法逐一示例说明使用方法 1 环境准备 启动MySQL, 创建一个名为test的数据库 创建Mav ...
- 15个Spring的核心注释示例
众所周知,Spring DI和Spring IOC是Spring Framework的核心概念.让我们从org.springframework.beans.factory.annotation和org ...
- 【译】Spring 4 @Profile注解示例
前言 译文链接:http://websystique.com/spring/spring-profile-example/ 本文将探索Spring中的@Profile注解,可以实现不同环境(开发.测试 ...
随机推荐
- CodeForces 446A DZY Loves Sequences (DP+暴力)
题意:给定一个序列,让你找出一个最长的序列,使得最多改其中的一个数,使其变成严格上升序列. 析:f[i] 表示以 i 结尾的最长上升长度,g[i] 表示以 i 为开始的最长上升长度,这两个很容易就求得 ...
- 插入CSS的方法
传送门 选择器 selector {declaration1; declaration2; ... declarationN } 例: p { text-aligh:center; } ...
- 堆和栈的区别【以java为例潜入分析】
Java的堆是一个运行时数据区,类的对象从中分配空间,这些对象通过new等指令建立. 堆是由垃圾回收来负责的,堆的优势是可以动态地分配内存大小,生存期也不必事先告诉编译器,Java的垃圾收集器会自动 ...
- sql注入方法以及防范
sql注入方法: 1.数字注入 ; get请求 www.bobo.com?id=1 可以查出 ID等于1的一条数据. 如果有人在链接后面增加 www.bobo.com?id=1 or 1=1 / w ...
- php可以定义数组的常量吗
是这样吗?<?php define('BEST_PHPER',array('name'=>'巩文','address'=>'china')); My God,明确告诉你不可以:原因是 ...
- php pdo oracle
<?php/** * Created by mestars. * User: mestars * Date: 6/13/16 * Time: 10:52 PM */header('Access- ...
- iOS 项目中的常见文件
iOS的笔记-项目中的常见文件 新建一个项目之后,有那么多的文件,下面介绍一下主要的几个. 1.文件名 (1)AppDelegate UIApplication的代理,app收到干扰的时候,进行处 ...
- FCC 基础JavaScript 练习3
1.通过使用提供的变量参数:名词myNoun.形容词myAdjective.动词myVerb.副词myAdverb,来创建一个新的句子 result, function wordBlanks(myNo ...
- C语言特殊知识点解析
1 数组 1.1 概念 数组是指某种数据类型,在内存上按照顺序存储.中括号([ ])是数组的标识,中括号内的数值标识该种数据类型变量的个数,中括号也有取值的作用. 1.2 数组使用 int a[10] ...
- strong,weak, retain, assign的区别@property的参数
strong,weak, retain, assign的区别@property的参数 先说经验 使用场合 copy:NSString,block, weak:UI控件,代理 strong:一般对象.自 ...