开涛spring3(5.4) - Spring表达式语言 之 5.4在Bean定义中使用EL
5.4.1 xml风格的配置
SpEL支持在Bean定义时注入,默认使用“#{SpEL表达式}”表示,其中“#root”根对象默认可以认为是 ApplicationContext,只有ApplicationContext实现默认支持SpEL,获取根对象属性其实是获取容器中的Bean。
首先看下配置方式(chapter5/el1.xml)吧:
<bean id="world" class="java.lang.String">
<constructor-arg value="#{' World!'}"/>
</bean>
<bean id="hello1" class="java.lang.String">
<constructor-arg value="#{'Hello'}#{world}"/>
</bean>
<bean id="hello2" class="java.lang.String">
<constructor-arg value="#{'Hello' + world}"/>
<!-- 不支持嵌套的 -->
<!--<constructor-arg value="#{'Hello'#{world}}"/>-->
</bean>
<bean id="hello3" class="java.lang.String">
<constructor-arg value="#{'Hello' + @world}"/>
</bean>
模板默认以前缀“#{”开头,以后缀“}”结尾,且不允许嵌套,如“#{'Hello'#{world}}”错误,如“#{'Hello' + world}”中“world”默认解析为Bean。当然可以使用“@bean”引用了。
接下来测试一下吧:
@Test
public void testXmlExpression() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter5/el1.xml");
String hello1 = ctx.getBean("hello1", String.class);
String hello2 = ctx.getBean("hello2", String.class);
String hello3 = ctx.getBean("hello3", String.class);
Assert.assertEquals("Hello World!", hello1);
Assert.assertEquals("Hello World!", hello2);
Assert.assertEquals("Hello World!", hello3);
}
是不是很简单,除了XML配置方式,Spring还提供一种注解方式@Value,接着往下看吧。
5.4.2 注解风格的配置
基于注解风格的SpEL配置也非常简单,使用@Value注解来指定SpEL表达式,该注解可以放到字段、方法及方法参数上。
测试Bean类如下,使用@Value来指定SpEL表达式:
package cn.javass.spring.chapter5;
import org.springframework.beans.factory.annotation.Value;
public class SpELBean {
@Value("#{'Hello' + world}")
private String value;
//setter和getter由于篇幅省略,自己写上
}
首先看下配置文件(chapter5/el2.xml):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<bean id="world" class="java.lang.String">
<constructor-arg value="#{' World!'}"/>
</bean>
<bean id="helloBean1" class="cn.javass.spring.chapter5.SpELBean"/>
<bean id="helloBean2" class="cn.javass.spring.chapter5.SpELBean">
<property name="value" value="haha"/>
</bean>
</beans>
配置时必须使用“<context:annotation-config/>”来开启对注解的支持。
有了配置文件那开始测试吧:
@Test
public void testAnnotationExpression() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter5/el2.xml");
SpELBean helloBean1 = ctx.getBean("helloBean1", SpELBean.class);
Assert.assertEquals("Hello World!", helloBean1.getValue());
SpELBean helloBean2 = ctx.getBean("helloBean2", SpELBean.class);
Assert.assertEquals("haha", helloBean2.getValue());
}
其中“helloBean1 ”值是SpEL表达式的值,而“helloBean2”是通过setter注入的值,这说明setter注入将覆盖@Value的值。
5.4.3 在Bean定义中SpEL的问题
如果有同学问“#{我不是SpEL表达式}”不是SpEL表达式,而是公司内部的模板,想换个前缀和后缀该如何实现呢?
那我们来看下Spring如何在IoC容器内使用BeanExpressionResolver接口实现来求值SpEL表达式,那如果我们通过某种方式获取该接口实现,然后把前缀后缀修改了不就可以了。
此处我们使用BeanFactoryPostProcessor接口提供postProcessBeanFactory回调方法,它是在IoC容器创建好 但还未进行任何Bean初始化时被ApplicationContext实现调用,因此在这个阶段把SpEL前缀及后缀修改掉是安全的,具体代码如下:
package cn.javass.spring.chapter5;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.expression.StandardBeanExpressionResolver;
public class SpELBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException {
StandardBeanExpressionResolver resolver = (StandardBeanExpressionResolver) beanFactory.getBeanExpressionResolver();
resolver.setExpressionPrefix("%{");
resolver.setExpressionSuffix("}");
}
}
首 先通过 ConfigurableListableBeanFactory的getBeanExpressionResolver方法获取 BeanExpressionResolver实现,其次强制类型转换为StandardBeanExpressionResolver,其为 Spring默认实现,然后改掉前缀及后缀。
开始测试吧,首先准备配置文件(chapter5/el3.xml):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<bean class="cn.javass.spring.chapter5.SpELBeanFactoryPostProcessor"/>
<bean id="world" class="java.lang.String">
<constructor-arg value="%{' World!'}"/>
</bean>
<bean id="helloBean1" class="cn.javass.spring.chapter5.SpELBean"/>
<bean id="helloBean2" class="cn.javass.spring.chapter5.SpELBean">
<property name="value" value="%{'Hello' + world}"/>
</bean>
</beans>
配置文件和注解风格的几乎一样,只有SpEL表达式前缀变为“%{”了,并且注册了“cn.javass.spring.chapter5.SpELBeanFactoryPostProcessor”Bean,用于修改前缀和后缀的。
写测试代码测试一下吧:
@Test
public void testPrefixExpression() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter5/el3.xml");
SpELBean helloBean1 = ctx.getBean("helloBean1", SpELBean.class);
Assert.assertEquals("#{'Hello' + world}", helloBean1.getValue());
SpELBean helloBean2 = ctx.getBean("helloBean2", SpELBean.class);
Assert.assertEquals("Hello World!", helloBean2.getValue());
}
此 处helloBean1 中通过@Value注入的“#{'Hello' + world}”结果还是“#{'Hello' + world}”说明不对其进行SpEL表达式求值了,而helloBean2使用“%{'Hello' + world}”注入,得到正确的“"Hello World!”。
开涛spring3(5.4) - Spring表达式语言 之 5.4在Bean定义中使用EL的更多相关文章
- Spring表达式语言 之 5.4在Bean定义中使用EL(拾伍)
5.4.1 xml风格的配置 SpEL支持在Bean定义时注入,默认使用"#{SpEL表达式}"表示,其中"#root"根对象默认可以认为是Applicati ...
- 开涛spring3(5.3) - Spring表达式语言 之 5.3 SpEL语法
5.3 SpEL语法 5.3.1 基本表达式 一.字面量表达式: SpEL支持的字面量包括:字符串.数字类型(int.long.float.double).布尔类型.null类型. 类型 示例 字 ...
- 开涛spring3(12.3) - 零配置 之 12.3 注解实现Bean定义
12.3 注解实现Bean定义 12.3.1 概述 前边介绍的Bean定义全是基于XML方式定义配置元数据,且在[12.2注解实现Bean依赖注入]一节中介绍了通过注解来减少配置数量,但并没有完全 ...
- 开涛spring3(12.2) - 零配置 之 12.2 注解实现Bean依赖注入
12.2 注解实现Bean依赖注入 12.2.1 概述 注解实现Bean配置主要用来进行如依赖注入.生命周期回调方法定义等,不能消除XML文件中的Bean元数据定义,且基于XML配置中的依赖注入的 ...
- Spring3系列6 - Spring 表达式语言(Spring EL)
Spring3系列6-Spring 表达式语言(Spring EL) 本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.S ...
- 开涛spring3(12.4) - 零配置 之 12.4 基于Java类定义Bean配置元数据
12.4 基于Java类定义Bean配置元数据 12.4.1 概述 基于Java类定义Bean配置元数据,其实就是通过Java类定义Spring配置元数据,且直接消除XML配置文件. 基于Java ...
- 开涛spring3(9.4) - Spring的事务 之 9.4 声明式事务
9.4 声明式事务 9.4.1 声明式事务概述 从上节编程式实现事务管理可以深刻体会到编程式事务的痛苦,即使通过代理配置方式也是不小的工作量. 本节将介绍声明式事务支持,使用该方式后最大的获益是简 ...
- 开涛spring3(9.2) - Spring的事务 之 9.2 数据库事务概述
9.2.1 概述 Spring框架支持事务管理的核心是事务管理器抽象,对于不同的数据访问框架(如Hibernate)通过实现策略接口 PlatformTransactionManager,从而能支持 ...
- 开涛spring3(9.3) - Spring的事务 之 9.3 编程式事务
9.3 编程式事务 9.3.1 编程式事务概述 所谓编程式事务指的是通过编码方式实现事务,即类似于JDBC编程实现事务管理. Spring框架提供一致的事务抽象,因此对于JDBC还是JTA事务都是 ...
随机推荐
- 腾讯云上PhantomJS用法示例
崔庆才 前言 大家有没有发现之前我们写的爬虫都有一个共性,就是只能爬取单纯的html代码,如果页面是JS渲染的该怎么办呢?如果我们单纯去分析一个个后台的请求,手动去摸索JS渲染的到的一些结果,那简直没 ...
- .dll 文件编写和使用
1.基本概念 dll(dynamic-link library),动态链接库,是微软实现共享函数库的一种方式.动态链接,就是把一些常用的函数代码制作成dll文件,当某个程序调用到dll中的某个函数的时 ...
- 【Azure】Azure技能树
- Go 并发随机打印1-n
package main import ( "fmt" "math/rand" "sync" "t ...
- GPIO的配置过程
今天看到一篇很好的博文,,看这里:http://www.cnblogs.com/crazyxu/archive/2011/10/14/2212337.html 下面总结一下,加深一下理解. 要使用GP ...
- JS中字符串与数组的一些常用方法
真是恨透了这些类似于substring substr slice 要么长得像,要么就功能相近的方法... 1⃣️string 1.substring(start开始位置的索引,end结束位置索引) 截 ...
- python——面向对象进阶
类的成员 类的成员可以分为三大类:字段.方法和属性 注:所有成员中,只有普通字段的内容保存对象中,即:根据此类创建了多少对象,在内存中就有多少个普通字段.而其他的成员,则都是保存在类中,即:无论对象的 ...
- 纯JS单页面赛车游戏代码分享
分享一个以前写的小游戏,纯js游戏,代码很简单.欢迎大家来喷呦! 效果图: 代码展示://直接复制到html文件即可 支持IE9+版本 <!DOCTYPE html> <html&g ...
- mui开发app之webview是什么
WebView(网络视图)能加载显示网页,可以将其视为一个浏览器,webview被封装在html5+,plus对象中,底层由java,OC实现. 先来谈谈我对webview的理解: 使用mui开发的a ...
- 【lucene系列学习二】Lucene实现高亮显示关键词
首先,导入下图所示库 然后,import org.apache.lucene.search.highlight.*; 下面,我们新建一个实现高亮显示功能的函数 public static String ...