Announcement Announcement Module
No announcement yet.
Replace old value with new value using Spring PropertyPlaceHolderConfigurer Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Replace old value with new value using Spring PropertyPlaceHolderConfigurer


    I have a properties file in which I have configured the thread pool data (count), this properties is loaded in spring context using the PropertyPlaceHolderConfigurer and will replace the thread pool data in the properties with the place holder defined in spring context. All is fine.

    Now based on the records count which I retrieve from the DB, Say I need to change the thread pool count from 10 to 15, similarly the connection pool which is defined in the same properties file needs to be changed from 10 to 15. Now how I can update the changes in the properties file and make sure the application context take that changes into account. Is there any way we can do it from Spring side by adding certain properties for the "PropertyPlaceHolderConfigurer" or whether we need to overwrite the new value by replacing the old value in the properties file using"threadcount", "15")? How we need to go about this please clarify.


  • #2
    Dear RithanyaLaxmi,

    Do I understand correctly that you want to create a PropertyPlaceHolderConfigurer that can retrieve data from your database?

    From the javadoc for the PropertyPlaceHolderConfigurer method resolvePlaceholder:

    Subclasses can override this for customized placeholder-to-key mappings or custom resolution strategies, possibly just using the given properties as fallback.

    It sounds as overriding this method would be a good point to do the lookup from your database.

    You can also google for DatabasePropertyPlaceholderConfigurer, a lot of people wrote similar code.

    Best wishes


    • #3
      Thanks. But I am not looking to retrive records from the Database through PlaceHolderConfigurer instead I want to replace the old property value (which is loaded through spring PropertyPlaceHolderConfigurer) with a new value. This new value will be written to the .properties file from a Java class. Now tell me how we can go about this in writing the new value to the property and how the spring applicationContext will be refreshed to accept this new value? Please clarify. I hope I am clear with the question.


      • #4
        Dear RithanyaLaxmi,

        What do you think of the following?

        1. Once you update the .properties file, get your subclass of PropertyPlaceHolderConfigurer from the application
        context (by getBean(..)) and call a new method (you could name it refreshProperties()) on it. This is the hacky way, if you
        want to do it really nice you could create
        a subclass of ApplicationContextEvent such as PropertiesChangedContextEvent and publish it to the application context
        and let it be picked up by the PropertyPlaceHolderConfigurer subclass.

        2. Let refreshProperties() call processProperties(ConfigurableListableBeanFactory, Properties) using the refreshed properties.
        If you want you can let your PropertyPlaceHolderConfigurer implement ApplicationContextAware, then it gets the application context
        on application context creation and you can pass this to processProperties.

        I haven't tried this but it sounds like it might work. You can also use a debugger and step through the
        PropertyPlaceHolderConfigurer and see how it works internally.

        Best wishes
        Marc Kropholler


        • #5
          Thanks Marc, Is there any examples or links on this that can be shared, as I am pretty new to this? Your help is highly appreciated in this regard. Thanks!


          • #6
            Replace old value with new value using Spring PropertyPlaceHolderConfigurer

            Dear RithanyaLaxmi,

            The main goal of spring is create, configure and connect the classes you create based on configuration you provide. The properties of your classes are set from the outside by spring (as opposed to set by the class itself or retrieved externally by the class This means you get smaller classes and also less connection between the classes, which means they are more easy to read and to use again, it is called dependency injection. You may want to read the javadoc for the BeanFactory interface and the general spring interface. I hope this helps, i can give some more info later on.

            Best wishes
            Marc Kropholler