Announcement Announcement Module
No announcement yet.
Problem autowiring ItemWriter defined with scope="step" Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem autowiring ItemWriter defined with scope="step"


    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.


  • #2
    Crazy question...why are you trying to wire an ItemReader into an ItemProcessor? Why not use just a regular DAO? There is no benefits of using an ItemReader in this way.


    • #3
      Hi mminella,

      I am autowiring an ItemWriter NOT an ItemReader.

      Last edited by maxjuiz; Oct 31st, 2012, 04:22 AM.


      • #4
        Either way, my question is valid. You shouldn't be trying to wire up ItemReader/ItemWriter components into your ItemProcessor. They carry extra weight that is unused and unnecessary. Just create and inject a DAO of somekind.


        • #5
          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.

          But the main question isn't the design of the process, the main question is why do an ItemWriter cannot be autowired if this bean is defined with late-binding? Spring Batch bug?



          • #6
            The function of autowiring isn't one of batch but of spring core, so it wouldn't be a batch bug. Can you post the code you're working with?


            • #7
              The code is:


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

              THE XML CONFIG OF THE JOB:

                  <job id="job1" xmlns="" incrementer="incrementer">
                                <!-- Main step -->
                       <step id="step1" ..../>
                        <!-- Listener for the job -->
                           <listener ref="commonJobListener" />
              <!-- ItemWriter -->
                  <bean id="itemWriter1" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
              <property name="resource" value="file:#{jobExecutionContext['timestampDir']}${output.file}" />

              THE ITEM PROCESS CODE:

              FlatFileItemWriter<FicheroOperacionesFilaSalida> itemWriter1;

              If I don't use the late-binding in the bean, there's no problem to autowired it.
              Last edited by maxjuiz; Nov 9th, 2012, 04:01 AM.


              • #8
                If I use the @Qualifier then the error refers to another bean that has no sense with that error.
                Which error do you get when using @Qualifier? Because that should be the fix to resolve ambiguous autowire candidates.


                • #9
                  The error using @Qualier refers to another bean, and something like 'no bean definition found for bean <beanName>....." BUT this bean is well-defined and has no relation with the ItemWriter that is causing the problem.

                  I just open a JIRA bug for this problem because it is very weird and I don't know how to continue.


                  • #10
                    Hi maxijuiz,

                    The spring container does not instantiate the beans with Step scope until the step start. So, the bean itemWriter1 is still not exist yet when the spring container just starts up. When you used the @Autowired, the spring container will look for a bean with id that matches variable name (or use qualifier if @Qualifier is used) itemWriter1 when the spring container starts up. Since the iterWriter1 is not exist yet, it will try to look for any FlatFileItemWriter type for injection. You may have some FlatFileItemWriter (and its sub classes) defined in the spring containers already and hence you got the error "There are many candidates of type org.springframework...FlatFileItemWriter."

                    To over come your issue, without knowing what you really trying to achieve, you could also declare the ItemProcessor class with the Step Scope. Your item processor class will not be instantiated until the step starts. When the step starts, the itemWriter1 will be instatiated and spring container will able to auto wire it.


                    • #11
                      Hi Charles,

                      I just read your post and I'm gonna try it. I'll tell you the result...

                      Thanks in advance !!!