SpEL表达式
SpEL表达式概述
1、什么是SpEL表达式
- SpEL:(spring expression language) 是一种表达式语言,是一种强大,简洁的装配Bean的方式。
- 他可以通过运行期间执行的表达式
将值装配到我们的属性或构造函数当中
,也可以调用JDK中提供的静态常量
,获取外部Properties文件
中的的配置。
2、SpEL表达式的作用
- 能够更加简单,多元的装配Bean,弥补了XML静态注入的不足。
- 简化开发,减少了一些逻辑、配置的编写,使代码更加简洁。
SpEL表达式的格式:#{表达式}
SpEL的使用方式
1、xml配置的方式
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="MyMessage" class="com.jp.MyMessage">
<property name="message" value="#{systemProperties['user.language']}"></property>
</bean>
</beans>
代码
package com.jp;
public class MyMessage {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
测试
package com.jp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context =new