views:

142

answers:

1

Before upgrading to Spring 3 I had this in my applicationContext.xml file:

    <bean class="com.northgateis.pole.ws.PolePayloadValidatingInterceptor">
      <property name="validateRequest" value="${validateRequest}" />
      <property name="validateResponse" value="${validateResponse}" />
    </bean>

where ${validateRequest) and ${validateRequest) refer to properties that may or may not be defined in my properties file.

In Spring 2, if these proeprties were not present in the properties file the setters on the bean were not called and so the defaults hard-coded in PolePayloadValidatingInterceptor were used.

After upgrading to Spring 3, it seems the behaviour is different: If the properties are not present in the properties file I get the following exception:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'annotationMapping' defined in class path resource [com/northgateis/pole/ws/applicationContext-ws.xml]: Could not resolve placeholder 'validateRequest'
 at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:272)
 at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:75)
 at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:640)
 at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:615)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:405)
 at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:272)

I tried dabbling with Spring EL but the following doesn't seem to work:

    <bean class="com.northgateis.pole.ws.PolePayloadValidatingInterceptor">
      <property name="validateRequest" value="${validateRequest?:true}" />
      <property name="validateResponse" value="${validateResponse?:false}" />
    </bean>

The value after the Elvis operator is always used, even when the properties are defined in the proeprties file. Interesting that the syntax is accepted.

Any suggestions?

+1  A: 

It looks like Spring 3's handling of default values with the Elvis operator was rather broken. This has apparently been fixed (see SPR-7209) in the fresh-out-of-the-oven Spring 3.0.3, and the correct syntax should be the rather baroque:

#{${validateRequest}?:true}
skaffman
Looks hopeful. This does indeed work when the property is null (i.e. blank in the properties file) but results in the same "Could not resolve placeholder 'validateRequest'" BeanDefinitionStoreException if the property is absent from the properties file. I'll try and re-open SPR-7209 and see if the Spring team will kindly allow absent to be treated as equivalent to blank.
David Easley