项目路径

先说一下三个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. Cpp module

  2. end()与andSelf()

    end() 回到最近的一个"破坏性"操作之前.即,将匹配的元素列表变为前一次的状态. 如 果之前没有破坏性操作,则返回一个空集.所谓的"破坏性"就是指任何改变所 ...

  3. sql数据库如何在数据库里面把其中一个数据库的表复制到另一个数据库里面

    在sqlserver数据库里面,我们肯定有这样一个情况,假如我用的是SQL2008,如何把数据库里面的整个表以及表内数据复制到另外一个表中.那应该如何操作??有两种方法,我们一起来看一下 复制表结构: ...

  4. (前缀和 内存分配)51NOD 1081 子段求和

    给出一个长度为N的数组,进行Q次查询,查询从第i个元素开始长度为l的子段所有元素之和.   例如,1 3 7 9 -1,查询第2个元素开始长度为3的子段和,1 {3 7 9} -1.3 + 7 + 9 ...

  5. js 事件循环机制 EventLoop

    js 的非阻塞I/O  就是由事件循环机制实现的 众所周知  js是单线程的 也就是上一个任务完成后才能开始新的任务 那js碰到ajxa和定时器.promise这些异步任务怎么办那?这时候就出现了事件 ...

  6. 2017青岛网络赛1008 Chinese Zodiac

    Chinese Zodiac Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) T ...

  7. WCF 相关配置

    WCF错误:413 Request Entity Too Large 在我们用WCF传输数据的时候,如果启用默认配置,传输的数据量过大,经常会出这个错误. WCF包含服务端与客户端,所以这个错误可能出 ...

  8. SQL编程语句

    视图 视图就是我们查询出来的虚拟表创建视图:create view 视图名 as SQL查询语句,分组,排序,in 等都不能写视图的用法: select * from 视图名 SQL编程 定义变量:d ...

  9. log4j2异步日志解读(二)AsyncLogger

    前文已经讲了log4j2的AsyncAppender的实现[log4j2异步日志解读(一)AsyncAppender],今天我们看看AsyncLogger的实现. 看了这个图,应该很清楚AsyncLo ...

  10. SQL生僻字模糊查询

    生僻字指在数据库默认的编码中不存 又称难字或冷僻字 一.SQL中解决生僻字录入乱码问题[调整列数据类型->由varchar改为NVARCHAR]