Hello Team,
Today I found a potentially interesting bug when I was working on a small application that uses DataSource beans. In my situation, I wanted to declare two DataSource beans and use one of them dynamically using AbstractRoutingDataSource, which is declared as @Primary
bean. Surprisingly, I was not able to run my application because of cyclic dependency:
org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthContributorAutoConfiguration
┌─────┐
| dataSource defined in <myclass>
↑ ↓
| readOnlyDataSource defined in <myclass>
↑ ↓
| org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker
└─────┘
It looks that both of those beans: DataSourceHealthContributorAutoConfiguration and DataSourceInitializerInvoker have a dependency on DataSource - it's not cyclic.
I've created a small app to allow you quickly reproduce this bug: https://github.com/kozub/spring-dependency-management-bug - in this case, there is a problem with resolving dependency for HibernateJpaConfiguration and DataSourceInitializerInvoker. It looks the same, so I believe that the difference in these two cases is a consequence of different jars on the classpath.
Please let me know if there are any additional details that I might provide to you.
Comment From: HAHAHA2333
I see your problem is that the database source was referenced several times during spring boot initialization.
Therefore, the first step: Change the comments on your startup class Will @ SpringBootApplication () Modified to @ SpringBootApplication (exclude = {DataSourceAutoConfiguration. Class}) Then, step two: Modify the constructor of your MyRoutingDataSource class Will: Public void setCurrentDS(DataSource currentDS) { Enclosing currentDS = currentDS; } Is amended as: Public MyRoutingDataSource(DataSource currentDS,String name) { Enclosing currentDS = currentDS; Map
Comment From: sbrannen
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.