2014-04-29 56 views
1

我想將輸入資源位置作爲字符串傳遞給域對象的字段。
我的配置是這樣的:閱讀器(StaxEventItemReader)資源到域對象

<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> 

和Domain.java是含有這樣一個字段一個JAXB生成的類:

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

這是應該由輸入資源作爲一個字符串填充。

我想過要麼擴展StaxEventItemReader以包含此功能或以某種方式使域的Processor資源可見,並填充字段的值,但卡住了。

有什麼建議嗎?

回答

2

讓你的com.example.Domain對象實現ResourceAware,以便讀者自動將當前資源注入到Domain對象中。

+0

忘了提及我無法修補Domain對象。處理器能否實現此接口,並在處理時更新Domain對象中字段的值? – dimzak

+0

您可以擴展'Domain'類(如'類DomainWithResource擴展域實現ResourceAware')並修改'xml中的domainMarshaller'定義?否則你可以在編組/對象創建期間操作並創建一個實現了ResourceAware ... –

+0

或擴展step2Reader並在'read()'手動設置當前資源名稱爲Domain.pathtodocument –