概念

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

最新文章

  1. poj1741 (点分治)
  2. 如何在Web服务器80端口上开启SSH服务
  3. NGif, Animated GIF Encoder for .NET
  4. 【JavaScript】重温Javascript继承机制
  5. 五、SQL映射的XML文件
  6. 解决办法:CMake编译时出现“error in configuration process project files may be invalid”
  7. mysql copy复制拷贝表数据及结构的几种方式(转)
  8. Spring aop切面插入事物回滚
  9. CentOS7的网卡启动不起来的问题
  10. shell编程企业级实战(2)
  11. PHP金钱数字转金钱大写
  12. 调整linux进程优先级
  13. adb devices 找不到设备的解决方法
  14. F#周报2019年第4期
  15. 跟随我在oracle学习php(7)
  16. Windows android appium python3 环境搭建
  17. mybatis的selectOne和selectList没有数据返回时的问题
  18. 概率主题模型简介 Introduction to Probabilistic Topic Models
  19. 关于JSON的解析方式
  20. GPL、BSD和Apache开源许可证

热门文章

  1. Java编程的逻辑 (54) - 剖析Collections - 设计模式
  2. HTML5里的input标签的required属性的提示
  3. oracle的启动和停用
  4. r语言 工作空间内的对象
  5. 安卓程序代写 网上程序代写[原]自定义View
  6. 【转】如果有人让你推荐Python技术书,请让他看这个列表
  7. QTcpSocket的连续发送数据和连续接收数据
  8. 关于Unity中的特殊文件目录和资源管理(专题四)
  9. 单表多个Count 条件进行查询拼接小妙用
  10. eclipse里面的常用快捷键