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