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注解,可以实现不同环境(开发.测试 ...
随机推荐
- windows7 RDP修改
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TerminalServer\WinStations\RDP Tcp\PortNumber”.
- Linux压缩命令(zip/gz/bz2/tar/tar.gz/tar.bz2)
一.Linux的压缩格式 .zip . gz . bz2 .tar 1..zip格式(Linux和Windows是可以互传的) 压缩命令 语法:zip 文件名.zip 文件名 ------压缩 ...
- Linux 下安装配置 JDK7(转载)
转自:http://dawndiy.com/archives/155/ 自从从Oracle收购Sun近三年来,已经有很多变化.早在8月,甲骨文将“Operating System Distributo ...
- E20171225-hm
abstract adj. 抽象的,理论上的; 难解的; 抽象派的; 茫然的;
- P4556 [Vani有约会]雨天的尾巴(线段树合并)
传送门 一道线段树合并 首先不难看出树上差分 我们把每一次修改拆成四个,在\(u,v\)分别放上一个,在\(lca\)和\(fa[lca]\)各减去一个,那么只要统计一下子树里的总数即可 然而问题就在 ...
- 51NOD 1088 最长回文子串&1089 最长回文子串 V2(Manacher算法)
回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串. 输入一个字符串Str,输出Str里最长回文子串的长度. Input 输入Str(Str的长度 <= 1000(第二题要 ...
- vux修改css样式的2种办法
最近手上有个移动端的项目.前端UI框架是选择的VUX.但是在使用的时候经常会发现框架自带的组件样式和我们要求的不一致.经常需要手动覆盖样式.这里记录下我们使用的2种方法. 我们以XHeader组件 ...
- redis 客户端工具 RedisDesktopManager
https://redisdesktop.com/download 可以查看到spring+redis 缓存的数据
- 洛谷 P3437 [POI2006]TET-Tetris 3D
二维线段树区间更新啊 树套树的外层树,如果是线段树的话一般似乎不能打标记?(毕竟标记不好下传) 然而起码对于这题是可以的...对于外层线段树,每个节点放两个内层线段树dat和setv,分别是得到的值和 ...
- 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe
题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...