Affects: 5.3.x

In Spring Boot we have some bean factory post-processors that ensure that a bean that initialises the database (such as Flyway or Liquibase) is depended on by beans that access the database (such as JdbcTemplate or EntityManagerFactory). We do this by adding bean names to the definition's dependsOn.

We've recently had an issue raised by a user who was trying to use JPA within a custom Liquibase changeset implementation. To our surprise, this works some of the time as the bean factory's eager caching of a bean allows it to break the cycle. I say some of the time as the breaking of the cycle was successful in the user's attempt at reproducing the problem but it does not succeed in their actual application.

This inconsistent behaviour coupled with our desire to guarantee that the database will have been initialized before anything tries to access it resulted in me wondering if it would be possible to disable the eager caching of a bean on a bean-by-bean basis. Perhaps via an attribute on the bean definition that we could set at the same time as we're configuring the dependencies?

We may also experiment with settings allowCircularReferences to false by default, similar to what we did earlier in 2.x where we disabled bean definition overriding by default.

Comment From: izeye

The "an issue" above appears to mean to point https://github.com/spring-projects/spring-boot/issues/27231, not https://github.com/spring-projects/spring-boot/issues/27325.

Comment From: wilkinsona

Thanks, @izeye. I’ve corrected the link.

Comment From: jhoeller

On review, I would rather recommend complete disabling of circular references, or designing for lazy resolution of a cycle through @Lazy or ObjectProvider. Individual disabling does not guide people into the right direction, arguably.