Announcement Announcement Module
Collapse
No announcement yet.
Initializing a Map with the @Value annotation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Initializing a Map with the @Value annotation

    The current @Value annotation seems to only work for simple, basic types.

    Is there a way to use it to initialize a map?

    That is, installing a collection of key value pairs driven from external properties?

    For my particular case, the keys are constants but the values are property driven. It would sure
    be nice to have less XML.

  • #2
    @Value is there really for setting values with Spring Expression Language SPel. So if you can write a SPel expression that evaluates to a Map, then yes you can use @Value.

    If you can't write a SPel, then you can write code to do the work.

    Also a Properties object is a Hashtable, so you can assign your Map directly from your Properties.

    Look at utilroperties as another solution, or even used in conjunction with SPel and @Value.

    Hope that helps.

    Mark

    Comment


    • #3
      Really appreciate the response and great ideas

      Great ideas, didn't pan out for me. Maybe I'm missing something.

      Did some digging into SPel and verified its support for lists but found no support for creating a map.
      Also noticed while perusing the source code that multidimensional arrays are not supported. Unless
      someone can post a map example, I don't think that it is currently possible.

      Assigning Map from Properties doesn't quite work since the key I want for my map is not the key for
      Properties.

      I could of course have local fields for all the values and then build the map programmatically,
      but that would involve having an init() method (or a lazy load implementation) and it seems that
      that direction is getting way too complicated.

      For now, it seems that the easiest way to do this is to have this one bean wired via XML and all
      the other beans be auto-scanned, @Autowired, and initialized by @Value. Project-wide, we don't
      have a huge number of these things, so it is not the end of the world.

      Comment

      Working...
      X