Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency relationships created by AbstractDependsOnBeanFactoryPostProcessors cannot be overridden #18362

Closed
jwenting opened this issue Sep 26, 2019 · 7 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@jwenting
Copy link

jwenting commented Sep 26, 2019

When upgrading to Spring Boot 2.1.8-RELEASE from 2.1.7-RELEASE we now get a BeanCreationException that didn't happen before (no code was changed, only the Spring Boot version number in the pom file).

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseEntityManager' defined in class path resource [xxxxxx/config/DatabaseContext.class]: Circular depends-on relationship between 'baseEntityManager' and 'delayedFlywayInitializer'
 	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
 	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
 	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:307)
 	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
 	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:307)
 	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
 	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1105)
 	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
 	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
 	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
 	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744)
 	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391)
 	at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
 	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
 	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204)

The bean definitions (in separate @Configuration annotated classes) are as follows:

    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean baseEntityManager() {
        return EntityManagerFactoryBuilder.buildEntityManagerFactory(dataSource(), "xxxxxx.config.MySQLCustomDialect",
                "nl.triopsys.aangetekendmailen.domain", JSR310_PACKAGE);
    }
    @Bean
    @DependsOn("baseEntityManager")
    FlywayMigrationInitializer delayedFlywayInitializer(Flyway flyway) {
        return new FlywayMigrationInitializer(flyway, null);
    }

Again, in Spring Boot 2.1.7 (and earlier, this code was originally written against Spring Boot 1.3 and has been working in production for several years) this works as expected, the delayedFlywayInitializer gets created after the baseEntityManager (in order to run Flyway migrations after Hibernate is done with its schema updates).

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 26, 2019
@wilkinsona
Copy link
Member

Thanks for the report and sorry for the inconvenience. This is an unanticipated side-effect of the fix for #18105.

We can't roll back that change without reinstating the problem that it addresses so I'm not yet sure what we should do. Ordering permitting, you may be able to work around the problem by using a BeanFactoryPostProcessor to set the dependsOn for the definition of your baseEntityManagerFactory bean, overwriting what Boot sets itself as of 2.1.8.

@wilkinsona wilkinsona added the for: team-attention An issue we'd like other members of the team to review label Sep 26, 2019
@philwebb philwebb changed the title Erroneous BeancreationException for circular dependency when upgrading to Spring Boot 2.1.8 Erroneous BeanCreationException for circular dependency when upgrading to Spring Boot 2.1.8 Sep 27, 2019
@wilkinsona wilkinsona added type: regression A regression from a previous release status: waiting-for-triage An issue we've not yet triaged and removed for: team-attention An issue we'd like other members of the team to review status: waiting-for-triage An issue we've not yet triaged labels Sep 27, 2019
@wilkinsona wilkinsona added this to the 2.1.x milestone Sep 27, 2019
@wilkinsona wilkinsona self-assigned this Sep 27, 2019
@wilkinsona
Copy link
Member

@jwenting I can reproduce the failure you have described with Spring Boot 2.1.8, but I cannot get it to work with 2.1.7. With 2.1.7 I see the failure that was fixed by #18105:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'flywayInitializer' available
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:771) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1221) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:294) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:307) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1105) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:743) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:390) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1214) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1203) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
	at com.example.demo.Gh18362Application.main(Gh18362Application.java:19) [main/:na]

Can you please provide, as a zip attached to this issue, a minimal sample that fails with 2.1.8 but works with 2.1.7?

@wilkinsona wilkinsona added status: waiting-for-feedback We need additional information before we can continue and removed type: regression A regression from a previous release labels Sep 27, 2019
@wilkinsona wilkinsona removed this from the 2.1.x milestone Sep 28, 2019
@jwenting
Copy link
Author

Will see what I can do. It's part of a pretty large system I inherited earlier this year, who knows what all the interdepencies are under the hood...

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Sep 30, 2019
@jwenting
Copy link
Author

demo.zip

The demo project will fail while running Flyway under 2.1.7, while trying to create the Flyway bean under 2.1.8

@wilkinsona
Copy link
Member

Thanks.

I can see that you have met 2.1.7's need for a bean named flywayInitializer by providing one that no-ops the migration. Unfortunately that's something of a hack that could have been taken as a signal that you were trying to do something that wasn't intended to work. Your use of both Hibernate and Flyway to update the database also doesn't follow the recommendation to use a single mechanism.

With that said, we'll see what we can do to allow you to continue with something similar to your current approach. Even if we're able to do something, you may want to consider reworking the way that you're using Flyway and Boot's auto-configuration of it. For example, providing your own Flyway bean, which will cause much of the auto-configuration to be off, may prove to be more robust.

@wilkinsona wilkinsona added this to the 2.1.x milestone Oct 2, 2019
@wilkinsona wilkinsona added for: team-attention An issue we'd like other members of the team to review type: bug A general bug and removed status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged labels Oct 2, 2019
@wilkinsona wilkinsona changed the title Erroneous BeanCreationException for circular dependency when upgrading to Spring Boot 2.1.8 Dependency relationships created by AbstractDependsOnBeanFactoryPostProcessors cannot be overridden Oct 2, 2019
@philwebb
Copy link
Member

philwebb commented Oct 2, 2019

We'll add an order to our AbstractDependsOnBeanFactoryPostProcessors to allow a post processor to change things.

@jwenting
Copy link
Author

jwenting commented Oct 3, 2019

From the commit messages in that piece of code I guess that they added the noop FlywayMigrationInitializer to force the loading of some application configuration settings from the database before the Flyway migration starts running.
That it also forces the Hibernate migration to run before Flyway was probably seen as a handy side effect more than anything.

I inherited this code that was created several years ago by people who're since gone on to other projects and/or left the company.

@wilkinsona wilkinsona removed the for: team-attention An issue we'd like other members of the team to review label Oct 3, 2019
@wilkinsona wilkinsona modified the milestones: 2.1.x, 2.1.10 Nov 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants