这一章节我们来讨论一下SpEl表达式的简单介绍与嵌入值。

1.SpEl表达式简单介绍

Spring Excpression Language (SpEL)语言支持在执行时操作和查询对象

事实上就是在执行的过程中操作对应的对象或者值。

2.SpEl嵌入值

(1)domain

蛋糕类:(不变)

  1. package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_14;
  2.  
  3. public class Cake {
  4.  
  5. private int id = 0;
  6.  
  7. private String name = "";
  8.  
  9. private double size = 0;
  10.  
  11. public String getName() {
  12. return name;
  13. }
  14.  
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18.  
  19. public double getSize() {
  20. return size;
  21. }
  22.  
  23. public void setSize(double size) {
  24. this.size = size;
  25. }
  26.  
  27. public int getId() {
  28. return id;
  29. }
  30.  
  31. public void setId(int id) {
  32. this.id = id;
  33. }
  34.  
  35. @Override
  36. public String toString() {
  37. return "cake id:" + id + " name:" + name + " size:" + size;
  38. }
  39.  
  40. }

(2)測试类:(打印蛋糕对象的属性)

  1. package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_14;
  2.  
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.test.context.ContextConfiguration;
  8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  9.  
  10. @RunWith(SpringJUnit4ClassRunner.class)
  11. @ContextConfiguration(locations = {
  12. "/com/raylee/my_new_spring/my_new_spring/ch01/topic_1_14/ApplicationContext-test.xml" })
  13. public class CakeTest {
  14.  
  15. @Autowired
  16. private ApplicationContext applicationContext;
  17.  
  18. @Test
  19. public void testCake() {
  20. Cake cake = applicationContext.getBean(Cake.class);
  21. System.out.println(cake);
  22. }
  23. }

(3)配置文件

  1. <?
  2.  
  3. xml version="1.0" encoding="UTF-8"?>
  4. <beans xmlns="http://www.springframework.org/schema/beans"
  5. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:aop="http://www.springframework.org/schema/aop"
  8. xsi:schemaLocation="
  9. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
  11. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  12. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
  13. http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
  14. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  15. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
  16.  
  17. <bean id="cake"
  18. class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_14.Cake">
  19. <property name="id" value="#{1}"/>
  20. <property name="name" value="#{'blueberry cheese cake'}"/>
  21. <!-- <property name='name' value='#{"blueberry cheese cake"}'/> -->
  22. <property name="size" value="#{5.5}"/>
  23. </bean>
  24.  
  25. </beans>

通过#{}把实际的值嵌入到对应的属性里面。当然事实上是嵌入值是没什么意思,由于这样已经把程序给写死了。

測试输出:

cake id:1 name:blueberry cheese cake size:5.5

总结:这一章节主要介绍SpEl表达式的简单介绍与嵌入值。

文件夹:http://blog.csdn.net/raylee2007/article/details/50611627

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

