wood burning stoves 2.0*
The moose likes Spring and the fly likes Want to change Application Property at startup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Want to change Application Property at startup " Watch "Want to change Application Property at startup " New topic
Author

Want to change Application Property at startup

Ravi Pandya
Greenhorn

Joined: Jun 07, 2007
Posts: 21

Hi to All,

I have situation in my application.
I have Property file called application.property and that one has some contains.

I want to change that contains at application startup, becore spring load.
I have to call server to get the latest values which I am going to use in my project.
so I have to change that onw before spring load so that I can use that one in my application.

Please guide me on this.

I have some idea that we can use PropertyPlaceholderConfigurer or PropertyOverrideConfigurer, but dont know how.

Ravi Pandya
SCJP 5, SCWCD 5
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Ravi Pandya wrote:
Hi to All,

I have situation in my application.
I have Property file called application.property and that one has some contains.

I want to change that contains at application startup, becore spring load.
I have to call server to get the latest values which I am going to use in my project.
so I have to change that onw before spring load so that I can use that one in my application.

Please guide me on this.

I have some idea that we can use PropertyPlaceholderConfigurer or PropertyOverrideConfigurer, but dont know how.


The PropertyPlaceholderConfigurer will read the .properties file and make them accessible in your configuration via ${}

so if you have a database.properties with say

database.url=my.url.com

Then in your Spring config file you declare a bean for the Configurer

<context:property-placeholder location="database.properties"/>

And use it in another bean



The URL property will be set to "my.url.com" from the properties file

Hope that helps and what you were looking for.

Mark>


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ravi Pandya
Greenhorn

Joined: Jun 07, 2007
Posts: 21

Hi.

I cannot out the value inside the config file, because I am getting this value from other server and then I have to user that one inside config file.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'd probably look into the property placeholder code and see how it does its variable substitution--just do the same thing, but getting the values from the other location instead of the properties file. My guess is that it shouldn't be that difficult.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

David Newton wrote:I'd probably look into the property placeholder code and see how it does its variable substitution--just do the same thing, but getting the values from the other location instead of the properties file. My guess is that it shouldn't be that difficult.


Other possibilities are

creating a FactoryBean class
implementing an init-method
create a BeanFactoryPostProcessor
create a PropertyEditor
if using Spring 3.0 create a Converter

Mark
Ravi Pandya
Greenhorn

Joined: Jun 07, 2007
Posts: 21
Hi.

thing is like this.

I have a property file which contains some properties.
At the Application startup I have to refresh this property file with some new properties.
and then I amd going to use that properties in to Application.

I am using Spring framework so I have to have something which refresh these properties with new values before spring loads.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

So you want to change the .properties file at Spring startup, meaning as Spring is starting up, it will update that file. Then later you will use that file to set properties in other classes.

So where does it figure out the new values, and why even need a .properties file if it is only used at Spring startup, and the Spring startup already then knows about the values. Why not then just use those values immediately instead of putting it into a properties file.

I think you might need to rethink your design.

What is the use case, and whys?

Mark
Ravi Pandya
Greenhorn

Joined: Jun 07, 2007
Posts: 21

Hi Mark.

Actually the flow is something like this.

I am taking some property values from property file and using those values, I am calling one server to get some more configuration values.

so for this thing I need property file for initial values and I am using spring so I need after that also.

This is a good way in my side because if I want to change something or anyone want to change something then we dont need to do that in property file. we just need to change in remote configuration server and restart the application, application will automatically take the new values at the startup and used those ones in application.

If you still feel that there is some other way to do this then please let me know.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Well, the purpose of a separate properties file is for that exact reason, you just change the properties file manually and restart the deployment. So now you are trying to go even further to externalize your externalized property file, but further externalizing the property file properties with another property file.

Aren't you going a little overboard here. Your location of the file can be "classpath:", "http:", or "file:"

Why not just use file so that the properties file is outside any .war and resides on the server.

I really think you are trying to over-complicate this.

Good Luck

Mark
Ravi Pandya
Greenhorn

Joined: Jun 07, 2007
Posts: 21

Hi Mark.

You are right, but this is requirement of our project.

we have to reinitialize our file.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I already gave you a solution that would handle this--why didn't you take it?
chitti mars
Greenhorn

Joined: Oct 23, 2011
Posts: 3
I have similar requirement, i have a db params in properties file.. at startup i need to encrypt the sensitive data and overwrite the properties file (with encrypted values). and while getting the db connection i need to decrypt internally and use for connection. Any suggestions please.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Want to change Application Property at startup