问题
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>
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!
回答1:
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" />
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;
}
...
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
And here: https://docs.spring.io/spring/docs/5.0.3.RELEASE/spring-framework-reference/core.html#beans-factory-extension-factorybean
来源:https://stackoverflow.com/questions/48407408/spring-batch-multiresourceitemreader-resources-reference-from-another-class