简体   繁体   中英

Change Java Spring Boot Bean while runtime

Hello fellow developers,

i created a library using the Spring boot framework.

This library is creating a dynamic database connection using @Beans where i create a "data" Bean which holds the unlimited Datasource beans provided by a Postgresql db. At the end i wanted to have a dynamic db connection which could be triggered from outside to change the db i want to connect to. The information of the different databases where stored as mentioned inside of a postgres. This is loaded at the application start into this bean. My Problem is, that i'm not able to switch between the different Datasource beans. Spring boot is creating them, but it seems like it's not possible to change the bean started at runtime of the application which only holds one of the unlimited Datasources... So also after a retriggering of the creation of the original bean it still uses the old datasource.

Is there a way to use the beans from spring boot and change them on runtime? Regards, Andreas

I believe you are asking for DB multitenancy support where tenants information is stored in a Postgres DB.

Configuring the persistent layer for multi-tenancy support involves configuring:

  • Hibernate, JPA and Datasources properties
  • Datasources beans
  • Entity manager factory bean
  • Transaction manager bean
  • Spring Data JPA and annotation-driven transactions

I recently blog about Multi-tenant applications using Spring Boot, JPA, Hibernate and Postgres and although the tenants data is stored in a yml "properties" file, it shouldn't be difficult to convert it to read tenant data from a DB. I think it would be a starting point for what you would like to accomplish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM