Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

configure multiple datasource with spring including namedparameterjdbctemplate

I have the following db config class in my project where i try to configure multiple jdbctemplate and a named jdbc template for one of the two data sources:

@Configuration
public class DatabaseConfiguration {

@Bean(name = "abcDataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource abcDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean(name = "jdbcABC")
@Autowired
public JdbcTemplate abcJdbcTemplate(@Qualifier("abcDataSource") DataSource datasource) {
    return new JdbcTemplate(datasource);
}

@Bean(name = "namedJdbcABC")
public NamedParameterJdbcTemplate namedParameterJdbcTemplate(@Qualifier("abcDataSource") DataSource datasource) {
    return new NamedParameterJdbcTemplate(datasource);
}


@Bean(name = "shDataSource")
@ConfigurationProperties(prefix = "spring.shdatasource")
public DataSource shDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean(name = "jdbcSh")
@Autowired
public JdbcTemplate shJdbcTemplate(@Qualifier("shDataSource") DataSource datasource) {
    return new JdbcTemplate(datasource);
}

I seem to have configured everything correctly in my yaml file:

spring:
    profiles:
        active: dev
    output.ansi.enabled: always

    shdatasource:
        driverClassName: oracle.jdbc.OracleDriver
        url: jdbc:oracle:thin:@sh.database.url:1521:sid
        username: test
        password: test
    datasource:
        driverClassName: oracle.jdbc.OracleDriver
        url: jdbc:oracle:thin:@abc.db.url:1521:oll
        username: test
        password: test

But i get error:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'somedao': Unsatisfied dependency expressed through field 'namedParameterJdbcTemplate';

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'abcDataSource' threw exception; nested exception is java.lang.IllegalStateException: No supported DataSource type found

Anybody can tell me If I misconfigured something?

like image 673
M06H Avatar asked Oct 20 '25 15:10

M06H


1 Answers

Doing this way worked for me:

@Configuration
public class DatabaseConfiguration {

    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource")
    public DataSourceProperties abcDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "abcDataSource")
    @Primary
    @ConfigurationProperties("spring.datasource")
    public DataSource abcDataSource() {
        return abcDataSourceProperties().initializeDataSourceBuilder().build();
    }


    @Bean
    @ConfigurationProperties("spring.shDataSource")
    public DataSourceProperties shDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "shDataSource")
    @ConfigurationProperties("spring.shDataSource")
    public DataSource shDataSource() {
        return shDataSourceProperties().initializeDataSourceBuilder().build();
    }

    @Bean(name = "jdbcABC")
    @DependsOn("abcDataSource")
    public JdbcTemplate abcJdbcTemplate(@Qualifier("abcDataSource") DataSource abcDataSource) {
        return new JdbcTemplate(abcDataSource);
    }


    @Bean(name = "namedJdbcABC")
    @DependsOn("abcDataSource")
    public NamedParameterJdbcTemplate namedParameterJdbcTemplate(@Qualifier("abcDataSource") DataSource abcDataSource) {
        return new NamedParameterJdbcTemplate(abcDataSource);
    }

    @Bean(name = "jdbcSh")
    @DependsOn("shDataSource")
    public JdbcTemplate shJdbcTemplate(@Qualifier("shDataSource") DataSource shDataSource) {
        return new JdbcTemplate(shDataSource);
    }

}

Then, in my main class I injected my beans:

@SpringBootApplication
public class Application implements CommandLineRunner {    


    @Autowired
    @Qualifier("abcDataSource")
    DataSource abcDataSource;

    @Autowired
    @Qualifier("shDataSource")
    DataSource shDataSource;

    @Autowired
    @Qualifier("jdbcABC")
    JdbcTemplate abcJdbcTemplate;

    @Autowired
    @Qualifier("namedJdbcABC")
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Autowired
    @Qualifier("jdbcSh")
    JdbcTemplate shJdbcTemplate;


    @Override
    public void run(String... strings) throws Exception {
        System.out.println(abcDataSource.toString());
        System.out.println(shDataSource.toString());
        System.out.println(abcJdbcTemplate.toString());
        System.out.println(namedParameterJdbcTemplate.toString());
        System.out.println(shJdbcTemplate.toString());
    }


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

}

and it worked as expected.

like image 105
Thiago Procaci Avatar answered Oct 23 '25 05:10

Thiago Procaci



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!