Spring 批处理 2.2 JavaConfig [英] Spring batch 2.2 JavaConfig
问题描述
我正在尝试让 Spring Batch 2.2 与 JavaConfig 一起工作.
现在他们有一个 @EnableBatchProcessing
注释来设置很多东西.默认该注解为其作业数据使用数据源,但我们不想保存此数据,也不想为其创建表.该文档说明了有关自定义的内容,但我无法使其正常工作:
- 用户必须提供一个
DataSource
作为上下文中的 bean,或者在配置类本身中实现 BatchConfigurer
,例如:
public class AppConfig extends DefaultBatchConfigurer {
在我们的旧版本中,我们已经能够使用 MapJobRepositoryFactoryBean
类,因此它将所有数据保存在内存中.无论如何要使用完整的 JavaConfig 方式而不定义 DataSource
?我一直无法让它工作.
即使我定义了两个数据源(一个从未使用过的内存中的 HSQL)和我们真正的 Oracle 数据源,它也不起作用,因为它找到了两个数据源而不是一个.
有谁知道如何让这个工作?或者是返回以 XML 方式进行配置的唯一解决方案?
假设没有其他工件需要 DataSource,您可以使用 java config 创建一个没有 DataSource 的上下文.为此,您的配置需要像您指出的那样扩展 DefaultBatchConfigurer.在那里,您将覆盖两个方法,createJobRepository() 和 setDataSource().下面是一个示例上下文(它没有定义作业或步骤,但它正确引导所有相关的 bean).
@Configuration@EnableBatchProcessing公共静态类 BatchConfiguration 扩展 DefaultBatchConfigurer {@覆盖受保护的 JobRepository createJobRepository() 抛出异常 {MapJobRepositoryFactoryBean 工厂 =新的 MapJobRepositoryFactoryBean();factory.afterPropertiesSet();返回 (JobRepository) factory.getObject();}@覆盖@自动连线public void setDataSource(DataSource dataSource) {如果(数据源!= null){super.setDataSource(dataSource);}}@豆角,扁豆公共数据源数据源(){返回空;}}
我确实认为简化这将是一个有用的功能,并已将其添加到 Jira.您可以在此处跟踪它的进度:https://jira.springsource.org/browse/BATCH-2048>
I'm trying to get Spring Batch 2.2 working with JavaConfig.
Nowadays they have a @EnableBatchProcessing
annotation that sets up a lot of things.
Default that annotation uses a datasource for its job data, but we don't want to save this data and don't want to create the table for it. The documentation says something about customizing but I have not been able to get it working:
- The user has to provide a
DataSource
as a bean in the context, or else implementBatchConfigurer
in the configuration class itself, e.g.:
public class AppConfig extends DefaultBatchConfigurer {
In our older version we've been able to use MapJobRepositoryFactoryBean
class so it keeps all its data in memory. Is there anyway to use the full JavaConfig way and not define a DataSource
? I've not been able to get it working.
Even if I define two data sources (one HSQL in-memory that never gets used) and our real Oracle datasource it does not work because it finds two data sources instead of one.
Anyone have an idea how to get this working? Or is the only solution going back to configuring this in the XML way?
Assuming that no other artifacts require a DataSource, you can use java config to create a context without a DataSource. To do that, your configuration will need to extend DefaultBatchConfigurer as you point out. In there, you'll override two methods, createJobRepository() and setDataSource(). Below is an example context (it doesn't define a job or steps, but it bootstraps all the related beans correctly).
@Configuration
@EnableBatchProcessing
public static class BatchConfiguration extends DefaultBatchConfigurer {
@Override
protected JobRepository createJobRepository() throws Exception {
MapJobRepositoryFactoryBean factory =
new MapJobRepositoryFactoryBean();
factory.afterPropertiesSet();
return (JobRepository) factory.getObject();
}
@Override
@Autowired
public void setDataSource(DataSource dataSource) {
if(dataSource != null) {
super.setDataSource(dataSource);
}
}
@Bean
public DataSource dataSource() {
return null;
}
}
I do think that simplifying this would be a useful feature and have added it to Jira. You can track it's progress here: https://jira.springsource.org/browse/BATCH-2048
这篇关于Spring 批处理 2.2 JavaConfig的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!