无法将SQLServerDriver强制转换为DataSource

时间:2018-03-27 13:20:25

标签: java spring-batch spring-cloud-task

在我的Spring Cloud Task项目中,我使用的是Spring Batch。我想从生产数据中分离元数据(BATCH_TASK_表),所以我像这样配置两个DataSource:

# DataSource: Production data
prod.datasource.jdbc-url=jdbc:sqlserver://localhost;databaseName=PROD
prod.datasource.data-source-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
prod.datasource.username=...
prod.datasource.password=...


# DataSource: Jobs and Tasks metadata
tasks.datasource.jdbc-url=jdbc:sqlserver://localhost;databaseName=TASKS
tasks.datasource.data-source-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
tasks.datasource.username=sa
tasks.datasource.password=...

+

public class DataSourceConfig {

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


    @Bean
    @ConfigurationProperties(prefix = "tasks.datasource")
    public DataSource dataSourceTasks() {
        return DataSourceBuilder.create().build();
    }

}

+

@Configuration
@EnableTask
@EnableBatchProcessing
@Import(DataSourceConfig.class)
public class JobConfig {

    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    private StepBuilderFactory steps;


    @Bean
    public TaskConfigurer taskConfigurer(@Qualifier("dataSourceTasks") DataSource source) {
        return new DefaultTaskConfigurer(source);
    }


    @Bean
    public BatchConfigurer batchConfigurer(@Qualifier("dataSourceTasks") DataSource source) {
        return new DefaultBatchConfigurer(source);
    }

    //+jobs, steps...

当我运行它时,我从Cannot cast com.microsoft.sqlserver.jdbc.SQLServerDriver to javax.sql.DataSource bean获得taskConfigurer。我想念一下吗?

使用Spring Boot starter Batch 2.0.0.RELEASE和Cloud starter Task 2.0.0.M3

2 个答案:

答案 0 :(得分:1)

使用 prod.datasource.driverClassName

而不是

prod.datasource.data-source-class-name

答案 1 :(得分:0)

com.microsoft.sqlserver.jdbc.SQLServerDriver未实现javax.sql.DataSource它只是驱动程序。

使用实现javax.sql.DataSource的类,例如com.microsoft.sqlserver.jdbc.SQLServerDataSource

或者您甚至可以使用替代DataSource,例如合并DataSource,例如com.mchange.v2.c3p0.ComboPooledDataSource