这一章节我们来说一下SpEl表达式的坑,以及补充一下SpEl表达式操作类的方法与常量。

1.domain

厨师类:(新增一个朋友属性)

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_16;public class Chief {private String name = "";private Oven oven;private Chief friend;public Chief getFriend() {return friend;}public void setFriend(Chief friend) {this.friend = friend;}public String getName() {return name;}public Oven getOven() {return oven;}public void setName(String name) {this.name = name;}public void setOven(Oven oven) {this.oven = oven;}public void userOven() {System.out.println(name + " use " + oven);}}

烤炉类:(增加了烤炉的大小)

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_16;public class Oven {private String name = "";private double size = 0;public double getSize() {return size;}public void setSize(double size) {this.size = size;}@Overridepublic String toString() {return name;}public String getName() {return name;}public void setName(String name) {this.name = name;}}

2.测试类:(不变)

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_16;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/com/raylee/my_new_spring/my_new_spring/ch01/topic_1_16/ApplicationContext-test.xml" })
public class ChiefTest {@Autowiredprivate ApplicationContext applicationContext;@Testpublic void testChief() {Chief jack = (Chief) applicationContext.getBean("jack");jack.userOven();Chief mike = (Chief) applicationContext.getBean("mike");mike.userOven();Chief rose = (Chief) applicationContext.getBean("rose");rose.userOven();}
}

3.配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="smallOven"class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_16.Oven"p:name="smallOven" p:size="#{T(java.lang.Math).PI}" /><bean id="mike"class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_16.Chief"p:name="mike"><property name="oven" ref="#{jack.oven}" /><property name="friend" ref="jack" /></bean><bean id="jack"class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_16.Chief"p:name="jack"><property name="oven" ref="smallOven" /><property name="friend" ref="mike" /></bean><bean id="rose"class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_16.Chief"p:name="rose"><property name="oven" ref="#{jack.getFriend().getOven()}" /><property name="friend" ref="jack" /></bean>
</beans>

注意的地方

(1)我们使用#{T(class).xxx}来控制类的方法与常量

(2)在执行的过程中一定要注意Bean前后属性,当上面的mike与jack互换位置的时候,就会抛异常,上面的是正确的

(3)在执行的过程中必须注意toString的问题,就像上面的#{jack.oven}和#{jack.getFriend().getOven()}其实就是先调用oven的toString方法,得到相应的名称,然后再放到配置文件去(我返回的名字跟id一致),然后再读取这个返回值的Bean在不在,如果不重写toString或者toString返回的字符串跟你Bean里面的id不匹配,直接抛异常

总结:这一章节通过一个例子来说明SpEl表达式的坑。

目录:http://blog.csdn.net/raylee2007/article/details/50611627

我的github:https://github.com/raylee2015/my_new_spring

从头认识Spring-1.14 SpEl表达式(3)-SpEl表达式的两个坑:Bean的顺序与Bean的toString方法相关推荐

  1. 记自己在spring中使用redis遇到的两个坑

    本人在spring中使用redis作为缓存时,遇到两个坑,现在记录如下,算是作为自己的备忘吧,文笔不好,望大家见谅: 一.配置文件 1 <!-- 加载Properties文件 --> 2 ...

  2. spring实战学习(二)spEL表达式

    什么是spEL表达式? spEL是spring表达式语言,它是一种强大,简介的装配Bean的方式,它通过运行期执行的表达式将值装配到Bean的属性或构造器参数中. 简单来说,以前我们装配Bean不管是 ...

  3. [Spring实战系列](11)SpEL使用表达式装配

    到目前为止,我们为Bean 的属性和构造器参数装配的所有东西都是在Spring 的XML 配置文件中静态定义的. <bean id = "yoona" class = &qu ...

  4. Spring3 表达式语言(SpEL)介绍

    http://leeyee.github.io/blog/2011/06/19/spring-expression-language/ Spring Expression Language (SpEL ...

  5. Sping-Spring表达式语言SpEL

    概述 SpEL:字面量 SpEL:引用 Bean.属性和方法 引用其他对象 引用其他对象的属性 调用其他方法,还可以链式操作 调用静态方法或静态属性 SpEL支持的运算符号 算数运算符:+, -, * ...

  6. 【web安全】Spring Data Commons 1.13.10 SpEL漏洞分析

    一.简介 Spring Data是一个用于简化数据库访问,并支持云服务的开源框架,Spring Data Commons是Spring Data下所有子项目共享的基础框架.Spring Data Co ...

  7. Go 学习笔记(62)— Go 中 switch 语句中的 switch 表达式和 case 表达式之间的关系

    switch 语句对 switch 表达式的结果类型,以及各个 case 表达式中子表达式的结果类型都是有要求的. 毕竟,在 Go 语言中,只有类型相同的值之间才有可能被允许进行判等操作. 1. sw ...

  8. java中缀表达式转后缀表达式_数据结构Java实现06----中缀表达式转换为后缀表达式...

    本文主要内容: 表达式的三种形式 中缀表达式与后缀表达式转换算法 一.表达式的三种形式: 中缀表达式:运算符放在两个运算对象中间,如:(2+1)*3.我们从小做数学题时,一直使用的就是中缀表达式. 后 ...

  9. 前缀、中缀和后缀表达式详解,中缀表达式到后缀表达式的转换规则,以及后缀表达式的计算规则,附计算代码

    1. 中缀.前缀和后缀表达式 1.1 中缀表达式 首先,中缀表达式的这个"缀"指运算符在两个操作数的位置.中缀表达式其实就是我们常用的算术表达式,比如 2 + 9 - (32 * ...

最新文章

  1. 女性走夜路不安全?英国奇葩新招:无人机护航,关键时刻用光吓退张三
  2. 一个关于解决序列化问题的编程技巧
  3. python3.6.5安装教程-Centos7安装python3.6.5
  4. sas table将缺失值计入百分比_SAS:通过数据块填充缺失值
  5. LeetCode - 121. 买卖股票的最佳时机
  6. 注册表操作(VC_Win32)
  7. iOS开发之Runtime关联属性
  8. 服务器常用的状态码及其对应的含义如下
  9. linux系统上项目部署
  10. iostream stdlib fstream io.h 头文件的作用
  11. Java网络编程从入门到精通 (9):使用isXxx方法判断地址类型
  12. java.io.StreamCorruptedException: invalid type code: AC错误的解决方法
  13. sql server,mysql,oracle 获取上一月时间
  14. SQL2005开发版下载地址
  15. js 判断数组的4种方法
  16. ubuntu18.04下EnlightenGAN运行过程记录
  17. OMAPL138报Error connecting to the target:Connect to PRSC failed解决办法
  18. Oracle报错宗介
  19. 附近的人实现(Redis 3.2 以上版本和es 性能比较)
  20. 萌新如何用Python实现人脸替换升级看高级程序员一步一步带你进阶

热门文章

  1. 注意力模型直观理解(Attention Model Intuition)
  2. 第三方接入支付宝授权登录(支付宝新建应用没有公钥和私钥)问题
  3. 蓝牙连接的sco问题
  4. JavaScript基础知识快速预览
  5. Market1501数据集介绍及相关代码
  6. 2. 【containerd】 containerd-shim-runc-v1与 containerd-shim-runc-v2 区别
  7. 董嘉文抵达之谜:真正的努力从来都不动声色
  8. JAVA中随机数的选取方法
  9. 数据仓库 OLAP
  10. Kubernetes中pod分类、核心组件、网络模型及kubectl命令使用