I followed this tutorial to configure a Spring Batch job in Java. It makes provision for multiple data sources by using an interface that's then implemented by each data source.
This is what I have so far:
InfrastructureConfig.java
public interface InfrastructureConfiguration {
@Bean
DataSource dataSource();
}
MySQLConfig.java
@Configuration
@Primary
public class MySQLConfiguration implements InfrastructureConfiguration {
@Bean
public DataSource dataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/employees?useSSL=false");
dataSource.setUsername("testing");
dataSource.setPassword("testing");
return dataSource;
}
}
PostgreSQLConfig.java
@Configuration
public class PostgreSQLConfiguration implements InfrastructureConfiguration {
@Bean
public DataSource dataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://localhost:5432/postgres");
dataSource.setUsername("postgres");
dataSource.setPassword("testing");
return dataSource;
}
}
JobConfig.java
@Configuration
public class JobConfig {
@Autowired
private InfrastructureConfig infrastructureConfig
....
}
By using the @Primary
annotation for my MySQLConfig, I'd expect the mySQLConfig bean to be used. Instead, I get this:
2017-03-09 12:46:21.422 INFO 1496 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'dataSource' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=mySQLConfiguration; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [config/MySQLConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=postgreSQLConfiguration; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [config/PostgreSQLConfiguration.class]]
It's overriding the mySQLConfig bean with the postgreSQLConfig bean, and thus using the postgresql driver. The question is, why?