wood burning stoves 2.0*
The moose likes Spring and the fly likes Database datasource bean: how to set properties at runtime? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Database datasource bean: how to set properties at runtime?" Watch "Database datasource bean: how to set properties at runtime?" New topic

Database datasource bean: how to set properties at runtime?

Paul Piesiak

Joined: Sep 06, 2011
Posts: 6


I think it would be easier to understand the problem if I wrote its context first. I have a Spring application which uses data stored in a database. The database info such as url, username and password should not be fixed. Here's what I want to achieve: after the applications starts, the user is prompted for a url of the database, username and password. The given information is used to set the properties of a datasource bean.

I did find a solution but I don't think it's in a good programming style. Here's the xml file code with the definition of the datasource bean:

The prompting for the db information takes place in the main method. Here's the code:

and the setting of the properties takes place at the construction of Zarzadca object:

This solutions works but I'm not happy with it. I know there is a possibility to point the bean to it's init method, where I could prompt for and set the properties of the db. So I was thinking about writing a class that extends the com.mchange.v2.c3p0.ComboPooledDataSource class and put an init method there. Sadly, I found out that the class is final and therefore cannot be extended. Could someone please help me solve this? I would be very thankful.

p.s. oh, and I would rather not use the property files.
Paul Piesiak

Joined: Sep 06, 2011
Posts: 6

Ok I think I found another solution that looks much better.
I created an interface called DataSourceConfigInterface:

I also created a class ComboPooledDataSourceConfig that implements this interface and is used to set the com.mchange.v2.c3p0.ComboPooledDataSource implementation of the data source. In this class the user is promted for the url, username and password and these information are set to the data source:

I moved the definition of dataSource bean from the dao context to another xml file, so now I have this:

and a new xml file dbDataSourceContext.xml

notice that I used the "init-method" in the dataSourceConfig bean so the method would start automatically. If one would use some other data source implementation which properties need to be set up at runtime, just create a new config class and a new xml file like the one above.
So now my main method looks like this:

and my Zarzadca constructor looks like this:

I think this looks much better than the previous code. But if anyone has a different idea or would like to comment this solution please let me know.
subject: Database datasource bean: how to set properties at runtime?
Similar Threads
data not persisted using hibernate spring integration
XML name space error in Spring data JPA hibernate applicationContext.xml
XML: multiple bean references - how to change them in one line?
totally stumped by synchronization.afterCommit error
running before I can crawl ( or how to add Spring to an existing Hibernate standalone app)