Reader(StaxEventItemReader) Resource to Domain object

浪子不回头ぞ 提交于 2019-12-02 13:23:33

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/23359814/readerstaxeventitemreader-resource-to-domain-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!