Skip to content

Conversation

nosan
Copy link
Contributor

@nosan nosan commented Feb 27, 2019

Consider the following example:

public class EmbeddedMongoAutoConfigurationTests {

	private AnnotationConfigApplicationContext context;

	@After
	public void close() {
		if (this.context != null) {
			this.context.close();
		}
	}

	@Test
	public void customMongoServer() {
		load(CustomMongoConfiguration.class);
	}

	private void load(Class<?> config, String... environment) {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
		if (config != null) {
			ctx.register(config);
		}
		TestPropertyValues.of(environment).applyTo(ctx);
		ctx.register(EmbeddedMongoAutoConfiguration.class, MongoAutoConfiguration.class,
				PropertyPlaceholderAutoConfiguration.class);
		ctx.refresh();
		this.context = ctx;
	}

	@Configuration
	static class CustomMongoConfiguration {

		@Bean(initMethod = "start", destroyMethod = "stop")
		public MongodExecutable customMongoServer(IMongodConfig mongodConfig,
				IRuntimeConfig runtimeConfig, ApplicationContext context) throws IOException {
			MongodStarter mongodStarter = MongodStarter.getInstance(runtimeConfig);
			return mongodStarter.prepare(mongodConfig);
		}

	}
}

This test does not work because of

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongo' defined in org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration: 'mongo' depends on missing bean 'embeddedMongoServer'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'embeddedMongoServer' available

	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
	...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'embeddedMongoServer' available
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:775)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1221)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:294)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:307)
	... 31 more

So, If we define a custom bean with a different name from embeddedMongoServer, ApplicationContext will not start, as EmbeddedMongoDependencyConfiguration has added dependsOn to { MongoClient.class, MongoClientFactoryBean.class } beans, but embeddedMongoServer has not existed anymore as it was substituted by a custom bean.

This PR tries to fix this.

Thanks

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 27, 2019
@nosan nosan changed the title This commit checks the existence of 'dependsOn' beans before adding them to BeanDefinition Checks the existence of 'dependsOn' beans before adding them to BeanDefinition Feb 27, 2019
@philwebb
Copy link
Member

Thanks for the PR, but I feel like this change might just mask problems and make them harder to diagnose. I think instead we should change the depends on checks in EmbeddedMongoAutoConfiguration to be based on type rather than name. I've raised #16540 to look into that.

@philwebb philwebb closed this Apr 11, 2019
@philwebb philwebb added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 11, 2019
@nosan nosan deleted the patch-1 branch April 18, 2019 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants