Spring 使用介紹(四)—— SpEL
一、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
spring <context:property-placeholder>使用說明
使用<context:property-placeholder>標簽導入多個properties文件
Spring 使用介紹(四)—— SpEL