Ben Tennyson
Ben Tennyson

Reputation: 649

@ConditionalOnProperty on the Configuration file is not loading the bean

I would like to load liquibase properties and initiate H2 database when I set use.liquibase is true. In all the other cases, I would like to go to the corresponding RDBMS instance. I have code something like this

@PropertySource("liquibase.properties")
@ConditionalOnProperty(value = "use.liquibase", havingValue = "true")
@Configuration
public class LiquibaseDaoConfig {

    @Bean(name = "dataSource")
    public DataSource dataSource(...) {
    //load properties from liquibase.properties
        //return H2 Datasource
    }
}

@Configuration
public class DaoConfig{
    @Bean(name = "dataSource")
    @ConditionalOnProperty(value = "use.liquibase", havingValue = "false")
    @Primary
    public DataSource dataSource(....) {
       // return Oracle Datatsource
    }

    @Bean
    public StuffDao stuffDao(DataSource dataSource) {
        return new StuffDaoImpl(dataSource);
    }
}

use.liquibase = true

I get No qualifying bean of type 'javax.sql.DataSource' available error. What am I doing wrong? Please help.

Upvotes: 0

Views: 705

Answers (1)

Antoniossss
Antoniossss

Reputation: 32517

havingValue = "false"

this will work if and only if property value will be exactly false. It wont work for for any other value that would normally resolve to logical false like eg. missing value.

So in general, check for typos and if given configuration classes arr on componen scan path

Upvotes: 1

Related Questions