MapMergeDirective

MapMergeDirective is special kind of spring bean whose only purpose is to modify a map in another spring bean. This allows SAP Commerce extensions to merge additional elements into Spring bean maps, and map properties on Spring beans.

Enabling mapMergeDirective

mapMergeDirective is enabled by default thanks to a declaration in Platform:
<bean id="lazyInitOverrideBeanFactoryPostProcessor" class="de.hybris.platform.spring.LazyInitOverrideBeanFactoryPostProcessor" > ... </bean>
				<bean id="mapMergeBeanPostProcessor" class="de.hybris.platform.spring.config.MapMergeDirectiveBeanPostProcessor"/>
				<bean id="mapMergeDirective" class="de.hybris.platform.spring.config.MapMergeDirective" abstract="true"/>
To enable mapMergeDirective, add the following beans in a custom web context :
<bean id="mapMergeBeanPostProcessor" class="de.hybris.platform.spring.config.MapMergeDirectiveBeanPostProcessor"/>
					<bean id="webLazyInitOverrideBeanFactoryPostProcessor" parent="lazyInitOverrideBeanFactoryPostProcessor" />

You don't have to redefine the mapMergeDirective bean because it is visible and accessible by all web contexts in Platform.

Using mapMergeDirective

Minimum Requirements

The minimum declaration of mapMergeDirective looks as follows:
<bean depends-on="targetMapBean" parent="mapMergeDirective">
						<property name="key" value="keyValue"/>
						<property name="value" value="valueValue"/>
						</bean>

It adds valueValue under the keyValue key into a Spring list named targetMapBean. The depends-on attribute should contain the name of a bean that should be modified. The parent attribute should always be set to mapMergeDirective. The key property identifies the key in a map that should be modified. The value property defines a value which should be added to map.

It is worth mentioning that mapMergeDirective supports multi-value maps. If a target map has a value under the key property that is of a collection type, the value is added to that collection. If an existing value and an inserted value are both a collection, then all values are added. If a given key exists but its value is not a collection, then the value is overwritten. If a given key doesn't exist, it gets added.

Additional Options

mapMergeDirective provides you with a few options. They enable you to define a source map that will be merged with a target map. Using the following properties, you can define a target map that you want to modify:
  • mapPropertyDescriptor is a property descriptor that defines a list property in a target bean that will be modified

  • fieldName is a field name that contains a list in a target bean that will be modified

Defining Target Location

By default, a target map id is defined by the depends-on attribute of a bean. If a map you want to modify is not defined as a Spring list bean, you can use the mapPropertyDescriptor or the fieldName properties.

mapPropertyDescriptor is a property descriptor that defines which property in a target bean is the map we want to modify. It is possible to use an indexed and/or nested name of a property to be modified .

fieldName is much more limiting than mapPropertyDescriptor because it can't access nested/indexed properties. It can, however, access private fields in a target bean and no getters/setters are needed.

See examples:

Example 1:

<!-- Adds value '444' under key 'bbb' into stringMap property of mergeFieldTestBean bean -->
						<bean depends-on="mergeFieldTestBean" parent="mapMergeDirective">
						<property name="mapPropertyDescriptor" value="stringMap"/>
						<property name="key" value="bbb"/>
						<property name="value" value="444"/>
						</bean>

Example 2:

<!-- Adds value '444' under key 'bbb' into stringMap field of mergeFieldTestBean bean -->
						<bean depends-on="mergeFieldTestBean" parent="mapMergeDirective">
						<property name="fieldName" value="stringMap"/>
						<property name="key" value="bbb"/>
						<property name="value" value="444"/>
						</bean>

Defining a Source Map

The sourceMap property allows mapMergeDirective to merge two maps. If a target map is a multi-value map, the values are not merged but overwritten:
<!-- merges key/values from stringMap into mergeFieldTestBean map -->
							<bean depends-on="mergeFieldTestBean" parent="mapMergeDirective">
							<property name="sourceMap" ref="stringMap"/>
							</bean>

How It Works

The de.hybris.platform.spring.config.MapMergeDirectiveBeanPostProcessor class is the heart of the mapMergeDirective mechanism. MapMergeDirectiveBeanPostProcessor is a BeanPostProcessor class that during creation of beans reacts to beans of the de.hybris.platform.spring.config.MapMergeDirective type, and applies modifications stored in them. mapMergeDirective beans are only data structures that hold information about where and what needs to be added. They contain no logic at all. Because MapMergeDirectiveBeanPostProcessor reacts only to created beans, it is important to have mapMergeDirective beans eagerly loaded. This is done using LazyInitOverrideBeanFactoryPostProcessor that ensures that important configuration beans are loaded eagerly.