使用SpEL表达式装配Bean
来源:互联网 发布:cf免费卡枪带软件 编辑:程序博客网 时间:2024/05/16 05:45
字面值:
在<property>
元素的value
属性中使用#{}
界定符把这个值装配到Bean的属性中
<property name="count" value="#{5}"/>...<!--浮点型数字--><property name="frequence" value="#{89.7}"/><!--科学计数法--><property name="capacity" value="#{1e4}"/><!--布尔型--><property name="enable" value="#{false}"/>
#{}
标记会提示Spring这个标记里的内容是SpEL表达式,它们还可以与非SpEL表达式的值混用:
<property name="count" value="The value is #{5}"/>
String类型的字面值可以使用单引号或双引号作为字符串的界定符:
<property name="name" value="#{'Chuck'}"/><!--或--><property name="name" value='#{"Chuck"}'/>
引用Bean、Properties和方法
在SpEL表达式中使用Bean ID将一个Bean装配到另一个Bean的属性中:
<property name="instrument" value="#{saxophone}"/><!--其等价于--><property name="instrument" ref="saxophone"/>
在SpEL表达式中使用Bean的引用来获取Bean的属性:
<bean id="carl" class="com.springinaction.springidol.Instrumentalist"> <property name="song" vaule="#{kenny.song}"/></bean>
其等价于以下代码
Instrumentalist carl = new Instrumentalist();carl.setSong(kenny.getSong());
在SpEL表达式中使用Bean的引用来调用Bean的方法,假设这里有一个songSelector Bean,该Bean有一个selectSong()方法:
<property name="song" value="#{songSelector.selectSong()}"/>
在SpEL表达式中使用null-safe存取器避免抛出空指针异常(NullPointerException):
<!--没有使用null-safe存取器--><property name="song" value="#{songSelector.selectSong().toUpperCase()}"/><!--使用null-safe存取器--><property name="song" value="#{songSelector.selectSong()?.toUpperCase()}"/>
如果没有使用null-safe存取器,则selectSong()返回null值会导致空指针异常,而使用null-safe存取器,即使用?.
运算符代替点.
来访问toUpperCase()方法,在访问右方法之前,该运算符会确保左边项的值不会为null。如果为null,则SpEL不再尝试调用toUpperCase()方法。
操作类:
在SpEL中,使用T()运算符会调用类作用域的方法和常量。
在SpEL表达式中使用T()运算符将指定类的静态常量装配到Bean的一个属性中:
<!--只需简单引用Math类的PI常量即可--><property name="multiplier" value="#{T(java.lang.Math).PI}"/>
使用T()运算符调用静态方法:
<!--将一个随机数(在0到1之间)装配到Bean的一个属性中--><property name="randomNumber" value="#{T(java.lang.Math)random()}"/>
0 0
- 使用SpEL表达式装配Bean
- Spring使用SpEL表达式装配Bean
- Spring:spring表达式-SPEL , 使用表达式装配bean属性。
- Spring 使用表达式进行装配 SpEL表达式
- Spring3.x--使用SpEL表达式装配
- [Spring实战系列](11)SpEL使用表达式装配
- spring ioc容器注入的集中方式,以及属性注入的集中依赖,继承,使用SPEL表达式,ioc容器的生命周期,bean的几种工厂方法配置方式,bean的自动装配
- SpEL表达式使用记录
- 深入学习Spring框架之七SpEL表达式装配
- (笔记)Spring实战_装配Bean(3)_使用表达式装配
- bean的继承类和使用spel
- 使用spring进行bean装配
- 使用@Autowired自动装配bean
- Spring 使用注解装配Bean
- Spring 配置使用 - 装配 Bean
- 使用注解自动化装配Bean
- spring4-2-bean配置-7-Spring表达式语言SpEL
- spel表达式
- 华为机试题【10】-求数字基root
- 第2章 一切都是对象
- Log4j的学习--Log4j 配置文件
- SGU 495 Kids and Prizes
- 【一天一道LeetCode】#160. Intersection of Two Linked Lists
- 使用SpEL表达式装配Bean
- OpenGL学习笔记5:着色器
- POJ 2182 Lost Cows
- 向量夹角顺时针或逆时针,交叉口向左拐向右拐的问题
- 第3章 操作符
- Kinect基础之在WinForm(C#)中显示彩色数据和深度数据
- Leetcode-minimum-depth-of-binary-tree
- STL基础(二)
- 第4章 控制执行流程