Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change bean property/value during runtime in spring

I am using spring mvc+hibernate+two databases

So for example: I create 2 sessionFactories. sessionFactory1 (using datasource1) and sessionFactory2 (using datasource2).

Would it be possible to change sessionFactory1 or sessionFactory2 to sessionFactory at runtime so that the dao/s references them. sessionFactory is already autowired to all dao/s.

I am searching for it right now I think @Configuration can help me but I am not sure.

I am trying AbstractRoutingDataSource but don't think it helps.

like image 685
precursor Avatar asked Nov 04 '22 11:11

precursor


1 Answers

Usually Spring wires your beans on application startup, so "re-wiring" (replacing references to sessionFactory1 with references to sessionFactory2 on runtime) does not seem easy to implement.

Maybe you could implement a "proxy bean" that is wired to your DAO objects and change the "target SessionFactory" of your proxy bean.

like image 192
Matthias Wuttke Avatar answered Nov 13 '22 04:11

Matthias Wuttke