相对来讲, java是一门静态语言。而我们今天要讲的是一门动态“语言”---SpEL。
动态语言和静态语言的最显著差别在于,举个例子," 'Hello'.toUperCase()"这只是一个普通的字符串,差别在于能否把它编译和运行起来得到结果。就是说动态语言能把一个字符串解释成程序语句。如果还不是很明白的话没关系,看下面的SpEL例子。(接下去的我就用例子来做介绍了)
public void testSpEL1() {
//ExpressionParser是Spring3里的一个包,用来动态解释一个字符串。
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression(" 'Hello,World' ");
System.out.println((String)exp.getValue());
}
这里'Hello,World' 是一个字符串,解释起来还是一个字符串,所以打印结果是:Hello,World
第二个例子:调用方法
public void testSpEL2() {
ExpressionParser parser = new SpelExpressionParser();
Expression exp=parser.parseExpression(" 'Hello'.concat(' World!')"); //这里调用了字符串String的concat方法
//Expression exp=parser.parseExpression("newString('helloworld').toUpperCase()");
Stringmessage=(String)exp.getValue();
}
第三个例子:调用属性
ExpressionParser parser= new SpelExpressionParser();
Expression exp=parser.parseExpression("'HelloWorld'.bytes"); //得到字符串的byte
//Expression exp=parser.parseExpression("'HelloWorld'.bytes.length"); //得到属性的属性
byte [] bytes=(byte[])exp.getValue();
第四个例子:调用类中的属性(下面开始复杂起来了)
@Resource
public User user; //注入之后,user.getName() 为 xiaoling
public void testSpEL() {
//设“值域”,限定在u这个对象里
EvaluationContext context = new StandardEvaluationContext(user);
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("username"); //==user.getUsername()
System.out.println((String)exp.getValue(context)); //结果:xiaoling
}
或者用更简洁的一个方式 :
System.out.println((String)exp.getValue(user));
这样就不用设值域了!呵呵!
第五个例子:给对象中的属性设值
StandardEvaluationContext context = new StandardEvaluationContext(user);
parser.parseExpression("username").setValue(context, "ling");
第六个例子:做判断
Expression exp=parser.parseExpression("name=='xiaoling' ");
boolean result=exp.getValue(context,Boolean.class); //evaluate stotrue
打印出来,结果为true
看到这,可能你还是一头雾水:这算什么新特性啊,哪有什么用处!
用处就在于,SpEL把java变成“动”的了!(纯属我个人观点!!)
接下来,我们把SpEL用到bean.xml中去
先看个例子(例一)
<bean id="numberGuess" class="cn.ling.spel.NumberGuess">
<property name="randomNumber" value="#{T(java.lang.Math).random()*100.0}"/>
</bean>
怎么样,可以在xml文件里面赋值了!呵呵!
value里面都用#{}来赋值。至于T(java.lang.Math),它是指某个类,即Math这个类。
看这个例子(例二):systemProperties
<property name="defaultLocale" value="#{systemProperties['user.region']}"/>
systemProperties是预定义好的,里面有很属性可以用,它就相当于java文件中的System.getProperty("user.region"),它能取出当前计算机所在的国家。
例三:引用xml里面的其它bean
<bean id="numberGuess" class="org.spring.samples.NumberGuess">
<property name="randomNumber" value="#{T(java.lang.Math).random()*100.0}"/>
<!--otherproperties-->
</bean>
<bean id="shapeGuess" class="org.spring.samples.ShapeGuess">
<property name="initialShapeSeed" value="#{numberGuess.randomNumber}"/>
<!--otherproperties-->
</bean>
SpEL还可以用在annotation中:
例一:用在属性上
public static class FieldValueTestBean{
@Value("#{systemProperties['user.region']}")
private String defaultLocale;
//set和get方法
public void setDefaultLocale(StringdefaultLocale) {
this.defaultLocale=defaultLocale;
}
public StringgetDefaultLocale(){
returnthis.defaultLocale;
}
}
例二:用在方法上
public static class PropertyValueTestBean{
private String defaultLocale;
@Value("#{systemProperties['user.region']}")
public void setDefaultLocale(String defaultLocale){
this.defaultLocale=defaultLocale;
}
public String getDefaultLocale(){
return this.defaultLocale;
}
}
}
例三:Autowired和构造器中
public class SimpleMovieLister{
private MovieFinder movieFinder;
private String defaultLocale;
@Autowired
public void configure(MovieFinder movieFinder,
@Value("#{systemProperties['user.region']}"}String defaultLocale){
this.movieFinder=movieFinder;
this.defaultLocale=defaultLocale;
}
//...
}
未完待续…………
分享到:
相关推荐
SpringBoot 自定义注解,属性支持SPEL表达式。介绍了SPEL表达式的解析
Spring 3.0中,新的表达式语言名叫Spring Expression Language(SpEL,直译就是Spring表达式语言)。Shaun将其描述为“嵌入在Spring编程模型上的,可以用来在Spring项目之间描述任务以及配置的微型脚本语言。SpEL...
◆Spring表达式(SpEL):用于bean定义的核心表达式分析器 ◆对基于注释的组件的更多支持:允许通过元注释创建注释的“快捷方式” ◆标准化的依赖性注入注释:对Java中依赖性注入的JSR-330标准的完整支持 ◆基于...
Spring 3.0中,新的表达式语言名叫Spring Expression Language(SpEL,直译就是Spring表达式语言)。Shaun将其描述为“嵌入在Spring编程模型上的,可以用来在Spring项目之间描述任务以及配置的微型脚本语言。SpEL...
spring spEL 表达式详解 运行环境:eclipse 构建工具:maven 不提供maven构建,maven用来解决jar包的依赖
Spring_SpEl表达式使用用例 只是一个简单的demo,有需要的可以看看
CVE-2022-22947 SpringCloud GateWay SpEL RCE.doc
SSH笔记-Spring表达式语言:SpEL,关于SpEl的字面量、引用 Bean、属性和方法、支持的运算符号
Dynamic-Datasource (opens new window)- 基于 SpringBoot 的多数据源组件,功能强悍,支持 Seata ...提供使用 spel动态参数 解析数据源方案。内置spel,session,header,支持自定义。 支持 多层数据源嵌套切换 。
Spring Expression Language (SpEL)中文文档。基于Spring4.x。
Dynamic-Datasource (opens new window)- 基于 SpringBoot 的多数据源组件,功能强悍,支持 Seata ...提供使用 spel动态参数 解析数据源方案。内置spel,session,header,支持自定义。 支持 多层数据源嵌套切换 。
主要介绍了Spring spel表达式使用方法示例,通过一些实例向大家展示了spel表达式的用法,需要的朋友可以了解下。
Spring框架中的spring-messaging模块提供了一种基于WebSocket的STOMP协议实现,STOMP消息代理在处理客户端消息时存在SpEL表达式注入漏洞,因此攻击者可以通过构造恶意的消息来实现远程代码执行。 Windows平台...
今天小编就为大家分享一篇关于Spring组件开发模式支持SPEL表达式,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
用于Spring Cloud Function SPEL表达式注入漏洞测试环境搭建,是编译好的服务端程序,命令号java -jar *.jar运行即可,服务端运行在127.0.0.1:8080端口
Spring Cloud Gateway Actuator API SpEL表达式注入命令执行 0day 漏洞复现
spring-framework.zip,spring表达式语言(spel)spring框架
主要介绍了spring表达式语言SpEL用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
- SpEL (Spring Expression Language) - Caching using Spring's cache abstraction - Sending and receiving JMS messages using Spring - Aspect-oriented programming support in Spring - Sending emails using ...