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注解,可以实现不同环境(开发.测试 ...
随机推荐
- Spark 多项式逻辑回归__二分类
package Spark_MLlib import org.apache.spark.ml.Pipeline import org.apache.spark.ml.classification.{L ...
- 15.extjs tabPanel的用法
转自:https://blog.csdn.net/mezhaha/article/details/78878894 本文导读:TabPanel继承于Ext.Panel,Ext.TabPanel就是有选 ...
- C++中虚析构函数的作用 (转载)
转自:http://blog.csdn.net/starlee/article/details/619827 我们知道,用C++开发的时候,用来做基类的类的析构函数一般都是虚函数.可是,为什么要这样做 ...
- MySQL5.7 windows二进制安装
200 ? "200px" : this.width)!important;} --> 介绍 1.下载解压 下载地址:http://dev.mysql.com/get/Dow ...
- Linux进入单用户模式的两种方法
单用户模式的作用 在使用Linux系统中,维护人员经常会碰到一个问题,就是在拥有root账号权限和密码的用户中,总是会出现忘记root密码的情况. 遇到这种情况,一般情况下,维护人员就会通过最常用的方 ...
- WEB前端学习
第一日:软件的安装和下载 1.百度搜索推荐使用webStorm前端神器进行开发,傻瓜式安装不必多说! 激活 前提:修改本地的hosts配置文件(/etc/hosts) 最后一行新增这句话:0.0.0. ...
- [C和指针] 6-指针
6.1 内存和地址 我们可以把计算机的内存看作是一条长街上的一排房屋,每座房子都可以容纳数据,并通过一个房号来标识. 这个比喻颇为有用,但也存在局限性.计算机的内存由以亿万计的位(bit)组成,每个位 ...
- 题解报告:poj 1321 棋盘问题(dfs)
Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...
- 405 Convert a Number to Hexadecimal 数字转换为十六进制数
给定一个整数,编写一个算法将这个数转换为十六进制数.对于负整数,我们通常使用 补码运算 方法.注意: 十六进制中所有字母(a-f)都必须是小写. 十六进制字符串中不能包含多余的前导零.如果 ...
- sublime 3 最新注册码
http://9iphp.com/web/html/sublime-text-3-license-key.html