SpEL、PropertyPlaceholderConfigurer与@Value、#{}、${}
概念
SpEL:Spring EL表达式
PropertyPlaceholderConfigurer:即org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.java。我们通常通过配置一个PropertyPlaceholderConfigurer类型的bean,来将properties文件交给Spring来托管。
@Value、#{}、${}:通过@Value、#{}、${},可以读取受Spring托管的配置。(被托管的配置可能是properties文件、常量类的bean)
一、Spring EL表达式
EL表达式三个概念:
- 1. expression(表达式)
- 2. context(上下文)
- 3. root object(根对象)
Expression是从指定的上下文中获取相应的内容。
也就是说,Expression指定了要获取的property name,但还需要指定从什么地方获取该property value。
EL中可以从上下文context和root object中获取property value。
org.springframework.expression.Expression中提供了很多取property value的方法,如下几个比较重要:
从default standard context中获取property value
/**
* Evaluate this expression in the default standard context.
* @return the evaluation result
* @throws EvaluationException if there is a problem during evaluation
*/
Object getValue() throws EvaluationException;
从指定的context中获取property value
/**
* Evaluate this expression in the provided context and return the result of evaluation.
* @param context the context in which to evaluate the expression
* @return the evaluation result
* @throws EvaluationException if there is a problem during evaluation
*/
Object getValue(EvaluationContext context) throws EvaluationException;
从指定的root object中获取property value
/**
* Evaluate this expression against the specified root object
* @param rootObject the root object against which properties/etc will be resolved
* @return the evaluation result
* @throws EvaluationException if there is a problem during evaluation
*/
Object getValue(Object rootObject) throws EvaluationException;
EL表达式的功能
| 英文 | 中文 |
|---|---|
| Literal expressions | 字面值表达式 |
| Boolean and relational operators | 布尔和关系操作符 |
| Regular expressions | 正则表达式 |
| Class expressions | 类表达式 |
| Accessing properties, arrays, lists, maps | 访问properties、arrays、lists、maps |
| Method invocation | 方法调用 |
| Relational operators | 关系操作符 |
| Assignment | 赋值 |
| Calling constructors | 调用构造器 |
| Bean references | bean引用 |
| Array construction | 构建数组 |
| Inline lists | 内联lists |
| Inline maps | 内联maps |
| Ternary operator | 三元操作符 |
| Variables | 变量 |
| User defined functions | 用户定义的功能 |
| Collection projection | 集合投影 |
| Collection selection | 集合选择 |
| Templated expressions | 模板化表达式 |
参考:
[1]http://www.cnblogs.com/larryzeal/p/5964621.html
[2]http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#expressions
二、 @Value、#{}、${}
http://www.cnblogs.com/larryzeal/p/5910149.html
Spring的属性注入,都依赖属性文件是受Spring容器管理的。
可以这么说,想要使用Spring的功能,那么,你的资源(实体、类、资源文件等)都必需受Spring容器的管理。当然,properties文件也不例外。
Spring对#{}和${}的支持,是通过org.springframework.expression.common.TemplateAwareExpressionParser来实现的。
public Expression parseExpression(String expressionString, ParserContext context)
throws ParseException {
if (context == null) {
context = NON_TEMPLATE_PARSER_CONTEXT;
}
if (context.isTemplate()) { // 按${}来处理
return parseTemplate(expressionString, context);
}
else { // 按#{}来处理,即使用EL表达式来处理
return doParseExpression(expressionString, context);
}
}

