<div id="article_content" class="article_content csdn-tracking-statistics tracking-click" data-mod="popu_519" data-dsm="post">
<div >
<h2 style="line-height:1.5em; margin:0px 0px 0.5em; padding:0px; font-family:Verdana,Arial,Helvetica,sans-serif"><a name="t0"></a>
5.1 概述</h2>
<h3 style="font-size:1.2em; line-height:1.5em; margin:0px 0px 0.5em; padding:0px; font-family:Verdana,Arial,Helvetica,sans-serif"><a name="t1"></a>
5.1.1 概述</h3>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
Spring表达式语言全称为“Spring Expression Language”,缩写为“SpEL”,类似于Struts2x中使用的OGNL表达式语言,能在运行时构建复杂表达式、存取对象图属性、对象方法调用等等,并且能与Spring功能完美整合,如能用来配置Bean定义。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
表达式语言给静态Java语言增加了动态功能。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
SpEL是单独模块,只依赖于core模块,不依赖于其他模块,可以单独使用。</p>
<h3 style="font-size:1.2em; line-height:1.5em; margin:0px 0px 0.5em; padding:0px; font-family:Verdana,Arial,Helvetica,sans-serif"><a name="t2"></a>
5.1.2 能干什么</h3>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
表达式语言一般是用最简单的形式完成最主要的工作,减少我们的工作量。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
SpEL支持如下表达式:</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
<strong>一、基本表达式:</strong>字面量表达式、关系,逻辑与算数运算表达式、字符串连接及截取表达式、三目运算及Elivis表达式、正则表达式、括号优先级表达式;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
<strong>二、类相关表达式:</strong>类类型表达式、类实例化、instanceof表达式、变量定义及引用、赋值表达式、自定义函数、对象属性存取及安全导航表达式、对象方法调用、Bean引用;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
<strong>三、集合相关表达式:</strong>内联List、内联数组、集合,字典访问、列表,字典,数组修改、集合投影、集合选择;不支持多维内联数组初始化;不支持内联字典定义;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
<strong>四、其他表达式</strong>:模板表达式。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
<strong>注:SpEL表达式中的关键字是不区分大小写的。</strong></p>
<h2 style="line-height:1.5em; margin:0px 0px 0.5em; padding:0px; font-family:Verdana,Arial,Helvetica,sans-serif"><a name="t3"></a>
5.2 SpEL基础</h2>
<h3 style="font-size:1.2em; line-height:1.5em; margin:0px 0px 0.5em; padding:0px; font-family:Verdana,Arial,Helvetica,sans-serif"><a name="t4"></a>
5.2.1 HelloWorld</h3>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
首先准备支持SpEL的Jar包:“org.springframework.expression-3.0.5.RELEASE.jar”将其添加到类路径中。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
SpEL在求表达式值时一般分为四步,其中第三步可选:首先构造一个解析器,其次解析器解析字符串表达式,在此构造上下文,最后根据上下文得到表达式运算后的值。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
让我们看下代码片段吧:</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
</p>
<div style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-weight:bold; font-size:14px; font-family:'arial black','avant garde'">java代码:</span>
<div class="dp-highlighter" id="" style="font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace; font-size:12px; width:661.53125px; overflow:auto; margin-left:9px; padding:1px; word-break:break-all; word-wrap:break-word; background-color:transparent">
<div class="bar">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold">Java代码 <a target="_blank" title="收藏这段代码" style="color:rgb(233,101,14); text-decoration:underline"><img class="star" src="http://jinnianshilongnian.iteye.com/images/icon_star.png" alt="收藏代码" style="border:0px"></a></div>
</div>
<ol start="1" class="dp-j" style="font-size:1em; line-height:1.4em; margin:0px 0px 1px; padding:2px 0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(43,145,175)">
<li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"><span class="keyword" style="color:rgb(127,0,85); font-weight:bold">package</span> cn.javass.spring.chapter5; </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"><span class="keyword" style="color:rgb(127,0,85); font-weight:bold">import</span> junit.framework.Assert; </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"><span class="keyword" style="color:rgb(127,0,85); font-weight:bold">import</span> org.junit.Test; </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"><span class="keyword" style="color:rgb(127,0,85); font-weight:bold">import</span> org.springframework.expression.EvaluationContext; </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"><span class="keyword" style="color:rgb(127,0,85); font-weight:bold">import</span> org.springframework.expression.Expression; </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"><span class="keyword" style="color:rgb(127,0,85); font-weight:bold">import</span> org.springframework.expression.ExpressionParser; </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"><span class="keyword" style="color:rgb(127,0,85); font-weight:bold">import</span> org.springframework.expression.spel.standard.SpelExpressionParser; </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"><span class="keyword" style="color:rgb(127,0,85); font-weight:bold">import</span> org.springframework.expression.spel.support.StandardEvaluationContext; </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"><span class="keyword" style="color:rgb(127,0,85); font-weight:bold">public</span> <span class="keyword" style="color:rgb(127,0,85); font-weight:bold">class</span> SpELTest { </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> <span class="annotation" style="color:rgb(100,100,100)">@Test</span> </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> <span class="keyword" style="color:rgb(127,0,85); font-weight:bold">public</span> <span class="keyword" style="color:rgb(127,0,85); font-weight:bold">void</span> helloWorld() { </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> ExpressionParser parser = <span class="keyword" style="color:rgb(127,0,85); font-weight:bold">new</span> SpelExpressionParser(); </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> Expression expression = </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black">parser.parseExpression(<span class="string" style="color:blue">"('Hello' + ' World').concat(#end)"</span>); </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> EvaluationContext context = <span class="keyword" style="color:rgb(127,0,85); font-weight:bold">new</span> StandardEvaluationContext(); </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> context.setVariable(<span class="string" style="color:blue">"end"</span>, <span class="string" style="color:blue">"!"</span>); </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> Assert.assertEquals(<span class="string" style="color:blue">"Hello World!"</span>, expression.getValue(context)); </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> } </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black">} </span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
接下来让我们分析下代码:</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
1)创建解析器:SpEL使用ExpressionParser接口表示解析器,提供SpelExpressionParser默认实现;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
2)解析表达式:使用ExpressionParser的parseExpression来解析相应的表达式为Expression对象。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
3)构造上下文:准备比如变量定义等等表达式需要的上下文数据。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
4)求值:通过Expression接口的getValue方法根据上下文获得表达式值。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
是不是很简单,接下来让我们看下其具体实现及原理吧。</p>
<h3 style="font-size:1.2em; line-height:1.5em; margin:0px 0px 0.5em; padding:0px; font-family:Verdana,Arial,Helvetica,sans-serif"><a name="t5"></a>
5.2.3 SpEL原理及接口</h3>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
SpEL提供简单的接口从而简化用户使用,在介绍原理前让我们学习下几个概念:</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
<strong>一、表达式:</strong>表达式是表达式语言的核心,所以表达式语言都是围绕表达式进行的,从我们角度来看是“干什么”;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
<strong>二、解析器:</strong>用于将字符串表达式解析为表达式对象,从我们角度来看是“谁来干”;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
<strong>三、上下文:</strong>表达式对象执行的环境,该环境可能定义变量、定义自定义函数、提供类型转换等等,从我们角度看是“在哪干”;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
<strong>四、根对象及活动上下文对象:</strong>根对象是默认的活动上下文对象,活动上下文对象表示了当前表达式操作的对象,从我们角度看是“对谁干”。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
理解了这些概念后,让我们看下SpEL如何工作的呢,如图5-1所示:</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
<img alt="" src="http://sishuok.com/forum/upload/2012/2/21/f9e368b5b0576e51708f468d8f1b936c__1.JPG" style="border:0px"></p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
图5-1 工作原理</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
1)首先定义表达式:“1+2”;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
2)定义解析器ExpressionParser实现,SpEL提供默认实现SpelExpressionParser;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
2.1)SpelExpressionParser解析器内部使用Tokenizer类进行词法分析,即把字符串流分析为记号流,记号在SpEL使用Token类来表示;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
2.2)有了记号流后,解析器便可根据记号流生成内部抽象语法树;在SpEL中语法树节点由SpelNode接口实现代表:如OpPlus表示加操作节点、IntLiteral表示int型字面量节点;使用SpelNodel实现组成了抽象语法树;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
2.3)对外提供Expression接口来简化表示抽象语法树,从而隐藏内部实现细节,并提供getValue简单方法用于获取表达式值;SpEL提供默认实现为SpelExpression;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
3)定义表达式上下文对象(可选),SpEL使用EvaluationContext接口表示上下文对象,用于设置根对象、自定义变量、自定义函数、类型转换器等,SpEL提供默认实现StandardEvaluationContext;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
4)使用表达式对象根据上下文对象(可选)求值(调用表达式对象的getValue方法)获得结果。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
接下来让我们看下SpEL的主要接口吧:</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
<strong>1)ExpressionParser接口</strong>:表示解析器,默认实现是org.springframework.expression.spel.standard包中的SpelExpressionParser类,使用parseExpression方法将字符串表达式转换为Expression对象,对于ParserContext接口用于定义字符串表达式是不是模板,及模板开始与结束字符:</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
</p>
<div style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-weight:bold; font-size:14px; font-family:'arial black','avant garde'">java代码:</span>
<div class="dp-highlighter" id="" style="font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace; font-size:12px; width:661.53125px; overflow:auto; margin-left:9px; padding:1px; word-break:break-all; word-wrap:break-word; background-color:transparent">
<div class="bar">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold">Java代码 <a target="_blank" title="收藏这段代码" style="color:rgb(233,101,14); text-decoration:underline"><img class="star" src="http://jinnianshilongnian.iteye.com/images/icon_star.png" alt="收藏代码" style="border:0px"></a></div>
</div>
<ol start="1" class="dp-j" style="font-size:1em; line-height:1.4em; margin:0px 0px 1px; padding:2px 0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(43,145,175)">
<li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"><span class="keyword" style="color:rgb(127,0,85); font-weight:bold">public</span> <span class="keyword" style="color:rgb(127,0,85); font-weight:bold">interface</span> ExpressionParser { </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> Expression parseExpression(String expressionString); </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> Expression parseExpression(String expressionString, ParserContext context); </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black">} </span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
来看下示例:</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
</p>
<div style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-weight:bold; font-size:14px; font-family:'arial black','avant garde'">java代码:</span>
<div class="dp-highlighter" id="" style="font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace; font-size:12px; width:661.53125px; overflow:auto; margin-left:9px; padding:1px; word-break:break-all; word-wrap:break-word; background-color:transparent">
<div class="bar">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold">Java代码 <a target="_blank" title="收藏这段代码" style="color:rgb(233,101,14); text-decoration:underline"><img class="star" src="http://jinnianshilongnian.iteye.com/images/icon_star.png" alt="收藏代码" style="border:0px"></a></div>
</div>
<ol start="1" class="dp-j" style="font-size:1em; line-height:1.4em; margin:0px 0px 1px; padding:2px 0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(43,145,175)">
<li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"><span class="annotation" style="color:rgb(100,100,100)">@Test</span> </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"><span class="keyword" style="color:rgb(127,0,85); font-weight:bold">public</span> <span class="keyword" style="color:rgb(127,0,85); font-weight:bold">void</span> testParserContext() { </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> ExpressionParser parser = <span class="keyword" style="color:rgb(127,0,85); font-weight:bold">new</span> SpelExpressionParser(); </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> ParserContext parserContext = <span class="keyword" style="color:rgb(127,0,85); font-weight:bold">new</span> ParserContext() { </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> <span class="annotation" style="color:rgb(100,100,100)">@Override</span> </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> <span class="keyword" style="color:rgb(127,0,85); font-weight:bold">public</span> <span class="keyword" style="color:rgb(127,0,85); font-weight:bold">boolean</span> isTemplate() { </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> <span class="keyword" style="color:rgb(127,0,85); font-weight:bold">return</span> <span class="keyword" style="color:rgb(127,0,85); font-weight:bold">true</span>; </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> } </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> <span class="annotation" style="color:rgb(100,100,100)">@Override</span> </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> <span class="keyword" style="color:rgb(127,0,85); font-weight:bold">public</span> String getExpressionPrefix() { </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> <span class="keyword" style="color:rgb(127,0,85); font-weight:bold">return</span> <span class="string" style="color:blue">"#{"</span>; </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> } </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> <span class="annotation" style="color:rgb(100,100,100)">@Override</span> </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> <span class="keyword" style="color:rgb(127,0,85); font-weight:bold">public</span> String getExpressionSuffix() { </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> <span class="keyword" style="color:rgb(127,0,85); font-weight:bold">return</span> <span class="string" style="color:blue">"}"</span>; </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> } </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> }; </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> String template = <span class="string" style="color:blue">"#{'Hello '}#{'World!'}"</span>; </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> Expression expression = parser.parseExpression(template, parserContext); </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black"> Assert.assertEquals(<span class="string" style="color:blue">"Hello World!"</span>, expression.getValue()); </span></li><li style="font-size:1em; margin:0px 0px 0px 38px; padding:0px 0px 0px 10px; border-left-width:1px; border-left-style:solid; border-left-color:rgb(209,215,220); line-height:18px; background-color:rgb(250,250,250)">
<span style="color:black">} </span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
在此我们演示的是使用ParserContext的情况,此处定义了ParserContext实现:定义表达式是模块,表达式前缀为“#{”,后缀为“}”;使用parseExpression解析时传入的模板必须以“#{”开头,以“}”结尾,如"#{'Hello '}#{'World!'}"。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
默认传入的字符串表达式不是模板形式,如之前演示的Hello World。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
<strong>2)EvaluationContext接口</strong>:表示上下文环境,默认实现是org.springframework.expression.spel.support包中的StandardEvaluationContext类,使用setRootObject方法来设置根对象,使用setVariable方法来注册自定义变量,使用registerFunction来注册自定义函数等等。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
<strong>3)Expression接口:</strong>表示表达式对象,默认实现是org.springframework.expression.spel.standard包中的SpelExpression,提供getValue方法用于获取表达式值,提供setValue方法用于设置对象值。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
了解了SpEL原理及接口,接下来的事情就是SpEL语法了。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
<br>
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:25.2000007629395px">
</p>
<h2 style="line-height:1.5em; margin:0px 0px 0.5em; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"><a name="t6"></a>
5.3 SpEL语法</h2>
<h3 style="font-size:1.2em; line-height:1.5em; margin:0px 0px 0.5em; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"><a name="t7"></a>
5.3.1 基本表达式</h3>
<p></p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px">一、字面量表达式:</span> SpEL支持的字面量包括:字符串、数字类型(int、long、float、double)、布尔类型、null类型。</p>
<table cellpadding="0" border="1" cellspacing="0" style="color:rgb(0,0,0); font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; font-size:12px; padding:0px; margin:0px">
<tbody>
<tr style="padding:0px; margin:0px">
<td width="72" style="font-size:1em; font-family:verdana,arial,helvetica,sans-serif; padding:0px; margin:0px">
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
类型</p>
</td>
<td width="468" style="font-size:1em; font-family:verdana,arial,helvetica,sans-serif; padding:0px; margin:0px">
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
示例</p>
</td>
</tr>
<tr style="padding:0px; margin:0px">
<td width="72" style="font-size:1em; font-family:verdana,arial,helvetica,sans-serif; padding:0px; margin:0px">
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
字符串</p>
</td>
<td width="468" style="font-size:1em; font-family:verdana,arial,helvetica,sans-serif; padding:0px; margin:0px">
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
String str1 = parser.parseExpression("'Hello World!'").getValue(String.class);</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
String str2 = parser.parseExpression("\"Hello World!\"").getValue(String.class);</p>
</td>
</tr>
<tr style="padding:0px; margin:0px">
<td width="72" style="font-size:1em; font-family:verdana,arial,helvetica,sans-serif; padding:0px; margin:0px">
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
数字类型</p>
</td>
<td width="468" style="font-size:1em; font-family:verdana,arial,helvetica,sans-serif; padding:0px; margin:0px">
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
int int1 = parser.parseExpression("1").getValue(Integer.class);</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
long long1 = parser.parseExpression("-1L").getValue(long.class);</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
float float1 = parser.parseExpression("1.1").getValue(Float.class);</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
double double1 = parser.parseExpression("1.1E+2").getValue(double.class);</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
int hex1 = parser.parseExpression("0xa").getValue(Integer.class);</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
long hex2 = parser.parseExpression("0xaL").getValue(long.class);</p>
</td>
</tr>
<tr style="padding:0px; margin:0px">
<td width="72" style="font-size:1em; font-family:verdana,arial,helvetica,sans-serif; padding:0px; margin:0px">
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
布尔类型</p>
</td>
<td width="468" style="font-size:1em; font-family:verdana,arial,helvetica,sans-serif; padding:0px; margin:0px">
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
boolean true1 = parser.parseExpression("true").getValue(boolean.class);</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
boolean false1 = parser.parseExpression("false").getValue(boolean.class);</p>
</td>
</tr>
<tr style="padding:0px; margin:0px">
<td width="72" style="font-size:1em; font-family:verdana,arial,helvetica,sans-serif; padding:0px; margin:0px">
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
null类型</p>
</td>
<td width="468" style="font-size:1em; font-family:verdana,arial,helvetica,sans-serif; padding:0px; margin:0px">
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
Object null1 = parser.parseExpression("null").getValue(Object.class);</p>
</td>
</tr>
</tbody>
</table>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px"> </span></p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px">二、算数运算表达式:</span> SpEL支持加(+)、减(-)、乘(*)、除(/)、求余(%)、幂(^)运算。</p>
<table cellpadding="0" border="1" cellspacing="0" style="color:rgb(0,0,0); font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; font-size:12px; padding:0px; margin:0px">
<tbody>
<tr style="padding:0px; margin:0px">
<td width="72" style="font-size:1em; font-family:verdana,arial,helvetica,sans-serif; padding:0px; margin:0px">
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
类型</p>
</td>
<td width="468" style="font-size:1em; font-family:verdana,arial,helvetica,sans-serif; padding:0px; margin:0px">
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
示例</p>
</td>
</tr>
<tr style="padding:0px; margin:0px">
<td width="72" style="font-size:1em; font-family:verdana,arial,helvetica,sans-serif; padding:0px; margin:0px">
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
加减乘除</p>
</td>
<td width="468" style="font-size:1em; font-family:verdana,arial,helvetica,sans-serif; padding:0px; margin:0px">
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
int result1 = parser.parseExpression("1+2-3*4/2").getValue(Integer.class);//-3</p>
</td>
</tr>
<tr style="padding:0px; margin:0px">
<td width="72" style="font-size:1em; font-family:verdana,arial,helvetica,sans-serif; padding:0px; margin:0px">
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
求余</p>
</td>
<td width="468" style="font-size:1em; font-family:verdana,arial,helvetica,sans-serif; padding:0px; margin:0px">
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
int result2 = parser.parseExpression("4%3").getValue(Integer.class);//1</p>
</td>
</tr>
<tr style="padding:0px; margin:0px">
<td width="72" style="font-size:1em; font-family:verdana,arial,helvetica,sans-serif; padding:0px; margin:0px">
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
幂运算</p>
</td>
<td width="468" style="font-size:1em; font-family:verdana,arial,helvetica,sans-serif; padding:0px; margin:0px">
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:1.5em">
int result3 = parser.parseExpression("2^3").getValue(Integer.class);//8</p>
</td>
</tr>
</tbody>
</table>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
SpEL还提供求余(MOD)和除(DIV)而外两个运算符,与“%”和“/”等价,不区分大小写。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px">三、关系表达式:</span>等于(==)、不等于(!=)、大于(>)、大于等于(>=)、小于(<)、小于等于(<=),区间(between)运算,如“parser.parseExpression("1>2").getValue(boolean.class);”将返回false;而“parser.parseExpression("1 between {1, 2}").getValue(boolean.class);”将返回true。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
between运算符右边操作数必须是列表类型,且只能包含2个元素。第一个元素为开始,第二个元素为结束,区间运算是包含边界值的,即 xxx>=list.get(0) && xxx<=list.get(1)。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
SpEL同样提供了等价的“EQ” 、“NE”、 “GT”、“GE”、 “LT” 、“LE”来表示等于、不等于、大于、大于等于、小于、小于等于,不区分大小写。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px">四、逻辑表达式:</span>且(and)、或(or)、非(!或NOT)。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span style="padding:0px; margin:0px; background-color:inherit">String expression1 = </span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"2>1 and (!true or !false)"</span><span style="padding:0px; margin:0px; background-color:inherit">; </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">boolean</span><span style="padding:0px; margin:0px; background-color:inherit"> result1 = parser.parseExpression(expression1).getValue(</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">boolean</span><span style="padding:0px; margin:0px; background-color:inherit">.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(<span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">true</span><span style="padding:0px; margin:0px; background-color:inherit">, result1); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">String expression2 = <span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"2>1 and (NOT true or NOT false)"</span><span style="padding:0px; margin:0px; background-color:inherit">; </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">boolean</span><span style="padding:0px; margin:0px; background-color:inherit"> result2 = parser.parseExpression(expression2).getValue(</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">boolean</span><span style="padding:0px; margin:0px; background-color:inherit">.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(<span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">true</span><span style="padding:0px; margin:0px; background-color:inherit">, result2); </span></span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
注:<span style="line-height:1.5em; padding:0px; margin:0px">逻辑运算符不支持 Java中的 && 和 || 。</span></p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px">五、字符串连接及截取表达式:</span>使用“+”进行字符串连接,使用“'String'[0] [index]”来截取一个字符,目前只支持截取一个,如“'Hello ' + 'World!'”得到“Hello World!”;而“'Hello World!'[0]”将返回“H”。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px">六、三目运算及Elivis运算表达式:</span></p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
三目运算符 <span style="line-height:1.5em; padding:0px; margin:0px">“表达式1?表达式2:表达式3”</span>用于构造三目运算表达式,如“2>1?true:false”将返回true;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
Elivis运算符<span style="line-height:1.5em; padding:0px; margin:0px">“表达式1?:表达式2”</span>从Groovy语言引入用于简化三目运算符的,当表达式1为非null时则返回表达式1,当表达式1为null时则返回表达式2,简化了三目运算符方式“表达式1? 表达式1:表达式2”,如“null?:false”将返回false,而“true?:false”将返回true;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px">七、正则表达式:</span>使用“str matches regex,如“'123' matches '\\d{3}'”将返回true;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px">八、括号优先级表达式:</span>使用“(表达式)”构造,括号里的具有高优先级。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<h3 style="font-size:1.2em; line-height:1.5em; margin:0px 0px 0.5em; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"><a name="t8"></a>
5.3.3 类相关表达式</h3>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px">一、类类型表达式:</span>使用“T(Type)”来表示java.lang.Class实例,“Type”必须是类全限定名,“java.lang”包除外,即该包下的类可以不指定包名;使用类类型表达式还可以进行访问类静态方法及类静态字段。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
具体使用方法如下:</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="annotation" style="color:rgb(100,100,100); padding:0px; margin:0px; background-color:inherit">@Test</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">public</span><span style="padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">void</span><span style="padding:0px; margin:0px; background-color:inherit"> testClassTypeExpression() { </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> ExpressionParser parser = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> SpelExpressionParser(); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> <span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//java.lang包类访问</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Class<String> result1 = parser.parseExpression(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"T(String)"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(Class.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Assert.assertEquals(String.<span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">, result1); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> <span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//其他包类访问</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> String expression2 = <span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"T(cn.javass.spring.chapter5.SpELTest)"</span><span style="padding:0px; margin:0px; background-color:inherit">; </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Class<String> result2 = parser.parseExpression(expression2).getValue(Class.<span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); Assert.assertEquals(SpELTest.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">, result2); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> <span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//类静态字段访问</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit"> result3=parser.parseExpression(</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"T(Integer).MAX_VALUE"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Assert.assertEquals(Integer.MAX_VALUE, result3); </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> <span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//类静态方法调用</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit"> result4 = parser.parseExpression(</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"T(Integer).parseInt('1')"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Assert.assertEquals(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">1</span><span style="padding:0px; margin:0px; background-color:inherit">, result4); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">} </span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
对于java.lang包里的可以直接使用“T(String)”访问;其他包必须是类全限定名;可以进行静态字段访问如“T(Integer).MAX_VALUE”;也可以进行静态方法访问如“T(Integer).parseInt('1')”。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px">二、类实例化:</span>类实例化同样使用java关键字“new”,类名必须是全限定名,但java.lang包内的类型除外,如String、Integer。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="annotation" style="color:rgb(100,100,100); padding:0px; margin:0px; background-color:inherit">@Test</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">public</span><span style="padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">void</span><span style="padding:0px; margin:0px; background-color:inherit"> testConstructorExpression() { </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> ExpressionParser parser = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> SpelExpressionParser(); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> String result1 = parser.parseExpression(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"new String('haha')"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(String.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Assert.assertEquals(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"haha"</span><span style="padding:0px; margin:0px; background-color:inherit">, result1); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Date result2 = parser.parseExpression(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"new java.util.Date()"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(Date.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Assert.assertNotNull(result2); </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">} </span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
实例化完全跟Java内方式一样。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px">三、instanceof表达式:</span>SpEL支持instanceof运算符,跟Java内使用同义;如“'haha' instanceof T(String)”将返回true。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px">四、变量定义及引用:</span>变量定义通过EvaluationContext接口的setVariable(variableName, value)方法定义;在表达式中使用“#variableName”引用;除了引用自定义变量,SpE还允许引用根对象及当前上下文对象,使用“#root”引用根对象,使用“#this”引用当前上下文对象;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="annotation" style="color:rgb(100,100,100); padding:0px; margin:0px; background-color:inherit">@Test</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">public</span><span style="padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">void</span><span style="padding:0px; margin:0px; background-color:inherit"> testVariableExpression() { </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> ExpressionParser parser = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> SpelExpressionParser(); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> EvaluationContext context = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> StandardEvaluationContext(); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> context.setVariable(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"variable"</span><span style="padding:0px; margin:0px; background-color:inherit">, </span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"haha"</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> context.setVariable(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"variable"</span><span style="padding:0px; margin:0px; background-color:inherit">, </span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"haha"</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> String result1 = parser.parseExpression(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#variable"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context, String.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Assert.assertEquals(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"haha"</span><span style="padding:0px; margin:0px; background-color:inherit">, result1); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> context = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> StandardEvaluationContext(</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"haha"</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> String result2 = parser.parseExpression(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#root"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context, String.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Assert.assertEquals(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"haha"</span><span style="padding:0px; margin:0px; background-color:inherit">, result2); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> String result3 = parser.parseExpression(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#this"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context, String.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Assert.assertEquals(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"haha"</span><span style="padding:0px; margin:0px; background-color:inherit">, result3); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">} </span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
使用“#variable”来引用在EvaluationContext定义的变量;除了可以引用自定义变量,还可以使用“#root”引用根对象,“#this”引用当前上下文对象,此处“#this”即根对象。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px">五、自定义函数:</span>目前只支持类静态方法注册为自定义函数;SpEL使用StandardEvaluationContext的registerFunction方法进行注册自定义函数,其实完全可以使用setVariable代替,两者其实本质是一样的;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="annotation" style="color:rgb(100,100,100); padding:0px; margin:0px; background-color:inherit">@Test</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">public</span><span style="padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">void</span><span style="padding:0px; margin:0px; background-color:inherit"> testFunctionExpression() </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">throws</span><span style="padding:0px; margin:0px; background-color:inherit"> SecurityException, NoSuchMethodException { </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> ExpressionParser parser = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> SpelExpressionParser(); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> StandardEvaluationContext context = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> StandardEvaluationContext(); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Method parseInt = Integer.<span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">.getDeclaredMethod(</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"parseInt"</span><span style="padding:0px; margin:0px; background-color:inherit">, String.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> context.registerFunction(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"parseInt"</span><span style="padding:0px; margin:0px; background-color:inherit">, parseInt); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> context.setVariable(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"parseInt2"</span><span style="padding:0px; margin:0px; background-color:inherit">, parseInt); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> String expression1 = <span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#parseInt('3') == #parseInt2('3')"</span><span style="padding:0px; margin:0px; background-color:inherit">; </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">boolean</span><span style="padding:0px; margin:0px; background-color:inherit"> result1 = parser.parseExpression(expression1).getValue(context, </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">boolean</span><span style="padding:0px; margin:0px; background-color:inherit">.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Assert.assertEquals(<span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">true</span><span style="padding:0px; margin:0px; background-color:inherit">, result1); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">} </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> </span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
此处可以看出“registerFunction”和“setVariable”都可以注册自定义函数,但是两个方法的含义不一样,推荐使用“registerFunction”方法注册自定义函数。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px">六、赋值表达式:</span>SpEL即允许给自定义变量赋值,也允许给跟对象赋值,直接使用“#variableName=value”即可赋值:</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="annotation" style="color:rgb(100,100,100); padding:0px; margin:0px; background-color:inherit">@Test</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">public</span><span style="padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">void</span><span style="padding:0px; margin:0px; background-color:inherit"> testAssignExpression() { </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> ExpressionParser parser = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> SpelExpressionParser(); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> <span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//1.给root对象赋值</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> EvaluationContext context = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> StandardEvaluationContext(</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"aaaa"</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> String result1 = parser.parseExpression(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#root='aaaaa'"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context, String.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Assert.assertEquals(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"aaaaa"</span><span style="padding:0px; margin:0px; background-color:inherit">, result1); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> String result2 = parser.parseExpression(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#this='aaaa'"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context, String.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Assert.assertEquals(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"aaaa"</span><span style="padding:0px; margin:0px; background-color:inherit">, result2); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> <span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//2.给自定义变量赋值</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> context.setVariable(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#variable"</span><span style="padding:0px; margin:0px; background-color:inherit">, </span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"variable"</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> String result3 = parser.parseExpression(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#variable=#root"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context, String.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Assert.assertEquals(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"aaaa"</span><span style="padding:0px; margin:0px; background-color:inherit">, result3); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">} </span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
使用“#root='aaaaa'”给根对象赋值,使用“"#this='aaaa'”给当前上下文对象赋值,使用“#variable=#root”给自定义变量赋值,很简单。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px">七、对象属性存取及安全导航表达式:</span>对象属性获取非常简单,即使用如“a.property.property”这种点缀式获取,SpEL对于属性名首字母是不区分大小写的;SpEL还引入了Groovy语言中的安全导航运算符“(对象|属性)?.属性”,用来避免但“?.”前边的表达式为null时抛出空指针异常,而是返回null;修改对象属性值则可以通过赋值表达式或Expression接口的setValue方法修改。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span style="padding:0px; margin:0px; background-color:inherit">ExpressionParser parser = </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> SpelExpressionParser(); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//1.访问root对象属性</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Date date = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> Date(); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">StandardEvaluationContext context = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> StandardEvaluationContext(date); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit"> result1 = parser.parseExpression(</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"Year"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context, </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(date.getYear(), result1); </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit"> result2 = parser.parseExpression(</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"year"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context, </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(date.getYear(), result2); </span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
对于<span style="line-height:1.5em; padding:0px; margin:0px">当前上下文对象属性及方法访问,可以直接使用属性或方法名访问</span>,比如此处根对象date属性“year”,注意此处属性名首字母不区分大小写。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//2.安全访问</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">context.setRootObject(<span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">null</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Object result3 = parser.parseExpression(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#root?.year"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context, Object.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(<span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">null</span><span style="padding:0px; margin:0px; background-color:inherit">, result3); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> </span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
SpEL引入了Groovy的安全导航运算符,比如此处根对象为null,所以如果访问其属性时肯定抛出空指针异常,而采用“?.”安全访问导航运算符将不抛空指针异常,而是简单的返回null。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//3.给root对象属性赋值</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">context.setRootObject(date); </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit"> result4 = parser.parseExpression(</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"Year = 4"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context, </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">4</span><span style="padding:0px; margin:0px; background-color:inherit">, result4); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">parser.parseExpression(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"Year"</span><span style="padding:0px; margin:0px; background-color:inherit">).setValue(context, </span><span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">5</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit"> result5 = parser.parseExpression(</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"Year"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context, </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">5</span><span style="padding:0px; margin:0px; background-color:inherit">, result5); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> </span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
给对象属性赋值可以采用赋值表达式或Expression接口的setValue方法赋值,而且也可以采用点缀方式赋值。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px">八、对象方法调用:</span>对象方法调用更简单,跟Java语法一样;如“'haha'.substring(2,4)”将返回“ha”;而对于根对象可以直接调用方法;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span style="padding:0px; margin:0px; background-color:inherit">Date date = </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> Date(); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">StandardEvaluationContext context = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> StandardEvaluationContext(date); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit"> result2 = parser.parseExpression(</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"getYear()"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context, </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(date.getYear(), result2); </span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
比如根对象date方法“getYear”可以直接调用。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px">九、Bean引用:</span>SpEL支持使用“@”符号来引用Bean,在引用Bean时需要使用BeanResolver接口实现来查找Bean,Spring提供BeanFactoryResolver实现;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="annotation" style="color:rgb(100,100,100); padding:0px; margin:0px; background-color:inherit">@Test</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">public</span><span style="padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">void</span><span style="padding:0px; margin:0px; background-color:inherit"> testBeanExpression() { </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> ClassPathXmlApplicationContext ctx = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> ClassPathXmlApplicationContext(); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> ctx.refresh(); </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> ExpressionParser parser = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> SpelExpressionParser(); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> StandardEvaluationContext context = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> StandardEvaluationContext(); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> context.setBeanResolver(<span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> BeanFactoryResolver(ctx)); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Properties result1 = parser.parseExpression(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"@systemProperties"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context, Properties.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Assert.assertEquals(System.getProperties(), result1); </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">} </span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
在示例中我们首先初始化了一个IoC容器,ClassPathXmlApplicationContext 实现默认会把“System.getProperties()”注册为“systemProperties”Bean,因此我们使用 “@systemProperties”来引用该Bean。</p>
<h3 style="font-size:1.2em; line-height:1.5em; margin:0px 0px 0.5em; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"><a name="t9"></a>
</h3>
<h3 style="font-size:1.2em; line-height:1.5em; margin:0px 0px 0.5em; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"><a name="t10"></a>
5.3.3 集合相关表达式</h3>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px">一、内联List:</span>从Spring3.0.4开始支持内联List,使用{表达式,……}定义内联List,如“{1,2,3}”将返回一个整型的ArrayList,而“{}”将返回空的List,对于字面量表达式列表,SpEL会使用java.util.Collections.unmodifiableList方法将列表设置为不可修改。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//将返回不可修改的空List</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">List<Integer> result2 = parser.parseExpression(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"{}"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(List.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//对于字面量列表也将返回不可修改的List</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">List<Integer> result1 = parser.parseExpression(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"{1,2,3}"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(List.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(<span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> Integer(</span><span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">1</span><span style="padding:0px; margin:0px; background-color:inherit">), result1.get(</span><span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">0</span><span style="padding:0px; margin:0px; background-color:inherit">)); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">try</span><span style="padding:0px; margin:0px; background-color:inherit"> { </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> result1.set(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">0</span><span style="padding:0px; margin:0px; background-color:inherit">, </span><span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">2</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> <span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//不可能执行到这,对于字面量列表不可修改</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Assert.fail(); </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">} <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">catch</span><span style="padding:0px; margin:0px; background-color:inherit"> (Exception e) { </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">} </span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//对于列表中只要有一个不是字面量表达式,将只返回原始List,</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//不会进行不可修改处理</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">String expression3 = <span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"{{1+2,2+4},{3,4+4}}"</span><span style="padding:0px; margin:0px; background-color:inherit">; </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">List<List<Integer>> result3 = parser.parseExpression(expression3).getValue(List.<span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">result3.get(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">0</span><span style="padding:0px; margin:0px; background-color:inherit">).set(</span><span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">0</span><span style="padding:0px; margin:0px; background-color:inherit">, </span><span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">1</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">2</span><span style="padding:0px; margin:0px; background-color:inherit">, result3.size()); </span></span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//声明一个大小为2的一维数组并初始化</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">[] result2 = parser.parseExpression(</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"new int[2]{1,2}"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">[].</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span style="padding:0px; margin:0px; background-color:inherit">//定义一维数组但不初始化</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">[] result1 = parser.parseExpression(</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"new int[1]"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">[].</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
二、内联数组:和Java 数组定义类似,只是在定义时进行多维数组初始化。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//定义多维数组但不初始化</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span style="padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">[][][] result3 = parser.parseExpression("new
int[1][2][3]").getValue(</span><span style="padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">[][][].</span><span style="padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//错误的定义多维数组,多维数组不能初始化</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">String expression4 = <span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"new int[1][2][3]{{1}{2}{3}}"</span><span style="padding:0px; margin:0px; background-color:inherit">; </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">try</span><span style="padding:0px; margin:0px; background-color:inherit"> { </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">[][][] result4 = parser.parseExpression(expression4).getValue(</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">[][][].</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Assert.fail(); </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">} <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">catch</span><span style="padding:0px; margin:0px; background-color:inherit"> (Exception e) { </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">} </span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
三、集合,字典元素访问:SpEL目前支持所有集合类型和字典类型的元素访问,使用“集合[索引]”访问集合元素,使用“map[key]”访问字典元素;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//SpEL内联List访问</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit"> result1 = parser.parseExpression(</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"{1,2,3}[0]"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//即list.get(0)</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">1</span><span style="padding:0px; margin:0px; background-color:inherit">, result1); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> </span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//SpEL目前支持所有集合类型的访问</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Collection<Integer> collection = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> HashSet<Integer>(); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">collection.add(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">1</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">collection.add(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">2</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">EvaluationContext context2 = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> StandardEvaluationContext(); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">context2.setVariable(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"collection"</span><span style="padding:0px; margin:0px; background-color:inherit">, collection); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit"> result2 = parser.parseExpression(</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#collection[1]"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context2, </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//对于任何集合类型通过Iterator来定位元素</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">2</span><span style="padding:0px; margin:0px; background-color:inherit">, result2); </span></span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//SpEL对Map字典元素访问的支持</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Map<String, Integer> map = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> HashMap<String, Integer>(); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">map.put(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"a"</span><span style="padding:0px; margin:0px; background-color:inherit">, </span><span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">1</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">EvaluationContext context3 = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> StandardEvaluationContext(); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">context3.setVariable(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"map"</span><span style="padding:0px; margin:0px; background-color:inherit">, map); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit"> result3 = parser.parseExpression(</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#map['a']"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context3, </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">1</span><span style="padding:0px; margin:0px; background-color:inherit">, result3); </span></span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
注:<span style="line-height:1.5em; padding:0px; margin:0px">集合元素访问是通过Iterator遍历来定位元素位置的</span>。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px">四、列表,字典,数组元素修改:</span>可以使用赋值表达式或Expression接口的setValue方法修改;</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//1.修改数组元素值</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">[] array = </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">[] {</span><span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">1</span><span style="padding:0px; margin:0px; background-color:inherit">, </span><span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">2</span><span style="padding:0px; margin:0px; background-color:inherit">}; </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">EvaluationContext context1 = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> StandardEvaluationContext(); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">context1.setVariable(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"array"</span><span style="padding:0px; margin:0px; background-color:inherit">, array); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit"> result1 = parser.parseExpression(</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#array[1] = 3"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context1, </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">3</span><span style="padding:0px; margin:0px; background-color:inherit">, result1); </span></span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//2.修改集合值</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Collection<Integer> collection = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> ArrayList<Integer>(); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">collection.add(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">1</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">collection.add(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">2</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">EvaluationContext context2 = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> StandardEvaluationContext(); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">context2.setVariable(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"collection"</span><span style="padding:0px; margin:0px; background-color:inherit">, collection); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit"> result2 = parser.parseExpression(</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#collection[1] = 3"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context2, </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">3</span><span style="padding:0px; margin:0px; background-color:inherit">, result2); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">parser.parseExpression(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#collection[1]"</span><span style="padding:0px; margin:0px; background-color:inherit">).setValue(context2, </span><span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">4</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">result2 = parser.parseExpression(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#collection[1]"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context2, </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">4</span><span style="padding:0px; margin:0px; background-color:inherit">, result2); </span></span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//3.修改map元素值</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Map<String, Integer> map = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> HashMap<String, Integer>(); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">map.put(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"a"</span><span style="padding:0px; margin:0px; background-color:inherit">, </span><span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">1</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">EvaluationContext context3 = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> StandardEvaluationContext(); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">context3.setVariable(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"map"</span><span style="padding:0px; margin:0px; background-color:inherit">, map); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit"> result3 = parser.parseExpression(</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#map['a'] = 2"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context3, </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">int</span><span style="padding:0px; margin:0px; background-color:inherit">.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">2</span><span style="padding:0px; margin:0px; background-color:inherit">, result3); </span></span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
对数组修改直接对“#array[index]”赋值即可修改元素值,同理适用于集合和字典类型。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px">五、集合投影:</span>在SQL中投影指从表中选择出列,而在SpEL指根据集合中的元素中通过选择来构造另一个集合,该集合和原集合具有相同数量的元素;SpEL使用“(list|map).![投影表达式]”来进行投影运算:</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//1.首先准备测试数据</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Collection<Integer> collection = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> ArrayList<Integer>(); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">collection.add(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">4</span><span style="padding:0px; margin:0px; background-color:inherit">); collection.add(</span><span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">5</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Map<String, Integer> map = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> HashMap<String, Integer>(); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">map.put(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"a"</span><span style="padding:0px; margin:0px; background-color:inherit">, </span><span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">1</span><span style="padding:0px; margin:0px; background-color:inherit">); map.put(</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"b"</span><span style="padding:0px; margin:0px; background-color:inherit">, </span><span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">2</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//2.测试集合或数组</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">EvaluationContext context1 = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> StandardEvaluationContext(); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">context1.setVariable(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"collection"</span><span style="padding:0px; margin:0px; background-color:inherit">, collection); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Collection<Integer> result1 = </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">parser.parseExpression(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#collection.![#this+1]"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context1, Collection.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">2</span><span style="padding:0px; margin:0px; background-color:inherit">, result1.size()); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(<span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> Integer(</span><span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">5</span><span style="padding:0px; margin:0px; background-color:inherit">), result1.iterator().next()); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> </span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
对于集合或数组使用如上表达式进行投影运算,其中投影表达式中“#this”代表每个集合或数组元素,可以使用比如“#this.property”来获取集合元素的属性,其中“#this”可以省略。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//3.测试字典</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">EvaluationContext context2 = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> StandardEvaluationContext(); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">context2.setVariable(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"map"</span><span style="padding:0px; margin:0px; background-color:inherit">, map); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">List<Integer> result2 = </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">parser.parseExpression(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#map.![ value+1]"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context2, List.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">2</span><span style="padding:0px; margin:0px; background-color:inherit">, result2.size()); </span></span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
SpEL投影运算还支持Map投影,但Map投影最终只能得到List结果,如上所示,对于投影表达式中的“#this”将是Map.Entry,所以可以使用“value”来获取值,使用“key”来获取键。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
<span style="line-height:1.5em; padding:0px; margin:0px">六、集合选择:</span>在SQL中指使用select进行选择行数据,而在SpEL指根据原集合通过条件表达式选择出满足条件的元素并构造为新的集合,SpEL使用“(list|map).?[选择表达式]”,其中选择表达式结果必须是boolean类型,如果true则选择的元素将添加到新集合中,false将不添加到新集合中。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//1.首先准备测试数据</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Collection<Integer> collection = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> ArrayList<Integer>(); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">collection.add(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">4</span><span style="padding:0px; margin:0px; background-color:inherit">); collection.add(</span><span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">5</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Map<String, Integer> map = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> HashMap<String, Integer>(); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">map.put(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"a"</span><span style="padding:0px; margin:0px; background-color:inherit">, </span><span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">1</span><span style="padding:0px; margin:0px; background-color:inherit">); map.put(</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"b"</span><span style="padding:0px; margin:0px; background-color:inherit">, </span><span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">2</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//2.集合或数组测试</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">EvaluationContext context1 = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> StandardEvaluationContext(); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">context1.setVariable(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"collection"</span><span style="padding:0px; margin:0px; background-color:inherit">, collection); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Collection<Integer> result1 = </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">parser.parseExpression(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#collection.?[#this>4]"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context1, Collection.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">1</span><span style="padding:0px; margin:0px; background-color:inherit">, result1.size()); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(<span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> Integer(</span><span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">5</span><span style="padding:0px; margin:0px; background-color:inherit">), result1.iterator().next()); </span></span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
对于集合或数组选择,如“#collection.?[#this>4]”将选择出集合元素值大于4的所有元素。选择表达式必须返回布尔类型,使用“#this”表示当前元素。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:25.2000007629395px; margin:0px auto; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2463.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//3.字典测试</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">EvaluationContext context2 = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> StandardEvaluationContext(); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">context2.setVariable(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"map"</span><span style="padding:0px; margin:0px; background-color:inherit">, map); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Map<String, Integer> result2 = </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">parser.parseExpression(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#map.?[#this.key != 'a']"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context2, Map.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(<span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">1</span><span style="padding:0px; margin:0px; background-color:inherit">, result2.size()); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">List<Integer> result3 = </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> parser.parseExpression(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#map.?[key != 'a'].![value+1]"</span><span style="padding:0px; margin:0px; background-color:inherit">).getValue(context2, List.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">Assert.assertEquals(<span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> Integer(</span><span class="number" style="color:rgb(192,0,0); padding:0px; margin:0px; background-color:inherit">3</span><span style="padding:0px; margin:0px; background-color:inherit">), result3.iterator().next()); </span></span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
对于字典选择,如“#map.?[#this.key != 'a']”将选择键值不等于”a”的,其中选择表达式中“#this”是Map.Entry类型,而最终结果还是Map,这点和投影不同;集合选择和投影可以一起使用,如“#map.?[key != 'a'].![value+1]”将首先选择键值不等于”a”的,然后在选出的Map中再进行“value+1”的投影。</p>
<h3 style="font-size:1.2em; line-height:1.5em; margin:0px 0px 0.5em; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"><a name="t11"></a>
</h3>
<h3 style="font-size:1.2em; line-height:1.5em; margin:0px 0px 0.5em; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif"><a name="t12"></a>
5.3.4 表达式模板</h3>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
模板表达式就是由字面量与一个或多个表达式块组成。每个表达式块由“前缀+表达式+后缀”形式组成,如“${1+2}”即表达式块。在前边我们已经介绍了使用ParserContext接口实现来定义表达式是否是模板及前缀和后缀定义。在此就不多介绍了,如“Error ${#v0} ${#v1}”表达式表示由字面量“Error ”、模板表达式“#v0”、模板表达式“#v1”组成,其中v0和v1表示自定义变量,需要在上下文定义。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<h3 style="font-size:1.2em; line-height:1.5em; margin:0px 0px 0.5em; padding:0px; color:black"><a name="t13"></a>
5.4.1 xml风格的配置</h3>
<h2 style=""><span style="font-family:verdana,arial,helvetica,sans-serif; font-size:14px; font-weight:normal; line-height:21px; background-color:rgb(250,250,250)"></span>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
SpEL支持在Bean定义时注入,默认使用“#{SpEL表达式}”表示,其中“#root”根对象默认可以认为是ApplicationContext,只有ApplicationContext实现默认支持SpEL,获取根对象属性其实是获取容器中的Bean。</p>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
首先看下配置方式(chapter5/el1.xml)吧:</p>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="margin:0px auto; font-size:12px; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; font-size:12px; width:671px; overflow:auto; margin:18px 0px!important; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; font-size:12px; border-width:0px">
<div class="tools" style="padding:3px; text-align:left; margin:0px; color:black; font-weight:bold">
<a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="font-size:12px; line-height:1.4em; margin:0px 0px 1px 45px!important; padding:0px; border:1px solid rgb(209,215,220); list-style:decimal; color:rgb(92,92,92); background-color:rgb(255,255,255)">
<li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"><bean id=</span><span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"world"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">class</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">=</span><span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"java.lang.String"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">> </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> <constructor-arg value=<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"#{' World!'}"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">/> </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"></bean> </span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"><bean id=<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"hello1"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">class</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">=</span><span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"java.lang.String"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">> </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> <constructor-arg value=<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"#{'Hello'}#{world}"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">/> </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"></bean> </span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"><bean id=<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"hello2"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">class</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">=</span><span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"java.lang.String"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">> </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> <constructor-arg value=<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"#{'Hello' + world}"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">/> </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> <!-- 不支持嵌套的 --> </span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> <!--<constructor-arg value=<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"#{'Hello'#{world}}"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">/>--> </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"></bean> </span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"><bean id=<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"hello3"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">class</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">=</span><span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"java.lang.String"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">> </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> <constructor-arg value=<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"#{'Hello' + @world}"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">/> </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"></bean> </span></li></ol>
</div>
</div>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
模板默认以前缀“#{”开头,以后缀“}”结尾,且不允许嵌套,如“#{'Hello'#{world}}”错误,如“#{'Hello' + world}”中“world”默认解析为Bean。当然可以使用“@bean”引用了。</p>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
接下来测试一下吧:</p>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="margin:0px auto; font-size:12px; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; font-size:12px; width:671px; overflow:auto; margin:18px 0px!important; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; font-size:12px; border-width:0px">
<div class="tools" style="padding:3px; text-align:left; margin:0px; color:black; font-weight:bold">
<a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="font-size:12px; line-height:1.4em; margin:0px 0px 1px 45px!important; padding:0px; border:1px solid rgb(209,215,220); list-style:decimal; color:rgb(92,92,92); background-color:rgb(255,255,255)">
<li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"><span class="annotation" style="color:rgb(100,100,100); font-size:12px; padding:0px; margin:0px; background-color:inherit">@Test</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">public</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">void</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> testXmlExpression() { </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> ApplicationContext ctx = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">new</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> ClassPathXmlApplicationContext(</span><span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"chapter5/el1.xml"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> String hello1 = ctx.getBean(<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"hello1"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">, String.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">class</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> String hello2 = ctx.getBean(<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"hello2"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">, String.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">class</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> String hello3 = ctx.getBean(<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"hello3"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">, String.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">class</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> Assert.assertEquals(<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"Hello World!"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">, hello1); </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> Assert.assertEquals(<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"Hello World!"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">, hello2); </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> Assert.assertEquals(<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"Hello World!"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">, hello3); </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">} </span></li></ol>
</div>
</div>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
是不是很简单,除了XML配置方式,Spring还提供一种注解方式@Value,接着往下看吧。</p>
</h2>
<h3 style="font-size:1.2em; line-height:1.5em; margin:0px 0px 0.5em; padding:0px; color:black"><a name="t15"></a>
</h3>
<h3 style="font-size:1.2em; line-height:1.5em; margin:0px 0px 0.5em; padding:0px; color:black"><a name="t16"></a>
5.4.2 注解风格的配置</h3>
<h2 style=""><span style="font-family:verdana,arial,helvetica,sans-serif; font-size:14px; font-weight:normal; line-height:21px; background-color:rgb(250,250,250)"></span>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
基于注解风格的SpEL配置也非常简单,使用@Value注解来指定SpEL表达式,该注解可以放到字段、方法及方法参数上。</p>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
测试Bean类如下,使用@Value来指定SpEL表达式:</p>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="margin:0px auto; font-size:12px; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; font-size:12px; width:671px; overflow:auto; margin:18px 0px!important; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; font-size:12px; border-width:0px">
<div class="tools" style="padding:3px; text-align:left; margin:0px; color:black; font-weight:bold">
<a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="font-size:12px; line-height:1.4em; margin:0px 0px 1px 45px!important; padding:0px; border:1px solid rgb(209,215,220); list-style:decimal; color:rgb(92,92,92); background-color:rgb(255,255,255)">
<li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">package</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> cn.javass.spring.chapter5; </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">import</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> org.springframework.beans.factory.annotation.Value; </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">public</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">class</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> SpELBean { </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> <span class="annotation" style="color:rgb(100,100,100); font-size:12px; padding:0px; margin:0px; background-color:inherit">@Value</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">(</span><span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"#{'Hello' + world}"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">) </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">private</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> String value; </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> <span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; font-size:12px; background-color:inherit">//setter和getter由于篇幅省略,自己写上</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">} </span></li></ol>
</div>
</div>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
首先看下配置文件(chapter5/el2.xml):</p>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="margin:0px auto; font-size:12px; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; font-size:12px; width:671px; overflow:auto; margin:18px 0px!important; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; font-size:12px; border-width:0px">
<div class="tools" style="padding:3px; text-align:left; margin:0px; color:black; font-weight:bold">
<a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="font-size:12px; line-height:1.4em; margin:0px 0px 1px 45px!important; padding:0px; border:1px solid rgb(209,215,220); list-style:decimal; color:rgb(92,92,92); background-color:rgb(255,255,255)">
<li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"><?xml version=</span><span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"1.0"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> encoding=</span><span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"UTF-8"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">?> </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"><beans xmlns=<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"http://www.springframework.org/schema/beans"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> xmlns:xsi=<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"http://www.w3.org/2001/XMLSchema-instance"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> xmlns:context=<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"http://www.springframework.org/schema/context"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> xsi:schemaLocation=" </span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> http:<span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; font-size:12px; background-color:inherit">//www.springframework.org/schema/beans</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> http:<span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; font-size:12px; background-color:inherit">//www.springframework.org/schema/beans/spring-beans-3.0.xsd</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> http:<span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; font-size:12px; background-color:inherit">//www.springframework.org/schema/context</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">http:<span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; font-size:12px; background-color:inherit">//www.springframework.org/schema/context/spring-context-3.0.xsd"></span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> <context:annotation-config/> </span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> <bean id=<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"world"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">class</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">=</span><span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"java.lang.String"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">> </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> <constructor-arg value=<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"#{' World!'}"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">/> </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </bean> </span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> <bean id=<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"helloBean1"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">class</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">=</span><span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"cn.javass.spring.chapter5.SpELBean"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">/> </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> <bean id=<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"helloBean2"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">class</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">=</span><span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"cn.javass.spring.chapter5.SpELBean"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">> </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> <property name=<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"value"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> value=</span><span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"haha"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">/> </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </bean> </span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"></beans> </span></li></ol>
</div>
</div>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
配置时必须使用“<span style="font-size:14px; line-height:1.5em; padding:0px; margin:0px"><strong><context:annotation-config/></strong></span>”来开启对注解的支持。</p>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
有了配置文件那开始测试吧:</p>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<div style="margin:0px auto; font-size:12px; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; font-size:12px; width:671px; overflow:auto; margin:18px 0px!important; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; font-size:12px; border-width:0px">
<div class="tools" style="padding:3px; text-align:left; margin:0px; color:black; font-weight:bold">
<a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="font-size:12px; line-height:1.4em; margin:0px 0px 1px 45px!important; padding:0px; border:1px solid rgb(209,215,220); list-style:decimal; color:rgb(92,92,92); background-color:rgb(255,255,255)">
<li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"><span class="annotation" style="color:rgb(100,100,100); font-size:12px; padding:0px; margin:0px; background-color:inherit">@Test</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">public</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">void</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> testAnnotationExpression() { </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> ApplicationContext ctx = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">new</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> ClassPathXmlApplicationContext(</span><span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"chapter5/el2.xml"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> SpELBean helloBean1 = ctx.getBean(<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"helloBean1"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">, SpELBean.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">class</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> Assert.assertEquals(<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"Hello World!"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">, helloBean1.getValue()); </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> SpELBean helloBean2 = ctx.getBean(<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"helloBean2"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">, SpELBean.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; font-size:12px; padding:0px; margin:0px; background-color:inherit">class</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; background-color:rgb(255,255,255)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit"> Assert.assertEquals(<span class="string" style="color:blue; font-size:12px; padding:0px; margin:0px; background-color:inherit">"haha"</span><span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">, helloBean2.getValue()); </span></span></li><li style="font-size:12px; margin:0px!important; padding:0px 3px 0px 10px!important; border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; color:rgb(92,92,92); background-color:rgb(248,248,248)">
<span style="color:black; font-size:12px; padding:0px; margin:0px; background-color:inherit">} </span></li></ol>
</div>
</div>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
其中“helloBean1 ”值是SpEL表达式的值,而“helloBean2”是通过setter注入的值,这说明setter注入将覆盖@Value的值。</p>
<p style="margin:0px; padding:0px; font-size:14px; font-family:verdana,arial,helvetica,sans-serif; line-height:1.5em">
</p>
</h2>
<h3 style="font-size:1.2em; line-height:1.5em; margin:0px 0px 0.5em; padding:0px; color:black"><a name="t18"></a>
5.4.3 在Bean定义中SpEL的问题</h3>
<h2 style="line-height:1.5em; margin:0px 0px 0.5em; padding:0px; font-family:Helvetica,Tahoma,Arial,sans-serif">
<span style="font-family:verdana,arial,helvetica,sans-serif; font-size:14px; font-weight:normal; line-height:21px; background-color:rgb(250,250,250)"></span>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.5em">
如果有同学问“#{我不是SpEL表达式}”不是SpEL表达式,而是公司内部的模板,想换个前缀和后缀该如何实现呢?</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.5em">
那我们来看下Spring如何在IoC容器内使用BeanExpressionResolver接口实现来求值SpEL表达式,那如果我们通过某种方式获取该接口实现,然后把前缀后缀修改了不就可以了。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.5em">
此处我们使用BeanFactoryPostProcessor接口提供postProcessBeanFactory回调方法,它是在IoC容器创建好但还未进行任何Bean初始化时被ApplicationContext实现调用,因此在这个阶段把SpEL前缀及后缀修改掉是安全的,具体代码如下:</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.5em">
</p>
<div style="margin:0px auto; font-size:12px; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">package</span><span style="padding:0px; margin:0px; background-color:inherit"> cn.javass.spring.chapter5; </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">import</span><span style="padding:0px; margin:0px; background-color:inherit"> org.springframework.beans.BeansException; </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">import</span><span style="padding:0px; margin:0px; background-color:inherit"> org.springframework.beans.factory.config.BeanFactoryPostProcessor; </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">import</span><span style="padding:0px; margin:0px; background-color:inherit"> org.springframework.beans.factory.config.ConfigurableListableBeanFactory; </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">import</span><span style="padding:0px; margin:0px; background-color:inherit"> org.springframework.context.expression.StandardBeanExpressionResolver; </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">public</span><span style="padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit"> SpELBeanFactoryPostProcessor </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">implements</span><span style="padding:0px; margin:0px; background-color:inherit"> BeanFactoryPostProcessor { </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> <span class="annotation" style="color:rgb(100,100,100); padding:0px; margin:0px; background-color:inherit">@Override</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">public</span><span style="padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">void</span><span style="padding:0px; margin:0px; background-color:inherit"> postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">throws</span><span style="padding:0px; margin:0px; background-color:inherit"> BeansException { </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> StandardBeanExpressionResolver resolver = (StandardBeanExpressionResolver) beanFactory.getBeanExpressionResolver(); </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> resolver.setExpressionPrefix(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"%{"</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> resolver.setExpressionSuffix(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"}"</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> } </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">} </span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.5em">
首先通过 ConfigurableListableBeanFactory的getBeanExpressionResolver方法获取BeanExpressionResolver实现,其次强制类型转换为StandardBeanExpressionResolver,其为Spring默认实现,然后改掉前缀及后缀。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.5em">
开始测试吧,首先准备配置文件(chapter5/el3.xml):</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.5em">
</p>
<div style="margin:0px auto; font-size:12px; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span style="padding:0px; margin:0px; background-color:inherit"><?xml version=</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"1.0"</span><span style="padding:0px; margin:0px; background-color:inherit"> encoding=</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"UTF-8"</span><span style="padding:0px; margin:0px; background-color:inherit">?> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><beans xmlns=<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"http://www.springframework.org/schema/beans"</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> xmlns:xsi=<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"http://www.w3.org/2001/XMLSchema-instance"</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> xmlns:context=<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"http://www.springframework.org/schema/context"</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> xsi:schemaLocation=" </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> http:<span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//www.springframework.org/schema/beans</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> http:<span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//www.springframework.org/schema/beans/spring-beans-3.0.xsd</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> http:<span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//www.springframework.org/schema/context</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> http:<span class="comment" style="color:rgb(0,130,0); padding:0px; margin:0px; width:auto; border:0px; background-color:inherit">//www.springframework.org/schema/context/spring-context-3.0.xsd"></span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> <context:annotation-config/> </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> <bean <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">=</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"cn.javass.spring.chapter5.SpELBeanFactoryPostProcessor"</span><span style="padding:0px; margin:0px; background-color:inherit">/> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> <bean id=<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"world"</span><span style="padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">=</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"java.lang.String"</span><span style="padding:0px; margin:0px; background-color:inherit">> </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> <constructor-arg value=<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"%{' World!'}"</span><span style="padding:0px; margin:0px; background-color:inherit">/> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> </bean> </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> <bean id=<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"helloBean1"</span><span style="padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">=</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"cn.javass.spring.chapter5.SpELBean"</span><span style="padding:0px; margin:0px; background-color:inherit">/> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> <bean id=<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"helloBean2"</span><span style="padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">=</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"cn.javass.spring.chapter5.SpELBean"</span><span style="padding:0px; margin:0px; background-color:inherit">> </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> <property name=<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"value"</span><span style="padding:0px; margin:0px; background-color:inherit"> value=</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"%{'Hello' + world}"</span><span style="padding:0px; margin:0px; background-color:inherit">/> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> </bean> </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"></beans> </span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.5em">
配置文件和注解风格的几乎一样,只有SpEL表达式前缀变为“%{”了,并且注册了“cn.javass.spring.chapter5.SpELBeanFactoryPostProcessor”Bean,用于修改前缀和后缀的。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.5em">
写测试代码测试一下吧:</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.5em">
</p>
<div style="margin:0px auto; font-size:12px; padding:0px; border:1px solid rgb(170,187,204)">
<span class="bold" style="font-size:14px; font-weight:bold; font-family:'arial black','avant garde'; padding:0px; margin:0px">java代码:</span>
<div class="dp-highlighter" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:671px; overflow:auto; padding:1px 0px 0px; word-break:break-all; word-wrap:break-word; border-width:0px; margin:18px 0px!important; background-color:rgb(231,229,220)">
<div class="bar" style="padding:0px 0px 0px 45px; margin:0px auto; border-width:0px">
<div class="tools" style="padding:3px; margin:0px; font-weight:bold"><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">查看</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">复制到剪贴板</a><a target="_blank" href="http://sishuok.com/forum/blogPost/list/2464.html" style="color:rgb(160,160,160); text-decoration:none; margin:0px 10px 0px 0px; font-size:9px; padding:0px; background-color:inherit">打印</a></div>
</div>
<ol class="dp-j" style="line-height:1.4em; padding:0px; border:1px solid rgb(209,215,220); list-style-position:initial; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)">
<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="annotation" style="color:rgb(100,100,100); padding:0px; margin:0px; background-color:inherit">@Test</span><span style="padding:0px; margin:0px; background-color:inherit"> </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">public</span><span style="padding:0px; margin:0px; background-color:inherit"> </span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">void</span><span style="padding:0px; margin:0px; background-color:inherit"> testPrefixExpression() { </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> ApplicationContext ctx = <span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">new</span><span style="padding:0px; margin:0px; background-color:inherit"> ClassPathXmlApplicationContext(</span><span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"chapter5/el3.xml"</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> SpELBean helloBean1 = ctx.getBean(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"helloBean1"</span><span style="padding:0px; margin:0px; background-color:inherit">, SpELBean.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Assert.assertEquals(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"#{'Hello' + world}"</span><span style="padding:0px; margin:0px; background-color:inherit">, helloBean1.getValue()); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> SpELBean helloBean2 = ctx.getBean(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"helloBean2"</span><span style="padding:0px; margin:0px; background-color:inherit">, SpELBean.</span><span class="keyword" style="color:rgb(0,102,153); font-weight:bold; padding:0px; margin:0px; background-color:inherit">class</span><span style="padding:0px; margin:0px; background-color:inherit">); </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important">
<span style="color:black; padding:0px; margin:0px; background-color:inherit"> Assert.assertEquals(<span class="string" style="color:blue; padding:0px; margin:0px; background-color:inherit">"Hello World!"</span><span style="padding:0px; margin:0px; background-color:inherit">, helloBean2.getValue()); </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); line-height:14px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">
<span style="color:black; padding:0px; margin:0px; background-color:inherit">} </span></li></ol>
</div>
</div>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.5em">
此处helloBean1 中通过@Value注入的“#{'Hello' + world}”结果还是“#{'Hello' + world}”说明不对其进行SpEL表达式求值了,而helloBean2使用“%{'Hello' + world}”注入,得到正确的“"Hello World!”。</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.5em">
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.5em">
<br>
</p>
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.5em">
本文借鉴:http://www.iteye.com/blogs/subjects/spring3?page=2</p>
</h2>
<br>
</div>
</div>