Announcement Announcement Module
No announcement yet.
Combining SpEL and property placeholders Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Combining SpEL and property placeholders

    Spring 3 has introduced a new expression language (SpEL) which can be used in bean definitions. The syntax itself is fairly well specified in the docs.

    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?

    Let me give a concrete example. I want to use the property syntax ${x.y.z}, backed by a custom PropertyPlaceholderConfigurer, but with the addition of "default value" syntax as provided by the elvis operator to handle cases where ${x.y.z} is undefined.

    I've tried the following syntaxes without success:


    Both result in an exception of some form or another.

    The docs make no mention of this interaction, so either such a thing is not possible, or it's undocumented.

    Anyone managed to do this?

  • #2
    Why not just use utilroperties (or PropertiesFactoryBean) with location(s) pointing to non-default settings, and default settings to be provided as local properties of utilroperties - by default local-override attribute of utilroperties is false, so default property values from XML will be overridden by property values in properties file. You can reference then properties in classes via @Value and/or XML with support of standard PropertyPlaceholderConfigurer.


    • #3
      I've considered that, but I need to able to keep the defaults specified "inline", as part of the expression. The reason for this is that various different bean definitions use the same property placeholder, but need different defaults.


      • #4
        Assuming that "various different bean definitions" are of same type/class, so every instance is defined as separate bean in XML referencing same class, then create constructor in bean class which will receive default value, and setter which will receive non-default value. Probably it would be good to externalize default values too.


        • #5
          Spring 3.0.x changelog, Changes in version 3.0.0.RC1 (2009-09-25):
          * PropertyPlaceholderConfigurer supports "${myKey:myDefaultValue}" defaulting syntax


          • #6
            Trial and error found the following works:
            #{'${x.y.z:}}'.length()==0?'defaultValue':'${x.y.z :}'}
            Raised SPR-7209 to cover this as it does seem a little unkind to developers even if it turns out this simply how things work.