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

ska*_*man 53 java spring spring-el

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 class Bean {

    String value;

    public void setValue(String value) {
        this.value = value;
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,测试用例:

package test;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class PlaceholderTest {

    private @Resource Bean testBean;

    @Test
    public void valueCheck() {
        assertThat(testBean.value, is("Value A"));
    }
}
Run Code Online (Sandbox Code Playgroud)

挑战 - 在beans文件中提出一个SpEL表达式,允许我在${x.y.z}无法解析的情况下指定默认值,并且必须将此默认值指定为表达式的一部分,而不是在另一个属性集中外部化.

axt*_*avt 27

要从SpEL表达式访问属性占位符,可以使用以下语法:#{'${x.y.z}'}.Hovewer,它无法用elvis运算符和默认值解决你的问题,因为它会${x.y.z}在无法解决时引发异常.

但是您不需要SpEL来声明属性的默认值:

<context:property-placeholder location="..." properties-ref="defaultValues"/>

<bean id = "defaultValues" class = "org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="properties">
        <props>
            <prop key="x.y.z">ZZZ</prop>
        </props>
    </property>
</bean>

<bean ...>
    <property name = "..." value = "${x.y.z}" />
</bean>
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的建议,但我需要能够使用已存在于上下文中的*existing*属性占位符配置器,我无​​法真正摆弄它.此外,我需要能够在线指定默认值,而不是外部化它们. (2认同)

Boz*_*zho 11

看来你错过了冒号:

#{ ${x.y.z} ?: 'defaultValue' }
Run Code Online (Sandbox Code Playgroud)

  • 呃,你是对的...编辑了我的问题..它没有解决它,但至少现在我得到了一个更有用的例外...... (2认同)

sma*_*wjw 9

${myProps.item:defaultValue}意味着当myProps.item不存在时,使用defaultValue.这是属性占位符的默认行为.

#{defaultValue} 表示字面值的SpEL.

因此,${myProps.item:#{defaultValue}}表示何时myProps.item不存在,然后计算SpEL的值,并将其分配给目标字段.

例:

${redis.auth:#{null}}表示当redis.auth属性不存在时,将其设置为null.


btp*_*ka3 8

如果你只是想设置占位符默认值,看到这个:

   <property name="value" value="${x.y.z:defaultValue}"/> 
Run Code Online (Sandbox Code Playgroud)

如果要测试SpEL和占位符之间的交互,请使用以下命令:

   <!-- set value "77-AA-BB-CC-88" when property "x.y.z" not exist -->
   <property name="value" value="77-#{'AA-${x.y.z:BB}-CC'}-88"/>
Run Code Online (Sandbox Code Playgroud)