Announcement Announcement Module
Collapse
No announcement yet.
Controlling data source for property injection (@Value) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


    • #3
      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.

      Comment


      • #4
        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, 12:30 PM.

        Comment


        • #5
          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

          Comment


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

            Comment


            • #7
              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.

              Comment

              Working...
              X