
一、SpEL介绍
Spring表达式语言全称为“Spring Expression Language”,缩写为“SpEL”,可在运行时构建复杂表达式
使用步骤:
1)创建解析器:ExpressionParser接口表示解析器,SpelExpressionParser提供默认实现
2)解析表达式:使用ExpressionParser.parseExpression()方法,将表达式解析为Expression对象
3)构造上下文:用于定义变量,由EvaluationContext接口表示,StandardEvaluationContext提供默认实现
4)求值:使用Expression.getValue()方法,根据上下文求得表达式值
@Test
public void testSpel() {
// spel解析器
ExpressionParser parser = new SpelExpressionParser(); // spel语句
Expression expression1 = parser.parseExpression("895");
Assert.assertTrue(895 == expression1.getValue(int.class)); // spel语句
Expression expression2 = parser.parseExpression("'hello'");
Assert.assertTrue(StringUtils.equals("hello", expression2.getValue(String.class))); // spel上下文
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("end", "zz");
Expression expression3 = parser.parseExpression("('yy' + 'pp').concat(#end)");
Assert.assertTrue(StringUtils.equals("yyppzz", expression3.getValue(context, String.class)));
}
二、在bean定义中使用SpEL
ApplicationContext实现默认支持SpEL,在Bean定义时注入时,使用“#{SpEL表达式}”表示
1)xml方式
<bean id="str1" class="java.lang.String">
<constructor-arg index="0" value="uouu"/>
</bean> <bean id="propBean" class="java.lang.String">
<constructor-arg index="0" value="#{str1}"/>
</bean>
@Test
public void testSpel2() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
String prop = context.getBean("propBean", String.class);
Assert.assertTrue(StringUtils.equals("uouu", prop));
}
2)注解方式
public class Hello {
@Value("#{str1}")
private String password; public String getPassword() {
return password;
}
}
<!-- 定义bean -->
<bean class="cn.matt.spel.Hello"></bean> <!-- 开启注解 -->
<context:annotation-config />
@Test
public void testSpel3() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
Hello hello = context.getBean(Hello.class);
Assert.assertTrue(StringUtils.equals("uouu", hello.getPassword()));
}
三、属性文件的使用
spring属性文件配置方式:
<!-- 全写方式 -->
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>jdbc1.properties</value>
<value>jdbc2.properties</value>
</list>
</property>
</bean>
上述配置的简写方式:
<!-- 简写方式 -->
<context:property-placeholder location="classpath:jdbc1.properties,classpath:jdbc2.properties"/>
使用实例如下:
# jdbc1.properties 文件
username=root
password=root
# jdbc2.properties 文件
username=admin
password=admin
<bean id="propBean" class="java.lang.String">
<constructor-arg index="0" value="${password}"/>
</bean>
@Test
public void testSpel2() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
String prop = context.getBean("propBean", String.class);
Assert.assertTrue(StringUtils.equals("admin", prop));
}
注:
1)spring使用“${属性名}”表示属性值,与SpEL使用"#{SpEL表达式}"不同
2)在多个属性文件中存在相同的属性值时,后者覆盖前者
3)支持注解方式,使用方式与SpEL注解类似,如:@Value("${password}")
参考:
第五章 Spring表达式语言 之 5.1 概述 5.2 SpEL基础 ——跟我学spring3
第五章 Spring表达式语言 之 5.3 SpEL语法 ——跟我学spring3
第五章 Spring表达式语言 之 5.4在Bean定义中使用EL—跟我学spring3