1

We have xml file with bean definition. As this is shared XML file between different modules, We can't change this xml, so as per project need, we decided to update bean definition.

<bean id="columnMap"
      class="org.springframework.beans.factory.config.MapFactoryBean">
    <property name="sourceMap">
        <map key-type="java.lang.String" value-type="java.lang.String">
            <entry key="Item Id" value="ITEM_ID"/>
        </map>
    </property>
</bean>

Now, need to update this bean in another xml and wanted to add new map entry.

Didier L
  • 18,905
  • 10
  • 61
  • 103
user2758176
  • 193
  • 1
  • 7
  • 20

1 Answers1

1

There are two strategies you can adopt and some really good examples are provided in this stack link (How can I inject a property value into a Spring Bean which was configured using annotations?) .
Look at the answer from @Umut Utkan which describes the solution well.

Strategy 1: Add a method invoking factory bean and use that factory to add values to an existing map instead of erroring/overriding. However since you cant change the bean definition , i am not going in detail into this (The link has more info on how to achieve it)

Strategy 2 : Very simple, set the merge flag of the collection to true as below.

<bean id="columnMap" class="org.springframework.beans.factory.config.MapFactoryBean">
    <property name="sourceMap">
        <util:map>
            <entry key="1" value="1"/>
        </util:map>
    </property>
</bean>
<bean id="specificColumnMap" class="org.springframework.beans.factory.config.MapFactoryBean" parent="columnMap">
    <property name="sourceMap">
        <util:map merge="true">
            <entry key="2" value="2"/>
        </util:map>
    </property>
</bean>

PS : I dont know if this falls in the category of a duplicate question, so i am going to post the answer and let the moderators take a call on that.

Arpan Kanthal
  • 475
  • 2
  • 7