This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
I'm developing a desktop application and spring is used in the service/dao layer. Right now our database connection info is specified in the applicationContext.xml file. What I need to do is have this be specified in some user settings that I can feed to Spring. If anyone has any know-how on doing this please let me know.
PropertyPlaceholderConfigurer. This will allow you to put the values into a .properties file so in the configuration, still in a separate xml file with using <import> but set properties like <property name="URL" value="${datasource.url}" where datasource.url is defined in the .properties file.
So in your UI of user settings, when they save it, it would write to the .properties file. Now here is the thing. Things like Datasources are immutable, so you can't change it on the fly. You would have to have code that closed the current ApplicationContext and create a new one, not really a clean solution.
Originally posted by Mark Spritzler: Things like Datasources are immutable, so you can't change it on the fly. You would have to have code that closed the current ApplicationContext and create a new one, not really a clean solution.
I agree that it's not a clean solution, but it makes things more dynamic. A better (and a little bit cleaner) solution is to make your datasource bean non singleton, and to reload it with the new parameters using the applicationContext.getBean() method. Of course this is not the cleanest solution ever.
If what you need is to specify the location of the database at the time of installation of the application, then I totally agree with Mark's first solution.
Mark, I use your solution in web apps all the time. I didn't think about how that would carry over to the desktop world so I am glad it is something I am familiar with. As far as it not being clean, it's not that bad.
Assuming a different database has different data my solution regardless was going be "You need to restart the application for these changes to take affect" anyway.