Closed
Description
Hello poeple,
I lost some hours configuring a bean with same id but different properties in java config, as the raised error message was not very helpful.
The bean in question must be created, as it is required via declarative @DependsOn
configuration.
The error
A component required a bean named 'postgresqlContainer' that could not be found.
- Bean method 'postgresqlContainer' in 'PersistenceConfig.DbServersConfig' not loaded because @ConditionalOnProperty (app.persistence.servers.postgresql.enabled=true) found different value in property 'enabled'
- Bean method 'postgresqlContainer' in 'PersistenceConfig.DbServersConfig' not loaded because @ConditionalOnProperty (app.persistence.servers.postgresql.enabled=true) found different value in property 'enabled'
This config fails:
@Configuration
public class PersistenceConfig {
@Configuration
public class DbServersConfig {
@Bean(value = "postgresqlContainer", initMethod = "start", destroyMethod = "stop")
@ConditionalOnProperty(prefix = "app.persistence.servers.postgresql", name = "enabled", havingValue = "true")
public PostgreSQLContainer postgresqlContainer(TCPostgresqlProperties postgresqlProperties) {
Instance retVal = <do_your_logic>
return retVal;
}
@Bean("postgresqlContainer")
@ConditionalOnProperty(prefix = "app.persistence.servers.postgresql", name = "enabled", havingValue = "false", matchIfMissing = true)
public PostgreSQLContainer postgresqlContainer() {
return null;
}
}
}
This config works:
@Configuration
public class PersistenceConfig {
@Configuration
public class DbServersConfig {
@Bean(value = "postgresqlContainer", initMethod = "start", destroyMethod = "stop")
@ConditionalOnProperty(prefix = "app.persistence.servers.postgresql", name = "enabled", havingValue = "true")
public PostgreSQLContainer postgresqlContainer(TCPostgresqlProperties postgresqlProperties) {
Instance retVal = <do_your_logic>
return retVal;
}
@Bean("postgresqlContainer")
@ConditionalOnProperty(prefix = "app.persistence.servers.postgresql", name = "enabled", havingValue = "false", matchIfMissing = true)
public PostgreSQLContainer postgresqlContainer2() {
return null;
}
}
}
If you name both methods differently (postgresqlContainer
and postgresqlContainer2
) then everything works as expected otherwise you get an error.
Is this desired behavior ?
I am unsure if my scenario could indicate a bug, too. I do not know if this problem happens only to @ConditionalOnProperty
or any other condition like @ConditionalOnExpression
.
Best regards