除了从JobParameters获取值或使用Value标记之外,有没有办法自定义MultiResourceItemReader资源?我尝试了以下但是没有用。
<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
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);
}
我做错了可以有人请让我知道。谢谢!
答案 0 :(得分:1)
使用FactoryBean
。创建一个返回Resource []
的实现,并将其注入MultiResourceItemReader
。 Spring将调用工厂bean并使用输出来填充依赖项。
一个例子看起来像这样:
<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
实施:
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