title: How does Spring 3 expression language interact with property placeholders?
body: <p>Spring 3 has introduced a new <a href="http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/expressions.html" rel="noreferrer">expression language</a> (SpEL) which can be used in bean definitions. The syntax itself is fairly well specified.</p>
  
  <p>What isn't clear is how, if at all, SpEL interacts with the property placeholder syntax that was already present in prior versions. Does SpEL have support for property placeholders, or do I have to combine the syntax of both mechanisms and hope they combine?</p>
  
  <p>Let me give a concrete example. I want to use the property syntax <code>${x.y.z}</code>, but with the addition of "default value" syntax as provided by the <a href="http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/expressions.html#d0e12053" rel="noreferrer">elvis operator</a> to handle cases where <code>${x.y.z}</code> is undefined.</p>
  
  <p>I've tried the following syntaxes without success:</p>
  
  <ul>
  <li><code>#{x.y.z?:'defaultValue'}</code></li>
  <li><code>#{${x.y.z}?:'defaultValue'}</code></li>
  </ul>
  
  <p>The first one gives me </p>
  
  <blockquote>
    <p>Field or property 'x' cannot be found
    on object of type
    'org.springframework.beans.factory.config.BeanExpressionContext'</p>
  </blockquote>
  
  <p>which suggests that SpEL doesn't recognise this as a property placeholder.</p>
  
  <p>The second syntax throws an exception saying that the placeholder is not recognised, so the placeholder resolver <em>is</em> being invoked, but is failing as expected, since the property is not defined.</p>
  
  <p>The docs make no mention of this interaction, so either such a thing is not possible, or it's undocumented.</p>
  
  <p>Anyone managed to do this?</p>
  
  <hr>
  
  <p>OK, I've come up with a small, self-contained test case for this. This all works as-is:</p>
  
  <p>First, the bean definitions:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;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
             "&gt; 
  
      &lt;context:property-placeholder properties-ref="myProps"/&gt;
  
      &lt;util:properties id="myProps"&gt;
          &lt;prop key="x.y.z"&gt;Value A&lt;/prop&gt;
      &lt;/util:properties&gt;
  
      &lt;bean id="testBean" class="test.Bean"&gt;
              &lt;!-- here is where the magic is required --&gt;
          &lt;property name="value" value="${x.y.z}"/&gt; 
  
              &lt;!-- I want something like this
          &lt;property name="value" value="${a.b.c}?:'Value B'"/&gt; 
              --&gt; 
      &lt;/bean&gt;     
  &lt;/beans&gt;
  </code></pre>
  
  <p>Then, the trivial bean class:</p>
  
  <p>package test;</p>
  
  <pre><code>public class Bean {
  
      String value;
  
      public void setValue(String value) {
          this.value = value;
      }
  }
  </code></pre>
  
  <p>And lastly, the test case:</p>
  
  <pre><code>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"));
      }
  }
  </code></pre>
  
  <p>The challenge - to come up with a SpEL expression in the beans file which allows me to specify a default value in cases where <code>${x.y.z}</code> cannot be resolved, and this default <em>must</em> be specified as part of the expression, not externalized in another property set.</p>