项目路径

先说一下三个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 各种写法示例的更多相关文章

  1. jsp的三种自定义标签 写法示例

    1.自定义方法标签 引入方式示例: <%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %> 写 ...

  2. Spring @Transactional使用的示例

    Spring @Transactional使用的示例: 参考: http://blog.csdn.net/seng3018/article/details/6690527 http://blog.si ...

  3. spring原理案例-基本项目搭建 03 创建工程运行测试 spring ioc原理实例示例

    下面开始项目的搭建 使用 Java EE - Eclipse 新建一 Dynamic Web Project Target Runtime 选 Apache Tomcat 7.0(不要选 Apache ...

  4. Spring中的IOC示例

    Spring中的IOC示例 工程的大概内容是: 一个人在中国时用中国话问候大家,在国外时用英语问候大家. 其中, IHelloMessage是接口,用来定义输出问候信息 public interfac ...

  5. 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 ...

  6. 手写Mybatis和Spring整合简单版示例窥探Spring的强大扩展能力

    Spring 扩展点 **本人博客网站 **IT小神 www.itxiaoshen.com 官网地址****:https://spring.io/projects/spring-framework T ...

  7. Spring JDBC常用方法详细示例

    Spring JDBC使用简单,代码简洁明了,非常适合快速开发的小型项目.下面对开发中常用的增删改查等方法逐一示例说明使用方法 1 环境准备 启动MySQL, 创建一个名为test的数据库 创建Mav ...

  8. 15个Spring的核心注释示例

    众所周知,Spring DI和Spring IOC是Spring Framework的核心概念.让我们从org.springframework.beans.factory.annotation和org ...

  9. 【译】Spring 4 @Profile注解示例

    前言 译文链接:http://websystique.com/spring/spring-profile-example/ 本文将探索Spring中的@Profile注解,可以实现不同环境(开发.测试 ...

随机推荐

  1. 【158】◀▶ Linux-Bash学习

    鸟哥的 Linux 私房菜      Linux 的 26 个命令      Shell 脚本教程      Linux 命令大全 目录——按文件顺序: echo:显示变量内容 printf:格式化输 ...

  2. render same axis

    // 当前渲染相机的参数    QGlobalCamera* curRenderCamera = _getWorld()->getMainCam();    const Matrix4& ...

  3. bzoj 1833: [ZJOI2010]count 数字计数【数位dp】

    非典型数位dp 先预处理出f[i][j][k]表示从后往前第i位为j时k的个数,然后把答案转换为ans(r)-ans(l-1),用预处理出的f数组dp出f即可(可能也不是dp吧--) #include ...

  4. [Swift通天遁地]一、超级工具-(7)创建一个图文并茂的笔记本程序

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  5. maptalks 如何加载 ArcGIS 瓦片图层

    最近需要加载 ArcGIS 瓦片图层,运行官网加载 ArcGIS 瓦片图层的 demo 是没有问题的.如果把 ArcGIS 瓦片图层 URL 换成是自已发布的 ArcGIS 地图服务,发现加载不出来, ...

  6. 模拟 HDOJ 5387 Clock

    题目传送门 /* 模拟:这题没啥好说的,把指针转成角度处理就行了,有两个注意点:结果化简且在0~180内:小时13点以后和1以后是一样的(24小时) 模拟题伤不起!计算公式在代码内(格式:hh/120 ...

  7. 设置VMWare虚拟机使拷贝虚拟机后固定原有的IP地址

    VMWare中已经安装并设置好的虚拟机在拷贝给别人后,再次打开该虚拟机时原有自动获取的IP地址将会变化,那么原有根据该IP地址进行的设置均将失效,还需要重新设置,比较麻烦,经过百度查询原来可以将虚拟机 ...

  8. 简单3步,你即可以用上myFocus

    Step 1. 在html的标签内引入相关文件 <script type="text/javascript" src="js/myfocus-2.0.0.min.j ...

  9. Echarts和Quartz简介

    一.Echarts ​ ECharts 是由百度前端团队开发的一款开源的基于 js 图形报表组件,一个使用 JavaScript 实现的开源可视化库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大 ...

  10. [ Luogu 4626 ] 一道水题 II

    \(\\\) \(Description\) 求一个能被\([1,n]\) 内所有数整除的最小数字,并对 \(100000007\) 取模 \(N\in [1,10^8]\) \(\\\) \(Sol ...