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

  • Problem autowiring ItemWriter defined with scope="step"

    Hi,

    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.




    Thanks,

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

    Comment


    • #3
      Hi mminella,

      I am autowiring an ItemWriter NOT an ItemReader.


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

      Comment


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

        Comment


        • #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?



          Thanks,

          Comment


          • #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?

            Comment


            • #7
              The code is:

              LISTENER TO BUILD THE DIRECTORY NAME:


              Code:
              @BeforeJob
                  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:

              Code:
                  <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.FlatFileItemWriter" scope="step">
              <property name="resource" value="file:#{jobExecutionContext['timestampDir']}${output.file}" />
              
                     ......................
              
              </bean>

              THE ITEM PROCESS CODE:

              Code:
              @Autowired
              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.

              Comment


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

                Comment


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

                  Comment


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

                    Comment


                    • #11
                      Hi Charles,

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

                      Thanks in advance !!!

                      Regards,

                      Comment

                      Working...
                      X