JavaBuilder
JavaBuilder

Reputation: 159

How Spring manages map-merge='true' in case of same key in parent map?

I have following 2 configs :


config-1.xml

  ---- import config-2.xml -----

     <bean id="map1" class="java.util.HashMap" parent="map2">     
         <constructor-arg>                                             
             <map merge="true">                                                      
                 <entry key="key1" value="value1"/>        
                 <entry key="key2" value="value2"/>         
             </map>                                                    
         </constructor-arg>                                            
     </bean>                                                           
     

config-2.xml

 <bean id="map2" class="java.util.HashMap">     
     <constructor-arg>                                             
         <map>                                                      
             <entry key="key1" value="new_value1"/>        
             <entry key="key3" value="value3"/>         
         </map>                                                    
     </constructor-arg>                                            
 </bean>

How spring manages this merge ? I will load bean through config-1.xml ( It has config-2.xml as import). So what I want is key1=new_value1 Please note that I cant touch config-1.xml as its used by other code so I should load new value via config-2.xml which is specific to my code.

Wherever I refer map1 , it should have following for my code :

key1=new_value1
key2=value2
key3=value3

Upvotes: 0

Views: 543

Answers (1)

CryptoFool
CryptoFool

Reputation: 23089

Looking at the Spring docs for Bean Definition Inheritance, it seems to me to pretty clearly say that anything specified in both child and parent will cause the child definition to override the parent definition:

https://docs.spring.io/spring-framework/docs/3.0.0.M4/reference/html/ch03s07.html

Maybe it could be a little more clear, but what the docs say seems to confirm what you're saying and what the testing you've cone confirms. Take this example:

<bean id="inheritedTestBean" abstract="true"
    class="org.springframework.beans.TestBean">
  <property name="name" value="parent"/>
  <property name="age" value="1"/>
</bean>

<bean id="inheritsWithDifferentClass"
      class="org.springframework.beans.DerivedTestBean"
      parent="inheritedTestBean" init-method="initialize">
    
  <property name="name" value="override"/>
  <!-- the age property value of 1 will be inherited from  parent -->

</bean>

The fact that nothing is said about what value the name property will take, but its value for the example is chosen as override, seems to pretty clearly suggest that override will be the resulting value of the child's name property. Further, the fact that it is mentioned explicitly that the age property will be inherited from the parent strongly suggests that the name property will not be, meaning that it will instead be overridden by the child definition. This seems like nothing but obvious behavior to me.

Upvotes: 1

Related Questions