title: How to set the resource of MultiResourceItemWriter dynamically from ItemReader in Spring Batch
body: <p>The batch job is:</p>
  
  <ul>
  <li><p>Reading from a csv file</p></li>
  <li><p>Create an xml file for every record(line) in the csv with name Patent.ID.xml(where ID is a field in the csv and Patent is my model class), example: 1.xml, 2.xml</p></li>
  </ul>
  
  <p>The problem is that I can't find a way to dynamically set the file-name to each ID from the csv file</p>
  
  <p>Here is my configuration:</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:batch="http://www.springframework.org/schema/batch"xmlns:task="http://www.springframework.org/schema/task"
      xmlns:util="http://www.springframework.org/schema/util"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/batch
          http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
          http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
          http://www.springframework.org/schema/util 
          http://www.springframework.org/schema/util/spring-util-3.2.xsd"&gt;
  
  
      &lt;!-- My beans --&gt;   
      &lt;bean id="patent" class="com.example.model.Patent" scope="prototype" /&gt;
      &lt;bean id="xmlsuffix" class="com.example.filename.PatentFileSuffixCreator"/&gt;
      &lt;bean id="noInputException"     class="com.example.listener.NoWorkFoundStepExecutionListener"/&gt;
  
      &lt;batch:job id="csvtoxml"&gt;
          &lt;batch:step id="step1"&gt;
              &lt;batch:tasklet&gt;             
                  &lt;batch:chunk reader="fileReader"     writer="multiResourceItemWriter" commit-interval="1"&gt;
                  &lt;/batch:chunk&gt;
                  &lt;batch:listeners&gt;
                      &lt;batch:listener ref="noInputException"/&gt;
                  &lt;/batch:listeners&gt;  
              &lt;/batch:tasklet&gt;
          &lt;/batch:step&gt;
      &lt;/batch:job&gt;
  
      &lt;bean id="multiResourceItemWriter"
      class="org.springframework.batch.item.file.MultiResourceItemWriter"&gt;
          &lt;property name="resource" value="file:xml/#{patent.ID}" /&gt;
          &lt;property name="delegate" ref="XMLwriter"/&gt;
          &lt;property name="itemCountLimitPerResource" value="1"/&gt;
          &lt;property name="resourceSuffixCreator" ref="xmlsuffix"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="XMLwriter" 
                  class="org.springframework.batch.item.xml.StaxEventItemWriter"&gt;
          &lt;property name="marshaller" ref="patentUnmarshaller" /&gt;
          &lt;property name="rootTagName" value="Patents" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="patentUnmarshaller" 
       class="org.springframework.oxm.xstream.XStreamMarshaller"&gt;
          &lt;property name="aliases"&gt;
              &lt;map&gt;
                  &lt;entry key="Patent"
                  value="com.example.model.Patent" /&gt;
              &lt;/map&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
    &lt;bean id="fileReader"
          class="org.springframework.batch.item.file.FlatFileItemReader" scope="step"&gt;
          &lt;property name="lineMapper" ref="lineMapper"/&gt;
          &lt;property name="resource" value="file:dbbrev_sample_m.csv"/&gt;
          &lt;property name="linesToSkip" value="1"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="lineMapper"
          class="org.springframework.batch.item.file.mapping.DefaultLineMapper"&gt;
          &lt;property name="fieldSetMapper" ref="fieldSetMapper"/&gt;
          &lt;property name="lineTokenizer" ref="lineTokenizer"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="lineTokenizer"
          class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"&gt;
          &lt;property name="delimiter" value=","/&gt;
          &lt;property name="names" value="ID,TYPE,NOPUBLICATION" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="fieldSetMapper"
          class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper"&gt;
          &lt;property name="targetType" value="com.example.model.Patent"/&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>