Question

I want to pass the input resource location as a String to a field of the domain object.
My configuration looks like this:

<bean id="step2Reader"
        class="org.springframework.batch.item.file.MultiResourceItemReader">
        <property name="resources" value="file:${step2.reader.resource}/*/*/*.xml"></property>
        <property name="delegate" ref="mainReader"></property>
    </bean>

<bean id="mainReader" class="org.springframework.batch.item.xml.StaxEventItemReader"
        scope="step">
        <property name="fragmentRootElementName" value="Domain" />
        <property name="unmarshaller" ref="domainMarshaller" />
    </bean>

    <bean id="domainMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="classesToBeBound">
            <list>
                <value>com.example.Domain</value>
            </list>
        </property>
    </bean>

and Domain.java is a JAXB generated class containing a field like this:

 @XmlElement(name = "PATH_TO_DOCUMENT", required = true)
 private String pathtodocument;

which is supposed to be filled by the input resource as a string.

I've thought of either extending StaxEventItemReader to include this functionality or somehow make visible the resource to the Processor of the domain and fill the value of field there, but got stuck.

Any suggestions?

Was it helpful?

Solution

Let your com.example.Domain object implements ResourceAware so reader will automagically injects current resource into Domain object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top