MultiResourceItemReader来自另一个类的资源

MultiResourceItemReader来自另一个类的资源

本文介绍了Spring Batch MultiResourceItemReader来自另一个类的资源参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了从JobParameters或使用Value标记获取值之外,是否可以自定义MultiResourceItemReader资源?我尝试了以下方法,但是没有用.

Is there a way to customize the MultiResourceItemReader Resources other than getting the value from JobParameters or using the Value tag? I tried the following but it did not work.

<bean id="ItemReader" class="org.springframework.batch.item.file.MultiResourceItemReader" scope="step">
      <property name="resources" ref="fileResources" />
      <property name="delegate" ref="flatFileItemReader" />
</bean>

<bean id="fileResources" class="com.app.batch.fileloader.file.FileResources" />

<bean id="flatFileItemReader" class="com.app.batch.fileloader.file.MyFlatFileItemReader">
    <property name="lineMapper">
        <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
            <property name="lineTokenizer">
                <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                    <property name="delimiter" value="," />
                </bean>
            </property>
            <property name="fieldSetMapper">
                <bean class="com.app.batch.fileloader.file.MyFileFieldSetMapper" />
            </property>
        </bean>
    </property>
</bean>

在FileResources Java类中,我像这样扩展了MultiResourceItemReader

In the FileResources Java class I extended the MultiResourceItemReader like this

public class FileResources extends MultiResourceItemReader<FileDTO>{

@Override
public void setResources(Resource[] resources){
    ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
    try{
        resources = patternResolver.getResources("file:" + properties.getPath() + "/*.csv");
    }
    catch(IOException ex){
        LOG.error("The resources must not be null");
    }

    super.setResources(resources);
}

我在做什么错,有人可以让我知道.谢谢!

What am I doing wrong can someone please let me know. Thanks!

推荐答案

使用FactoryBean.创建一个返回Resource []的实现,并将其注入到您的MultiResourceItemReader中. Spring将调用工厂bean并使用输出来填充依赖项.

Use a FactoryBean. Create an implementation that returns a Resource [] and inject that into your MultiResourceItemReader. Spring will call the factory bean and use the output to populate the dependency.

一个例子看起来像这样:

An example would look something like this:

<bean id="ItemReader" class="org.springframework.batch.item.file.MultiResourceItemReader" scope="step">
      <property name="resources" ref="fileResources" />
      <property name="delegate" ref="flatFileItemReader" />
</bean>

<bean id="fileResources" class="com.app.batch.fileloader.file.ResourcesFactoryBean" />

使用上面的配置,您将使用下面的FactoryBean实现:

With the above configuration, you'd use the below FactoryBean implementation:

public class ResourcesFactoryBean implements FactoryBean<Resource[]>{

    @Override
    public Resource[] getObject() {
        ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
        try{
            resources = patternResolver.getResources("file:" + properties.getPath() + "/*.csv");
        }
        catch(IOException ex){
            LOG.error("The resources must not be null");
        }

        return resources;
    }
   ...

您可以在此处的文档中了解有关Spring的FactoryBean接口的更多信息: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/FactoryBean.html

You can read more about Spring's FactoryBean interface in the documentation here: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/FactoryBean.html

在这里: https://docs.spring.io/spring/docs/5.0.3.RELEASE/spring-framework-reference/core.html#beans-factory-extension-factorybean

这篇关于Spring Batch MultiResourceItemReader来自另一个类的资源参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:07