I am new to spring I am trying to create a Database Manager page which shows the database details on page load and updates the database settings when the user press submit
You need to refresh your Application context and get the bean DriverManagerDataSource
again in order to reflect the changes written to propertie files.
context.refresh();
DriverManagerDataSource databaseSource = (DriverManagerDataSource)context.getBean("dataSource");
databaseSource.getUsername();
Tip for getting File object of your resource:
URI a = getClass().getResource("/com/smartcall/bundle/database.properties").toURI();
File file = new File(a);
You will not need to decode the path, i.e. replacing %20 with " ". Referrence
The values are read when the application is started, they are set to the target objects (the data source in your case), and the properties file is no longer being read. So even if you change the values, they are not reflected anywhere.
For the PropertyPlaceholderConfigurer
to reload the values you'd have to extend it and specify a reload period. But that still won't update the data source. You should also extend the data source class and make it re-read properties after a given interval. Or you can declare the DataSource
to be of scope prototype
, so that a new instance is obtained.each time, and the properties - reloaded.
This is if you don't want to restart the whole context (which might take a lot of time)