Update database settings in properties file in Spring

后端 未结 2 1901
醉话见心
醉话见心 2021-01-24 14:57

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

相关标签:
2条回答
  • 2021-01-24 15:14

    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

    0 讨论(0)
  • 2021-01-24 15:20

    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)

    0 讨论(0)
提交回复
热议问题