Spring3.x--使用SpEL表达式装配
2014-09-11 14:14
585 查看
我们为Bean的属性和构造器参数装配的所有东西都是在Spring的XML配置文件中静态定义的。同样我们装配其他Bean的引用也都是在配置Spring配置文件时就已经确定了。但是,我们为属性装配的值在运行期才能知道,那又如何实现呢?
Spring3引入了Spring表达式语言,SpEL。SpEL是一种强大、简洁的装配Bean的方式,它通过运行期执行的表达式将值装配到Bean的属性或构造器参数中,使用SpEL,可以实现超乎想象的装配效果,这是使用传统的Spring装配方式难以做到的。
SpEL拥有许多特性,包括:使用Bean的ID来引用Bean;调用方法和访问对象的属性;对值进行算术、关系和逻辑运算;正则表达式匹配;集合操作。
1、引用Bean、Properties和方法
引用其他Bean
<property name="instrument" value="#{saxophone}"/>
引用其他Bean的属性
<bean id="carl" class="com.spring.InstrumentList">
<property name="song" value="#{kenny.song}"/>
</bean>
引用其他Bean的方法
<property name="song" value="#{songSelector.selectSong()}"/>
调用UpperCase方法
<property name="song" value="#{songSelector.selectSong().toUpperCase()}"/>
?.跟上面的区别是当selectSong方法返回null将不执行toUpperCase
<property name="song" value="#{songSelector.selectSong()?.toUpperCase()}"/>
2、操作类
<property name="area" value="#{T(java.lang.Math).PI*circle.radius^2}"/>
Spring3引入了Spring表达式语言,SpEL。SpEL是一种强大、简洁的装配Bean的方式,它通过运行期执行的表达式将值装配到Bean的属性或构造器参数中,使用SpEL,可以实现超乎想象的装配效果,这是使用传统的Spring装配方式难以做到的。
SpEL拥有许多特性,包括:使用Bean的ID来引用Bean;调用方法和访问对象的属性;对值进行算术、关系和逻辑运算;正则表达式匹配;集合操作。
1、引用Bean、Properties和方法
引用其他Bean
<property name="instrument" value="#{saxophone}"/>
引用其他Bean的属性
<bean id="carl" class="com.spring.InstrumentList">
<property name="song" value="#{kenny.song}"/>
</bean>
引用其他Bean的方法
<property name="song" value="#{songSelector.selectSong()}"/>
调用UpperCase方法
<property name="song" value="#{songSelector.selectSong().toUpperCase()}"/>
?.跟上面的区别是当selectSong方法返回null将不执行toUpperCase
<property name="song" value="#{songSelector.selectSong()?.toUpperCase()}"/>
2、操作类
<property name="area" value="#{T(java.lang.Math).PI*circle.radius^2}"/>
相关文章推荐
- Spring 使用表达式进行装配 SpEL表达式
- Spring使用SpEL表达式装配Bean
- Spring实战-使用表达式装配(三)
- (笔记)Spring实战_装配Bean(3)_使用表达式装配
- spring_scope作用域和使用外部属性文件和spEL表达式
- spring同时使用aop和spel表达式出错:Property or field 'instrument' cannot be found on object of type 'com.sun.pr
- 使用Spring表达式语言进行装配
- spring同时使用aop和spel表达式出错:Property or field 'instrument' cannot be found on object of type 'com.sun.pr
- Spring的SpEL表达式标准简单使用
- Spring4.3x教程之二SpEL表达式的使用
- spring ioc容器注入的集中方式,以及属性注入的集中依赖,继承,使用SPEL表达式,ioc容器的生命周期,bean的几种工厂方法配置方式,bean的自动装配
- Spring4 学习系列之——SpEl表达式的使用
- 使用SpEL表达式装配Bean
- [Spring实战系列](11)SpEL使用表达式装配
- spring 装配bean(2)之SpEL表达式
- 【第五章】Spring表达式语言 之 5.1 概述 5.2 SpEL基础 ——跟我学spring3
- (5) Spring 如何装配 集合类 以及 【第二种 依赖注入 方式】使用 构造器 注入
- (6) Spring 如何装配 集合类 以及 【第二种 依赖注入 方式】使用 构造器 注入
- 【第五章】Spring表达式语言 之 5.4在Bean定义中使用EL—跟我学spring3
- 【第五章】Spring表达式语言 之 5.3 SpEL语法 ——跟我学spring3