EL对应的解析器:StandardBeanExpressionResolver
要将配置文件给Spring管理的话,一般会使用如下配置:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/props/jdbc.properties</value>
</list>
</property>
</bean>
这样我们就能在Spring环境下来使用它了。
@Value
@Value的值有两类:
- ① ${ property : default_value }
- ② #{ obj.property? : default_value }
就是说,第一个注入的是参数对应的property,第二个则是SpEL表达式对应的内容。 那个 default_value,就是前面的值为空时的默认值。注意二者的不同。
${key}
取properties文件中key对应的value。本质上是取PropertyPlaceholderConfigurer这个bean中的key对应的value。
#{object.field}
#是SpEl表达式,它取的是对象的属性。注意,这个对象object必需是Spring的Bean。
#{'${key}'}
$与#可以结合起来一起使用。可以参看例子:com.cn.kvn.framework.property_test.spel.SpelDomain.java
SpEL、PropertyPlaceholderConfigurer与@Value、#{}、${}的更多相关文章
- Spring 使用介绍(四)—— SpEL
一.SpEL介绍 Spring表达式语言全称为“Spring Expression Language”,缩写为“SpEL”,可在运行时构建复杂表达式 使用步骤: 1)创建解析器:ExpressionP ...
- Spring学习记录(七)---表达式语言-SpEL
SpEL---Spring Expression Language:是一个支持运行时查询和操作对象图表达式语言.使用#{...}作为定界符,为bean属性动态赋值提供了便利. ①对于普通的赋值,用Sp ...
- PropertiesFactoryBean PropertyPlaceholderConfigurer 区别
正如 stackoverflow上说的,PropertiesFactoryBean 是PropertiesLoaderSupport 直接的实现类, 专门用来管理properties文件的工厂bean ...
- Spring PropertyPlaceholderConfigurer数据库配置
pom.xml中添加依赖 <!-- mysql-connector-java --> <dependency> <groupId>mysql</groupId ...
- spring-表达式语言-SpEL【转】
Spring表达式语言(Spring Expression Language)简称:SpEL 课程概要: Spring表达式语言的入门介绍 Spring表达式语言的操作范围 Spring表达式语言的运 ...
- spring的多个PropertyPlaceholderConfigurer实例装配的问题
1. 默认情况下,使用PropertyPlaceholderConfigurer多实例装配出现异常 在项目中尝试 在不同的spring的配置文件中分别引入相应的properties文件,这样会在spr ...
- Spring中的SPEL
src\dayday\Person.java package dayday;/** * Created by I am master on 2016/11/28. */public class Per ...
- Spring里PropertyPlaceholderConfigurer类的使用
1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现.PropertyPlaceho ...
- spring spel
•Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言. •语法类似于 EL:SpEL 使用 #{…} 作为定界符,所有在大框号中的字符都将被认为是 SpE ...
随机推荐
- 手动释放linux内存cache
总有很多朋友对于Linux的内存管理有疑问,之前一篇linux下的内存管理方式似乎也没能清除大家的疑虑.而在新版核心中,似乎对这个问题提供了新的解决方法,特转出来给大家参考一下.最后,还附上我对这方法 ...
- 《C++程序设计教程——给予Visual Studio 2008》读书笔记1,2章
double *p1; //p1为指向double型的指针变量 POINT *p2; //p2为指向POINT型(点类型)的指针变量 int (*p3)[6]; //p ...
- Php5.5新特性 Generators详解
在PHP5.5.0版本中,新增了生成器(Generators)特性,用于简化实现迭代器接口(Iterator)创建简单的迭代器的复杂性. 通过生成器,我们可以轻松的使用foreach迭代一系列的数据, ...
- minerd
云服务器 ECS Linux 异常进程 minerd 导致系统 CPU 跑满 问题现象 云服务器 ECS Linux 服务器 CPU 跑满,或者使用服务器越来越慢. 问题原因 使用 top 命令看到有 ...
- Navi.Soft31.代码生成器(含下载地址)
1系统简介 1.1功能简述 在Net软件开发过程中,大部分时间都是在编写代码,并且都是重复和冗杂的代码.比如:要实现在数据库中10个表的增删改查功能,大部分代码都是相同的,只需修改10%的代码量.此时 ...
- Android圆角图片汇总
今天来对图片的圆角处理做一个简单小结,很多app里面都有圆角效果,根据不同的场景可以采用不同的方案,目前来说有三种方案是比较常用的 方案一 .9.png 应用场景:1.目标图片已知:2.针对布局背景; ...
- 数据表记录包含表索引和数值,请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照key值升序进行输出。
题目描述 数据表记录包含表索引和数值,请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照key值升序进行输出. 输入描述: 先输入键值对的个数然后输入成对的index和value值 ...
- git远程库与本地联系报错fatal: Not a git repository (or any of the parent directories): .git
在github上新建了一个仓库,然后相与本地的仓库联系起来 $ git remote add origin https://github.com/liona329/learngit.git fatal ...
- nodemon是个好东西
不说话,直接上图: 安装 使用
- C#基础---------------C#正则表达式2
C#正则表达式语法规则详解 正则表达式基础知识 一个正则表达式就是由普通字符(例如字符 a 到 z)以及特殊字符(称为元字符)组成的文字模式. 字母文本指的是普通文本如"a ...