Is it possible to configure a PropertySourcesPlaceholderConfigurer that reads from DataBase which further depends on properties file?

Viewed 30

I have a use case where i have a custom PropertySourcesPlaceholderConfigurer which reads properties from Database and have a dependency on DataSource Bean.

Earlier, Datasource bean was getting created using credentials in .java file only. and so things were working fine.

Now, we want to pick those credentials profile based from .properties/.yml file.

But, now it's like a cycle i.e. Spring properties environment needs Datasource and Datasource needs .properties.

Has anybody faced this problem ? Is there any solution ?

1 Answers

You can restructure your application that way:

  1. Read database credentials from the properties file based on profile you define (e.g. dev, int and prod).
  2. Use those properties in order to create Datasource to access your database.
  3. Now, having the access to the database, you can read what you want from the database and put it into Singleton Spring bean which you autowire where you want.
Related