Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Problem autowiring ItemWriter defined with scope="step"

  1. #1
    Join Date
    Oct 2012
    Location
    Madrid
    Posts
    20

    Question 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. #2
    Join Date
    Sep 2008
    Location
    Chicagoland, IL
    Posts
    366

    Default

    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.
    Michael Minella
    Spring Batch Lead
    Author - Pro Spring Batch
    http://www.michaelminella.com
    Twitter: @MichaelMinella

  3. #3
    Join Date
    Oct 2012
    Location
    Madrid
    Posts
    20

    Default

    Hi mminella,

    I am autowiring an ItemWriter NOT an ItemReader.


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

  4. #4
    Join Date
    Sep 2008
    Location
    Chicagoland, IL
    Posts
    366

    Default

    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.
    Michael Minella
    Spring Batch Lead
    Author - Pro Spring Batch
    http://www.michaelminella.com
    Twitter: @MichaelMinella

  5. #5
    Join Date
    Oct 2012
    Location
    Madrid
    Posts
    20

    Default

    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,

  6. #6
    Join Date
    Sep 2008
    Location
    Chicagoland, IL
    Posts
    366

    Default

    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?
    Michael Minella
    Spring Batch Lead
    Author - Pro Spring Batch
    http://www.michaelminella.com
    Twitter: @MichaelMinella

  7. #7
    Join Date
    Oct 2012
    Location
    Madrid
    Posts
    20

    Default

    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 at 04:01 AM.

  8. #8
    Join Date
    Aug 2011
    Posts
    22

    Default

    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. #9
    Join Date
    Oct 2012
    Location
    Madrid
    Posts
    20

    Default

    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. #10
    Join Date
    Jan 2013
    Location
    Sydney
    Posts
    1

    Default

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •