使用Java Config时的Spring批处理表前缀
我的Spring Batch存储库(部署在Oracle数据库上)位于不同的模式中,因此我需要预先添加模式名称。
使用XML配置时,这很容易做到:
但是,当我使用Java Config时,事实certificate这更棘手。 我找到的最佳解决方案是让我的Java Config类extend DefaultBatchConfigurer
并覆盖createJobRepository()
方法:
@Configuration @EnableBatchProcessing public class BatchConfiguration extends DefaultBatchConfigurer{ @Autowired private DataSource dataSource; @Autowired private PlatformTransactionManager transactionManager; @Override protected JobRepository createJobRepository() throws Exception { JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); factory.setDataSource(dataSource); factory.setTransactionManager(transactionManager); factory.setTablePrefix("GFA.BATCH_"); factory.afterPropertiesSet(); return factory.getObject(); } ... }
与XML解决方案相比,这几乎是代码! 而且它也不合逻辑 – 我的第一个猜测是提供一个@Bean
方法,如下所示:
@Bean public JobRepository jobRepository() throws Exception { JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); factory.setDataSource(dataSource); factory.setTransactionManager(transactionManager); factory.setTablePrefix("GFA.BATCH_"); factory.afterPropertiesSet(); return factory.getObject(); }
但这不行。
我的问题是:我的解决方案是最优的还是有更好的解决方案? 我宁愿定义一个Bean,而不是必须覆盖一些不太直观的类的方法……显然,如果我们可以将代码缩短到接近单行代码,那将会更好。 XML配置。
只需将此行添加到批量配置中注册的任何属性文件中:
spring.batch.table-prefix= GFA.BATCH_
仅供参考, spring.batch
前缀与Spring引导提供的org.springframework.boot.autoconfigure.batch.BatchProperties
一起映射。 请参阅github上的源代码。