1

I have wired two datasources following examples for Oracle DB:

@Configuration public class SpringConfigurationProperties extends DataSourceProperties {

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

@Bean
@Qualifier("primaryDatasource")
public NamedParameterJdbcTemplate primaryNpJdbcTemplate(DataSource dataSource) {
    return new NamedParameterJdbcTemplate(dataSource);
}

@Bean
@ConfigurationProperties(prefix="gps.bulk.load.database")
public DataSource bulkLoadDatasource() {
    return DataSourceBuilder.create().build();
}

@Bean
@Qualifier("bulkLoadDatasource")
public NamedParameterJdbcTemplate bulkLoadNpJdbcTemplate(DataSource dataSource) {
    return new NamedParameterJdbcTemplate(dataSource);
}

}

But I am geting the following error on startup:

org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker required a single bean, but 2 were found

  • looks like you have clashes with your datasources... so change @Qualifier("primaryDataSource") to "somethingDatasource" – Jonathan JOhx Feb 06 '19 at 17:17
  • May help https://stackoverflow.com/questions/51711250/connecting-to-multiple-database-in-spring-boot – rjdkolb Feb 06 '19 at 18:34

1 Answers1

0

I think i could reproduce this error:

Parameter 1 of constructor in org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker required a single bean, but 2 were found: - myConfig: defined in file [...MyConfig.class] - spring.datasource-org.springframework.boot.autoconfigure.jdbc.DataSourceProperties: defined in null

Seems like spring boot found 2 DataSourceProperties for the constructor DataSourceInitializerInvoker, i am not sure why, because i have only the one class that you have but when i mark my Configuration as Primary it works.

@Configuration 
@Primary
public class SpringConfigurationProperties extends DataSourceProperties {
...

}

Additional for Comment .properties:

spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driver-class-name=test

gps.bulk.load.database.url=test
gps.bulk.load.database.username=test
gps.bulk.load.database.password=test
gps.bulk.load.database.driver-class-name=test

pL4Gu33
  • 2,045
  • 16
  • 38
  • I had tried that at one point, but after some other configuration changes, yes this did get me past that error, but now I am getting: Caused by: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required. I have in my properties, and this was working with one DS, gps.prio.database.url=jdbc:oracle:thin:@... – Michael McCarthy Feb 06 '19 at 18:20
  • Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.JpaVendorAdapter]: Factory method 'jpaVendorAdapter' – Michael McCarthy Feb 06 '19 at 18:24
  • Caused by: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required – Michael McCarthy Feb 06 '19 at 18:24
  • But `gps.prio.database.url` is not your datasource it should be `gps.bulk.load.database.url` and `spring.datasource.url` or ? (I edit my answer) – pL4Gu33 Feb 06 '19 at 18:29
  • yea sorry that was another datasource that is still in the prop file just copied the wrong one to this thread. I got past the issues of start up, but no matter what I do, the secondary datasource always picks up the spring.datasource and ignores the override of @ConfigurationProperties... – Michael McCarthy Feb 07 '19 at 15:02