Skip to content

DatabaseComponentAndBeanInitializationOrdering doesn't support DataSource implementations #643

Closed
@alexpm-14

Description

What version of OpenRewrite are you using?

I am using

  • Maven plugin v5.46.1
  • rewrite-spring v5.24.1

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a single module project.

<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>5.46.1</version>
<configuration>
	<activeRecipes>
		<recipe>org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_3</recipe>
	</activeRecipes>
</configuration>
<dependencies>
	<dependency>
		<groupId>org.openrewrite.recipe</groupId>
		<artifactId>rewrite-spring</artifactId>
		<version>5.24.1</version>
	</dependency>
</dependencies>
</plugin>

What is the smallest, simplest way to reproduce the problem?

Include the plugin configuration in a simple project with this snippet configuring a DataSource:

@SpringBootApplication
public class WithDatabaseApplication {

	public static void main(String[] args) {
		SpringApplication.run(WithDatabaseApplication.class, args);
	}

	@Bean
	@ConfigurationProperties(prefix = "app.datasource")
	public HikariDataSource myDataSource() {
		return new HikariDataSource();
	}

}

What did you expect to see?

No changes in my class related to @DependsOnDatabaseInitialization annotation

What did you see instead?

The recipe DatabaseComponentAndBeanInitializationOrdering identify the HikariDataSource type (from method bean returnType) constructors as methods returning DataSource implementations, including the annotation as showed:

@SpringBootApplication
public class WithDatabaseApplication {

	public static void main(String[] args) {
		SpringApplication.run(WithDatabaseApplication.class, args);
	}

	@Bean
        @DependsOnDatabaseInitialization
	@ConfigurationProperties(prefix = "app.datasource")
	HikariDataSource myDataSource() {
		return new HikariDataSource();
	}

}

With this annotation the application fails due to circular reference because the Database initialization depends on the DataSource (and viceversa).

If I change the returnType to the interface DataSource it's not including the annotation, but i think it shouldn't neither for DataSource implementations.

What is the full stack trace of any errors you encountered?

2024-11-29T14:19:28.590+01:00  WARN 13400 --- [withDatabase] [           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: Error creating bean with name 'dataSourceScriptDatabaseInitializer': Requested bean is currently in creation: Is there an unresolvable circular reference?
2024-11-29T14:19:28.598+01:00  INFO 13400 --- [withDatabase] [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2024-11-29T14:19:28.626+01:00  INFO 13400 --- [withDatabase] [           main] .s.b.a.l.ConditionEvaluationReportLogger : 

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-11-29T14:19:28.661+01:00 ERROR 13400 --- [withDatabase] [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
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]
└──<-──┘


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.


Process finished with exit code 1

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    • Status

      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions