使用Java Config时的Spring批处理表前缀 [英] Spring Batch Table Prefix when using Java Config

查看:773
本文介绍了使用Java Config时的Spring批处理表前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

My Spring Batch存储库(部署在Oracle数据库中)位于不同的模式中,因此我需要在模式名称前添加。

My Spring Batch repository (deployed on an Oracle database) lies in a different schema such that I need to prepend the schema name.

使用XML配置时,很容易做到:

When using XML configuration, this would be easy to do:

<job-repository id="jobRepository" table-prefix="GFA.BATCH_" />

然而,当我使用Java Config时,事实证明这更棘手。
我找到的最佳解决方案是让我的Java Config类扩展DefaultBatchConfigurer 并覆盖 createJobRepository()方法:

However, as I use Java Config, this turns out to be more tricky. The best solution I found is to have my Java Config class extend DefaultBatchConfigurer and override the createJobRepository() method:

@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 方法,如下所示:

Compared to the XML solution, that's pretty much code! And it's not too logical either - my first guess was to provide an @Bean method as follows:

@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配置中的行代码。

My question is: Is my solution optimal or is there a better one? I would prefer to define a Bean instead of having to override some method of some class which is not very intuitive... And obviously it would be even better if we could shorten the code to be somewhat close to the one-line code in the XML configuration.

推荐答案

只需将此行添加到批量配置中注册的任何属性文件中:

Simply add this line to any of your properties files registered on you batch configuration:

spring.batch.table-prefix= GFA.BATCH_

仅供参考,前缀 spring.batch org.springframework.boot.autoconfigure.batch映射随着Spring启动提供的.BatchProperties 。请参阅源代码

FYI, the prefix spring.batch is mapped with org.springframework.boot.autoconfigure.batch.BatchProperties provided with Spring boot. See source code on github.

这篇关于使用Java Config时的Spring批处理表前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