从头认识Spring-1.14 SpEl表达式(1)-简单介绍与嵌入值的更多相关文章

  1. EL表达式的简单介绍

    EL表达式的简单介绍 一.JSP EL语言定义 E L(ExpressionLanguage)  目的:为了使JSP写起来更加简单. 表达式语言的灵感来自于ECMAScript 和 XPath 表达式 ...

  2. Spring 缓存注解 SpEL 表达式解析

    缓存注解上 key.condition.unless 等 SpEL 表达式的解析 SpEl 支持的计算变量: 1)#ai.#pi.#命名参数[i 表示参数下标,从 0 开始] 2)#result:Ca ...

  3. Spring进阶之路(10)-Advice简单介绍以及通过cglib生成AOP代理对象

    Advice简单介绍 1. Before:在目标方法运行之前运行织入.假设Before的处理中没有进行特殊的处理.那么目标方法终于会运行,可是假设想要阻止目标方法运行时.能够通过抛出一个异常来实现.B ...

  4. 利用SpEL 表达式实现简单的动态分表查询

    这里的动态分表查询并不是动态构造sql语句,而是利用SpEL操作同一结构的不同张表. 也可以参考Spring Data Jpa中的章节http://docs.spring.io/spring-data ...

  5. Spring实战学习笔记之SpEL表达式

            在Spring XML配置文件中装配Bean的属性和构造参数都是静态的,而在运行期才知道装配的值,就可以使用SpEL实现         SpEL表达式的首要目标是通过计算获得某个值. ...

  6. Spring SpEL表达式的理解

    Spring的IOC本质就一个容器,也就是一个对象的工厂,我们通过配置文件注册我们的Bean对象,通过他进行对象的组装与床架. SpEL表达式就是一种字符串编程,类似于JS里面的EVAL的作用,通过它 ...

  7. Spring Security -SpEL表达式

    Spring Security -SpEL表达式 开启SpEL表达式 <!-- use-expressions是否开启 SpEL表达式 o.s.s.web.access.expression.W ...

  8. 【spring boot】SpringBoot初学(2.2)– SpEL表达式读取properties属性到Java对象

    前言 github: https://github.com/vergilyn/SpringBootDemo 代码位置:(注意测试方法在,test下的SpelValueApplicationTest.c ...

  9. Spring系列.SpEL表达式

    Spring表达式语言 SpEL语言是一种强大的表达式语言,支持在运行时查询和操作对象.SpEL表达式不一定要创建IOC容器后才能使用.用户完全可以单独调用SpEL的API来独立的使用时SpEL表达式 ...

随机推荐

  1. Linux netstat命令具体解释

    简单介绍 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表.接口状态 (Interface Statistics).masquerade 连接,多播成员 (Multicast Memb ...

  2. Java并发编程的艺术(二)——重排序

    当我们写一个单线程程序时,总以为计算机会一行行地运行代码,然而事实并非如此. 什么是重排序? 重排序指的是编译器.处理器在不改变程序执行结果的前提下,重新排列指令的执行顺序,以达到最佳的运行效率. 重 ...

  3. ntp测试

    cmd下 w32tm /stripchart /computer:time1.aliyun.com linux ntpdate ntp1.aliyun.com

  4. git error: RPC failed; curl 56 GnuTLS recv error 解决方案

    // git 报错情况: error: RPC failed; curl 56 GnuTLS recv error (-110): The TLS connection was non-properl ...

  5. 用Eclipse开发Androd应用程序时,自带虚机模拟器太慢了,怎么办

    问:用Eclipse开发Androd应用程序时,系统自带模拟器太慢了,怎么办? 答:用Genymotin

  6. jpa命名规则 jpa使用sql语句 @Query

    关键字方法命名sql where字句 AndfindByNameAndPwdwhere name= ? and pwd =? OrfindByNameOrSexwhere name= ? or sex ...

  7. cannot be resolved. It is indirectly referenced from required .class files

    缺少引用. 把缺少的引用在导入一下...如果是mavan 在当前moudle里也要把 dependency加进来

  8. emouse思·睿—评论与观点整理之四

    虽说我主要做的硬件,平时的兴趣爱好比较关注移动互联网,混迹于虎嗅.爱范儿.雷锋网.36Kr.cnBeta.瘾科技.i黑马.TechWeb等这类科技以及创业媒体,遗憾的是系统的去写的并不多,好在还算充分 ...

  9. 访问Oracle数据库的工具【unfinished】

    ylbtech-Oracle:访问Oracle数据库的工具 访问Oracle数据库的工具 1. SQL*PLUS返回顶部 1.0, 1.0.1, 之network\admin\tnsnames.ora ...

  10. AndroidManifest.xml文件解析(转帖)

    原帖地址:http://www.cnblogs.com/pilang/archive/2011/04/20/2022932.html 一.关于AndroidManifest.xml       And ...