简体   繁体   中英

java.lang.IllegalStateException when i configure FlatFileItemWriter as stream with scpoe=“step” in spring batch

I am using spring batch and am trying to configure a FlatFileItemWriter as a stream to log all the records that cause exception into another flat file, I need to configure the scope="step" so that I can use place holders for configurations.

When i put scope="step" I get an exception, however when I remove it, the exception goes away.

Here is the configuration for job:

<batch:job id="helloWorldJob">
    <batch:step id="step1">
        <batch:tasklet>
            <batch:chunk reader="cvsFileItemReader" writer="cvsFileItemWriter"
                processor="itemProcessor" commit-interval="10" skip-limit="9999">
                <batch:skippable-exception-classes>
                    <batch:include class="java.lang.Exception" />
                    <batch:exclude class="java.io.FileNotFoundException" />
                </batch:skippable-exception-classes>
                <batch:streams>
                    <batch:stream ref="rejectFileItemWriter" />
                </batch:streams>
            </batch:chunk>
            <batch:listeners>
                <batch:listener ref="skipListner" />
            </batch:listeners>
        </batch:tasklet>
    </batch:step>
</batch:job>

Here is the configuration for the skip listener with property writer:

<bean id="skipListner" class="com.mykong.skipListner.RecordSkipListener">
    <property name="writer" ref="rejectFileItemWriter"></property>
</bean>

Here is my configuration for the writer:

<bean id="rejectFileItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">

    <property name="resource" value="#{jobParameters['REJECTFILE']}" />

    <property name="lineAggregator">
        <bean
            class="org.springframework.batch.item.file.transform.PassThroughLineAggregator">
        </bean>
    </property>

</bean>

And here is the exception that I get:

java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy2 implementing org.springframework.batch.item.file.ResourceAwareItemWriterItemStream,org.springframework.beans.factory.InitializingBean,org.springframework.batch.item.ItemWriter,org.springframework.batch.item.ItemStream,org.springframework.aop.scope.ScopedObject,java.io.Serializable,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [org.springframework.batch.item.file.FlatFileItemWriter] for property 'writer': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:264)
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:450)
    ... 17 more

If you set scope=step on a component, spring will only create a proxy for that component. When Spring uses that proxy, it sees only the "Interfaces" of the original class, not the class itself. So, if it tries to inject this proxy into another component, the defined property needs to have a matching interface type. It will refuse to inject it somewhere, where you have property with a concrete class as defined type.

Looking at your exception, you see that the scoped-proxy of your FlatFileItemReader implements the interfaces

org.springframework.batch.item.file.ResourceAwareItemWriterItemStream
org.springframework.beans.factory.InitializingBean
org.springframework.batch.item.ItemWriter
org.springframework.batch.item.ItemStream
org.springframework.aop.scope.ScopedObject java.io.Serializable
org.springframework.aop.framework.AopInfrastructureBean
org.springframework.aop.SpringProxy
org.springframework.aop.framework.Advised

and it seems, that there is somewhere a property named "writer" in a bean, which is defined as FlatFileItemReader where this proxy should be injected.

However, I don't see that in the code you have provided.

Is there another Bean in which you try to inject 'rejectFileItemWriter' into a property named "writer" with type "FlatFileItemReader"? If yes, changing this type to the interface 'ResourceAwareItemWriterItemStream' could do the trick. From your code, I assume, that you use this writer in your processor to log the "rejected" files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM