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

huangapple go评论73阅读模式
英文:

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

问题

我有一个使用情况,在这个情况下我有一个自定义的 PropertySourcesPlaceholderConfigurer,它从数据库中读取属性,并且依赖于 DataSource Bean。

以前,Datasource Bean 只是在 .java 文件中使用凭据进行创建。所以一切都运行得很好。

现在,我们想要从 .properties/.yml 文件中基于配置文件来选择这些凭据。

但是,现在出现了一个循环,即 Spring 属性环境需要 Datasource,而 Datasource 需要 .properties 文件。

有人遇到过这个问题吗?有解决方案吗?

英文:

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

得分: 1

你可以按照以下方式重新构建你的应用程序:

  1. 根据你定义的配置文件(如 dev、int 和 prod)读取数据库凭据。
  2. 使用这些属性创建 Datasource 以访问你的数据库。
  3. 现在,通过访问数据库,你可以从数据库中读取所需内容,并将其放入 Singleton Spring bean 中,然后在需要的地方进行自动装配。
英文:

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.

huangapple
  • 本文由 发表于 2020年9月26日 21:00:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64077971.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定