Results 1 to 7 of 7

Thread: Controlling data source for property injection (@Value)

  1. #1
    Join Date
    Aug 2010
    Location
    Broomfield, CO
    Posts
    20

    Question Controlling data source for property injection (@Value)

    Currently, @Value annotated fields retrieve their values from system properties at some point during Spring context initialization.

    I would like to know if there was a way to either:

    1) dictate where those values are retrieved (e.g., from someplace else other than system properties), or

    2) if that's not possible, inject something into the Spring lifecycle such that I could read my properties from someplace else and then add them to the system properties.

    For #2, we have a settings server defined in our Spring XML we would like to hit to retrieve these values. The key would be to have the settings server bean defined but prior to Spring processing the @Value annotations.

    Any assistance will be welcome, thanks.

  2. #2

    Default

    Yes. I use the PropertyPlaceholderConfigurer (http://static.springsource.org/sprin...onfigurer.html) to make values from a property file available.

    also see http://www.javaworld.com/community/node/8309

  3. #3
    Join Date
    Aug 2010
    Location
    Broomfield, CO
    Posts
    20

    Default

    Thanks for the reply. So, I'm a little confused. Are you suggesting I provide my own PropertyPlaceholderConfigurer? If so, I'm confused as to what method I would need to override to provide my values.

    Please note that my properties will not be coming from a properties file, but rather a property server - but I assume this can be injected.

  4. #4

    Default

    If I was in your shoes, I would look at the source for PropertyPlaceholderConfigurer and PropertyResourceConfigurer to see how I might extent/override/or reuse to create a bean that fetches the values from the property server and adds them to the context like reqular properties that one might pull from a .properties file.
    Last edited by jasonparallel; Feb 7th, 2013 at 12:30 PM.

  5. #5

    Default

    See this thread for how you could pipe jndi props into PropertyPlaceholderConfigurer
    http://forum.springsource.org/showth...search-in-JNDI

    I assume you could write something custom to push in as a location if you needed to

  6. #6
    Join Date
    Aug 2010
    Location
    Broomfield, CO
    Posts
    20

    Default

    Yea, I'll need to allot some time to play around with this. Thanks.

  7. #7
    Join Date
    Aug 2010
    Location
    Broomfield, CO
    Posts
    20

    Default

    So, it turns out one doesn't need to extend or override PropertyPlaceholderConfigurer (or any other class really), though I'm sure you could devise a working solution doing so. You can simply use Spring EL to wire field values as such:

    Code:
    @Component // i think requires <context:component-scan .../> or just declare your bean in your Spring XML.
    public class FooBean {
      
      public String getPropertyValue() {
        return "Hello World!";
      }
    }
    
    public class Bar {
    
      @Value(#{fooBean.propertyValue})
      private String propVal;
    
      public String getPropVal() {
    
        return this.propVal;  // will return "Hello World!"
      }
    }
    When Spring starts up, it will create a bean with the name of "fooBean" and then using Spring EL (note the # instead of the $) you can set the value of field based on a bean method (bean rules apply, they do for getters anyway).

    In our case, FooBean is a class that ultimately retrieves values from our settings server.

Tags for this Thread

Posting Permissions

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