在我的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
答案 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