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

1.SpEl表达式简单介绍

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

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

2.SpEl嵌入值

(1)domain

蛋糕类:(不变)

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_14;

public class Cake {

	private int id = 0;

	private String name = "";

	private double size = 0;

	public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public double getSize() {
return size;
} public void setSize(double size) {
this.size = size;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} @Override
public String toString() {
return "cake id:" + id + " name:" + name + " size:" + size;
} }

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

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_14;

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_14/ApplicationContext-test.xml" })
public class CakeTest { @Autowired
private ApplicationContext applicationContext; @Test
public void testCake() {
Cake cake = applicationContext.getBean(Cake.class);
System.out.println(cake);
}
}

(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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="cake"
class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_14.Cake">
<property name="id" value="#{1}"/>
<property name="name" value="#{'blueberry cheese cake'}"/>
<!-- <property name='name' value='#{"blueberry cheese cake"}'/> -->
<property name="size" value="#{5.5}"/>
</bean> </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. 【转】比较init-method,afterPropertiesSet和BeanPostProcessor

    一.简单介绍 1.init-method方法,初始化bean的时候执行,可以针对某个具体的bean进行配置.init-method需要在applicationContext.xml配置文档中bean的 ...

  2. 行尸走肉第一季/全集The Walking Dead迅雷下载

    本季The Walking Dead 1 第一季(2010)看点:<行尸走肉>讲述了警察瑞克在一次执法行动中因中弹负伤而不省人事,当他从昏迷中苏醒后却惊讶地发现,这个世界已然天翻地覆.周遭 ...

  3. 深入理解ClassLoader工作机制(jdk1.8)

    ClassLoader 顾名思义就是类加载器,ClassLoader 作用: 负责将 Class 加载到 JVM 中    审查每个类由谁加载(父优先的等级加载机制)    将 Class 字节码重新 ...

  4. ExtJS 4.2 教程-06:服务器代理(proxy)

    转载自起飞网,原文地址:http://www.qeefee.com/extjs-course-6-server-proxy ExtJS 4.2 教程-01:Hello ExtJS ExtJS 4.2 ...

  5. Eclipse启动时提示Fail to create the Java Virtual Machine的解决方法

    这个错误是Eclipse里面的一个bug,我们通过如下的设置就可以解决它. 打开eclipse安装目录下的eclipse.ini文件: 将其中的256m改为128m,512m改为256m,1024m改 ...

  6. 在Android中实现图片的裁剪

        本实例的功能是将用户选择的图片裁剪后放入ImagView,布局文件是个Button和ImageView.为了图片的正常显示,我们在裁剪后先将裁剪好的图片先存放到SD卡中,这样就能在以后开启应用 ...

  7. Qt中对QDomDocument和QDomnode的理解

    一.对QDomDocument和QDomnode的理解 QDom前缀的都是代表节点类型.所以有,QDomElement代表一个Element节点,而QDomText代表一个Text节点.QDomNod ...

  8. System.Reflection.TargetException:“非静态方法需要一个目标。”

    报错:TargetException, 非静态方法需要一个目标,非静态方法 如果实例为null,调用实例方法会报如上错. 解决办法: 检查实例是否为null,考虑什么情况下实例为null,然后排除实例 ...

  9. 【BZOJ】【4146】 【AMPPZ2014】Divisors

    暴力 由于值的范围很小($ \leq 2*10^6$),所以用一个cnt数组统计每个值有多少个数,然后从小到大,统计每个数的倍数即可. 根据调和数?的神奇性质= =这样是$O(nlogn)$的…… / ...

  10. 自适应尺寸变化的meanshift跟踪

    近期在看meanshift方面的文章,看了一篇博文对这篇文章<Robust scale-adaptive meanshift for tracking>寄予非常高的评价,所以把这篇文章简要 ...