Spring 3引入了一种新的表达式语言(SpEL),可以在bean定义中使用.语法本身已经很好地指定了.
不清楚的是,SpEL如何与先前版本中已存在的属性占位符语法进行交互.SpEL是否支持属性占位符,或者我是否必须结合两种机制的语法并希望它们结合起来?
让我举一个具体的例子.我想使用属性语法${x.y.z},但添加了由elvis运算符提供的"默认值"语法来处理${x.y.z}未定义的情况.
我尝试了以下语法但没有成功:
#{x.y.z?:'defaultValue'}#{${x.y.z}?:'defaultValue'}第一个给了我
在'org.springframework.beans.factory.config.BeanExpressionContext'类型的对象上找不到字段或属性'x'
这表明SpEL不承认这是一个属性占位符.
第二个语法抛出异常说占位符不被识别,所以占位符解析器是被调用,但未能如预期,因为没有定义属性.
文档没有提到这种交互,所以这样的事情是不可能的,或者它没有记录.
有人设法做到了吗?
好的,我已经为此设计了一个小型,独立的测试用例.这一切都按原样运作:
首先,bean定义:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
">
<context:property-placeholder properties-ref="myProps"/>
<util:properties id="myProps">
<prop key="x.y.z">Value A</prop>
</util:properties>
<bean id="testBean" class="test.Bean">
<!-- here is where the magic is required -->
<property name="value" value="${x.y.z}"/>
<!-- I want something like this
<property name="value" value="${a.b.c}?:'Value B'"/>
-->
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
然后,琐碎的bean类:
包装测试;
public …Run Code Online (Sandbox Code Playgroud) 虽然我知道tput rmcup从“备用屏幕”(在 中称为“杯子模式” man 5 terminfo)返回并恢复保存的屏幕,但它确实具有重新定位光标的副作用。
因此,如果tput smcup被调用,tput rmcup则恢复屏幕并重新定位光标,但如果您随后再键入一些命令或按 Enter 几次然后再次使用tput rmcup,光标将返回到原始保存的位置。
一个用例是在bash重播终端录音的脚本中[使用scriptreplay]:如果脚本提前结束而没有[相当于]调用tput rmcup,那么我希望能够在我的bash脚本中检测到这一点并tput rmcup自动调用。
简而言之,我希望能够确定当前的屏幕状态是什么;即,它是“备用屏幕”还是“正常屏幕”?
从man test:
! EXPRESSION
EXPRESSION is false
Run Code Online (Sandbox Code Playgroud)
和
-n STRING
the length of STRING is nonzero
STRING equivalent to -n STRING
-z STRING
the length of STRING is zero
Run Code Online (Sandbox Code Playgroud)
因此与(上面明确指出的)[ "$x" ]具有相同的效果,并且隐式地与 具有相同的效果。那么,为什么人们会选择使用或者可以说其他形式更清晰呢?这只是个人喜好还是我错过了一个重要的区别?[ -n "$x" ][ ! "$x" ][ -z "$x" ]-n-z
我见过关于此的其他问题(例如这个问题),但没有什么可以确定它们是否在功能上相同。$x我确实想知道如果我使用-nor是否可以避免引用,-z但如果$x包含空格,则与其他方法一样会失败。
(注意:这个问题与上面的类似问题不同,因为它是关于附加和分离 DOM 树之间的差异。)
\n一段简单的 HTML,包含一个 DIV,其元素之间没有空格:
\n<!DOCTYPE html>\n<html>\n\n <body>\n <div><h1>The Title</h1><p>A paragraph.</p><p>A second paragraph.</p></div>\n </body>\n\n <script type="text/javascript">\n\n const div = document.querySelector("div");\n\n console.log(div.innerText);\n\n const clone = div.cloneNode(true);\n console.log(clone.innerText);\n\n document.body.appendChild(clone);\n console.log(clone.innerText);\n\n </script>\n</html>Run Code Online (Sandbox Code Playgroud)\r\n我innerText向控制台输出了三遍。
第一次是原始 DIV 的时间:
\nThe Title\n\nA paragraph.\n\nA second paragraph.\nRun Code Online (Sandbox Code Playgroud)\n第二个是克隆的 DIV,我希望它是相同的,但它是:
\nThe TitleA paragraph.A second paragraph.\xe2\x80\x8b\nRun Code Online (Sandbox Code Playgroud)\n第三个也是克隆的 DIV,但将其添加到文档后,现在我期望它是:
\nThe Title\n\nA paragraph.\n\nA second paragraph.\nRun Code Online (Sandbox Code Playgroud)\n当它不是文档的一部分时,为什么间距不同?
\n我知道我已经安装了Perl 5.20.1.1.但我能以编程方式确定吗?
$]只提供修订,版本和子版本,即5.020001对我来说,意思5.20.1.
该Config模块(记录在这里)不似乎给什么比这更深.为了我:
perl -MConfig -e 'foreach (sort keys %Config) { print "$_ -> $Config{$_}\n" if /version|revision/io; }'
Run Code Online (Sandbox Code Playgroud)
得到:
PERL_API_REVISION -> 5
PERL_API_SUBVERSION -> 0
PERL_API_VERSION -> 20
PERL_REVISION -> 5
PERL_SUBVERSION -> 1
PERL_VERSION -> 20
Revision -> $Revision
SUBVERSION -> 1
api_revision -> 5
api_subversion -> 0
api_version -> 20
api_versionstring -> 5.20.0
ccversion ->
d_inc_version_list ->
d_libm_lib_version ->
db_version_major -> 0
db_version_minor -> 0
db_version_patch …Run Code Online (Sandbox Code Playgroud)