Tag: springel

Spring 3expression式语言如何与属性占位符交互?

Spring 3引入了一个新的expression式语言 (SpEL),可以在bean的定义中使用。 语法本身是相当明确的。 不清楚的是,SpEL是如何与先前版本中已经存在的属性占位符语法进行交互的。 SpEL是否支持财产占位符,还是我必须结合这两种机制的语法,并希望它们结合? 让我举一个具体的例子。 我想使用属性语法${xyz} ,但增加了elvis运算符提供的“默认值”语法来处理${xyz}未定义的情况。 我已经尝试了下面的语法,但没有成功: #{xyz?:'defaultValue'} #{${xyz}?:'defaultValue'} 第一个给我 在'org.springframework.beans.factory.config.BeanExpressionContext'types的对象上找不到字段或属性'x' 这表明SpEL不会将其识别为属性占位符。 第二个语法抛出一个exception,说占位符不被识别,所以占位符parsing器正在被调用,但是由于该属性没有被定义而失败。 文档没有提到这种交互,所以这样的事情是不可能的,或者是没有logging的。 任何人设法做到这一点? 好的,我想出了一个小型的,自包含的testing案例。 这一切都按原样运行: 首先,这个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="xyz">Value A</prop> </util:properties> <bean id="testBean" class="test.Bean"> <!– here is where the magic is […]

SpringValue语言(SpEL)与@Value:美元与哈希($与#)

对于何时使用${…}与#{…}相比,我有点困惑。 Spring的文档只使用#{…} ,但是有很多使用${…}的例子。 此外,当我开始使用SpEL时,我被告知使用${…}并且工作正常。 对于那些感到困惑的人来说,我将如何使用它会是一个例子 @Component public class ProxyConfiguration { @Value("${proxy.host}") private String host; @Value("${proxy.port}") private String port; : } 和一些财产档案: proxy.host=myproxy.host proxy.port=8000 我的问题是: 有什么不同或是一样的? 是弃用的一个版本,所以我应该使用另一个?