大家好我配置Spring Batch时遇到问题。我有已创建的@Primary dataSource的项目,我无法更改,但我想将批处理元数据保存在单独的内存数据库中,如H2。尝试了与其他帖子不同的东西,但最终仍然在@Primary dataSource中使用了metaData。这是我的示例代码:
@Configuration
@EnableBatchProcessing
public class BatchInfrastructureConfiguration{
@Bean
public TaskScheduler taskScheduler() {
return new ConcurrentTaskScheduler();
}
@Bean
public PlatformTransactionManager transactionManager(@Qualifier("batchDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean (name = "batchDataSource")
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder
.setType(EmbeddedDatabaseType.H2)
.build();
return db;
}
@Bean
DefaultBatchConfigurer defaultBatchConfigurer(@Qualifier("batchDataSource") DataSource dataSource){
return new DefaultBatchConfigurer(dataSource);
}
}
主要数据源:
@SpringBootApplication
public class ConfigtestApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigtestApplication.class, args);
}
@Bean
@Primary
public DataSource dataSource() throws ClassNotFoundException {
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setPassword("**");
dataSource.setUrl("jdbc:mysql://localhost/springbatch");
dataSource.setUsername("**");
dataSource.setDriverClass((Class<Driver>) Class.forName("com.mysql.jdbc.Driver"));
return dataSource;
}
}