如果我有2个作业,每个作业都写入一个不同的数据源,则将弹簧批处理元数据(jobExecution,结果等)写入与其一起工作的数据源中是有意义的。然而,春季批处理似乎指导您拥有一个用于该元数据的“主要”数据源。
我定义了两个数据源,两个都未标记为主要数据,并且应用程序无法启动:
Field dataSource in org.springframework.batch.core.configuration.annotation.AbstractBatchConfiguration required a single bean, but 2 were found:
- firstDataSource: defined by method 'firstDataSource' in class path resource [secret/DataSourceConfig.class]
- secondDataSource: defined by method 'secondDataSource' in class path resource [secret/DataSourceConfig.class]
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
我试图创建两个配置,每个配置都扩展DefaultBatchConfigurer:
@Configuration
@EnableBatchProcessing
public class MyJobConfiguration extends DefaultBatchConfigurer {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Bean
public Job my_job(@Qualifier("somestep") Step step) {
return jobBuilderFactory.get("my_job")
.start(step)
.build();
}
@Bean
@Profile("my_job")
public JobExecution launchMyJob(@Qualifier("my_job") Job job, JobLauncher jobLauncher) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
Map<String, JobParameter> params = new HashMap<>();
params.put("Time", new JobParameter(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date())));
return jobLauncher.run(job, new JobParameters(params));
}
@Override
@Autowired
public void setDataSource(@Qualifier("firstDataSource") DataSource dataSource) {
super.setDataSource(dataSource);
}
}
另一个完全相同,只是不同的作业和数据源。
那么当我显然想通过扩展DefaultBatchConfigurer来自己做时,为什么spring仍然尝试创建AbstractBatchConfiguration?
更多堆栈跟踪:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration': Unsatisfied dependency expressed through field 'dataSource'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected single matching bean but found 2: firstDataSource,secondDataSource
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
答案 0 :(得分:0)
您是说要在项目中使用多个数据源?
以下是供您参考的示例
https://github.com/michaelliao/springcloud/tree/master/data-multidatasource
答案 1 :(得分:0)
Spring Batch的SimpleBatchConfiguration
通过@EnableBatchProcessing
引入。它使用BatchConfigurer
提供需要添加到ApplicationContext
的组件。您真正想做的是创建一个BatchConfigurer
,它在正确的时间提供正确的DataSource
。然后SimpleBatchConfiguration
将使用配置程序创建的组件将它们添加到ApplicationContext
。