Results 1 to 6 of 6

Thread: Combining SpEL and property placeholders

Hybrid View

  1. #1
    Join Date
    Aug 2005
    Posts
    39

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

    #{x.y.z?'defaultValue'}
    #{${x.y.z}?'defaultValue'}

    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. #2
    Join Date
    May 2007
    Posts
    157

    Default

    Why not just use util:properties (or PropertiesFactoryBean) with location(s) pointing to non-default settings, and default settings to be provided as local properties of util:properties - by default local-override attribute of util:properties 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. #3
    Join Date
    Aug 2005
    Posts
    39

    Default

    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. #4
    Join Date
    May 2007
    Posts
    157

    Default

    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. #5
    Join Date
    May 2007
    Posts
    157

    Default

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

  6. #6
    Join Date
    Apr 2010
    Posts
    8

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •