Uploaded image for project: 'Spring Batch'
  1. Spring Batch
  2. BATCH-1905

Cannot autowire a delegate ItemWriter defined with step scope

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Blocker
    • Resolution: Works as Designed
    • Affects Version/s: 2.1.8
    • Fix Version/s: None
    • Component/s: Core
    • Environment:
      Windows 7
      Spring Tool Suite

      Version: 3.1.0.RELEASE
      Build Id: 201210061306

      Description

      I have a problem when I try to @Autowired a bean of type FlatFileItemWriter. I need to calculate a directory name in execution time and assign this value to the "resource" property using late-binding. There's no problem to achive this, but, when my batch runs, I receive an exception when Spring context tries to @Autowired this bean inside my ItemProcessor (I need to use it directly in the ItemProcessor for functional design)

      The exception is: There are many candidates of type org.springframework...FlatFileItemWriter.

      If I use the @Qualifier then the error refers to another bean that has no sense with that error.

      My ItemWriter works as "delegate". I couldn't write items during the process because I need to process all the items first to do some operations.

      Why do an ItemWriter cannot be autowired if this bean is defined with late-binding?

      ==========================================================================
      ==========================================================================
      ==========================================================================

      The code is:

      LISTENER TO BUILD THE DIRECTORY NAME:

      @BeforeJob
      void beforeJob(JobExecution jobExecution) {
      String dirTimestamp = BatchUtil.getInstance().getFormattedDate(new Date(), BatchConstants.DATE_FORMAT6);
      ExecutionContext executionContext = jobExecution.getExecutionContext();
      executionContext.putString(batchConfig.nombreDirTi mestamp, dirTimestamp + "/");
      }

      THE XML CONFIG OF THE JOB:

      <job id="job1" xmlns="http://www.springframework.org/schema/batch" incrementer="incrementer">

      <!-- Main step -->
      <step id="step1" ..../>

      <!-- Listener for the job -->
      <listeners>
      <listener ref="commonJobListener" />
      </listeners>
      </job>

      <!-- ItemWriter -->
      <bean id="itemWriter1" class="org.springframework.batch.item.file.FlatFil eItemWriter" scope="step">
      <property name="resource" value="file:#

      {jobExecutionContext['timestampDir']}

      ${output.file}" />

      ......................

      </bean>

      THE ITEM PROCESS CODE:

      @Autowired
      FlatFileItemWriter<FicheroOperacionesFilaSalida> itemWriter1;

      If I don't use the late-binding in the bean, there's no problem to autowired it.

        Attachments

          Activity

            People

            Assignee:
            mminella Michael Minella
            Reporter:
            maxjuiz MARTIN JUIZ
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Time Tracking

                Estimated:
                Original Estimate - 10d
                10d
                Remaining:
                Remaining Estimate - 10d
                10d
                Logged:
                Time Spent - Not Specified
                Not Specified