java - How does Spring 3 expression language interact with property placeholders? -


spring 3 has introduced new expression language (spel) can used in bean definitions. syntax specified.

what isn't clear how, if @ all, spel interacts property placeholder syntax present in prior versions. spel have support property placeholders, or have combine syntax of both mechanisms , hope combine?

let me give concrete example. want use property syntax ${x.y.z}, addition of "default value" syntax provided elvis operator handle cases ${x.y.z} undefined.

i've tried following syntaxes without success:

  • #{x.y.z?:'defaultvalue'}
  • #{${x.y.z}?:'defaultvalue'}

the first 1 gives me

field or property 'x' cannot found on object of type 'org.springframework.beans.factory.config.beanexpressioncontext'

which suggests spel doesn't recognise property placeholder.

the second syntax throws exception saying placeholder not recognised, placeholder resolver is being invoked, failing expected, since property not defined.

the docs make no mention of interaction, either such thing not possible, or it's undocumented.

anyone managed this?


ok, i've come small, self-contained test case this. works as-is:

first, bean definitions:

<?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 magic required -->         <property name="value" value="${x.y.z}"/>               <!-- want         <property name="value" value="${a.b.c}?:'value b'"/>              -->      </bean>      </beans> 

then, trivial bean class:

package test;

public class bean {      string value;      public void setvalue(string value) {         this.value = value;     } } 

and lastly, test case:

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"));     } } 

the challenge - come spel expression in beans file allows me specify default value in cases ${x.y.z} cannot resolved, , default must specified part of expression, not externalized in property set.

to access property placeholder spel expression, following syntax can used: #{'${x.y.z}'}. hovewer, can't solve problem elvis operator , default values, because throw exception when ${x.y.z} cannot resolved.

but don't need spel declare default values properties:

<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> 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -