重用bean的XML片段

时间:2019-01-10 22:35:45

标签: java spring

我有一个xml文件可以满足我的需要,但是它有很多重复的代码。例如-用户名和密码是xml文件中每个bean的一部分。我使用parentBean id创建了下面的文件,希望可以重用子bean中的所有内容。但是我遇到The content of element type "bean" must match错误。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
   <bean id="parentBean" class="com.dataloader.ProcessRunner" abstract="true">
   <description>Parent bean.</description>
      <map>
         <entry key="sfdc.debugMessages" value="true" />
         <entry key="sfdc.debugMessagesFile" value="C:\DLTest\accountExportSoapTrace.log" />
         <entry key="sfdc.endpoint" value="https://test.com" />
         <entry key="sfdc.username" value="test" />
         <entry key="sfdc.password" value="5b69d70a1dd974a21ecf64595ac02596f0c7c" />
         <entry key="process.encryptionKeyFile" value="D:\SF\dataLoader.key" />
         <entry key="sfdc.timeoutSecs" value="600" />
      </map>
   </bean>
   <bean id="account" singleton="false" parent="parentBean">
      <description>accountInsert exports all fields from the Account object.</description>
      <property name="name" value="account" />
      <property name="configOverrideMap">
         <map>
            <entry key="sfdc.entity" value="Account" />
            <entry key="process.operation" value="extract" />
            <entry key="sfdc.extractionSOQL" value="SELECT ID, Name, Phone FROM Account" />
            <entry key="process.mappingFile" value="D:\SF\accountExport.sdl" />
            <entry key="dataAccess.name" value="D:\SF\Account.csv" />
            <!--entry key="process.outputSuccess" 
                    value="c:\DLTest\accountExport_success.csv"/>
                <entry key="process.outputError" 
                    value="c:\DLTest\Log\accountExport_error.csv"/-->
            <entry key="dataAccess.type" value="csvWrite" />
            <entry key="process.initialLastRunDate" value="2018-10-1T00:00:00.000-0800" />
         </map>
      </property>
   </bean>
   <bean id="contact" singleton="false" parent="parentBean">
      <description>contactExport exports all fields from the Contact object.</description>
      <property name="name" value="contact" />
      <property name="configOverrideMap">
         <map>
            <!--entry key="sfdc.loadBatchSize" value="200"/-->
            <entry key="sfdc.entity" value="Contact" />
            <entry key="process.operation" value="extract" />
            <entry key="sfdc.extractionSOQL" value="SELECT ID, FirstName, LastName FROM Contact" />
            <entry key="process.mappingFile" value="D:\SF\contactExport.sdl" />
            <entry key="dataAccess.name" value="D:\SF\Contacts.csv" />
            <entry key="dataAccess.type" value="csvWrite" />
            <entry key="process.initialLastRunDate" value="2018-10-1T00:00:00.000-0800" />
         </map>
      </property>
   </bean>
</beans>

1 个答案:

答案 0 :(得分:0)

您需要在父bean上指定属性名称。 要合并父级和子级bean的映射,请在beans元素上设置属性default-merge="true"

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
    default-merge="true">
   <bean id="parentBean" class="com.dataloader.ProcessRunner" abstract="true">
   <description>Parent bean.</description>
     <property name="configOverrideMap">
       <map>
         <entry key="sfdc.debugMessages" value="true" />
         <entry key="sfdc.debugMessagesFile" value="C:\DLTest\accountExportSoapTrace.log" />
         <entry key="sfdc.endpoint" value="https://test.com" />
         <entry key="sfdc.username" value="test" />
         <entry key="sfdc.password" value="5b69d70a1dd974a21ecf64595ac02596f0c7c" />
         <entry key="process.encryptionKeyFile" value="D:\SF\dataLoader.key" />
         <entry key="sfdc.timeoutSecs" value="600" />
       </map>
     </property>
   </bean>
   ... rest of the applicationContext