I am using springboot 2.6.3. I am using multiple (2) sql datasource, something a little similar to what is explained here

So I have 2 @Configuration classes with all the different @Bean namely; DataSourceProperties DataSource LocalContainerEntityManagerFactoryBean PlatformTransactionManager

I have a third @Configuration Appconfig where I have the following;

    @Bean
    @Primary
    public ObjectMapper mapper() {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        return new ObjectMapper().setDateFormat(df);
    }

We've had that config since about springboot 2.3.X. But with 2.6.X it throws the following;

2022-01-28 16:53:07.844  WARN [myApp,,] 17192 --- [           main] ConfigServletWebServerApplicationContext : Exception 
encountered during context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 
'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init
/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 
'dataSourceScriptDatabaseInitializer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException:
 Error creating bean with name 'mapper' defined in class path resource [com/acme/AppConfig.class]: Bean instantiation via factory
 method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[com.fasterxml.jackson.databind.ObjectMapper]: Factory method 'mapper' threw exception; nested exception is 
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'mapper': Requested bean is
 currently in creation: Is there an unresolvable circular reference?

***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

   dataSourceScriptDatabaseInitializer defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]
┌─────┐
|  mapper defined in class path resource [com/acme/AppConfig.class]
└─────┘

Action:

Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.

I tried using Jackson2ObjectMapperBuilder but result was quite similar.

If I disable @SpringBootApplication(exclude = { SqlInitializationAutoConfiguration.class }) Then I get the following;

2022-01-28 17:17:09.575  WARN [myApp,,] 18513 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mapper' defined in class path resource [com/acme/AppConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.fasterxml.jackson.databind.ObjectMapper]: Factory method 'mapper' threw exception; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'mapper': Requested bean is currently in creation: Is there an unresolvable circular reference?

The dependencies of some of the beans in the application context form a cycle:

┌──->──┐
|  mapper defined in class path resource [com/acme/AppConfig.class]
└──<-──┘

It does work in other applications/microservices using datasources, but not when they are manually created as such. Any pointer on how to get forward would be appreciated.

Regards.

Comment From: manofthepeace

Ok so, a library project was autowiring the ObjectMapper. I cannot fully (or at all) explain still the circular dependency.

Having a new objectMapper instance in that dependency made the error go away.

I tried putting @lazy on it in the library module autowired ObjectMapper, but it did error still happened.

Would be nice if I could understand the underlying, but if not at least the error is fixed.