ListMergeDirective

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

Enabling ListMergeDirective

ListMergeDirective is enabled by default thanks to a declaration in Platform.

The following code snippet shows the configuration provided by SAP Commerce:
<bean id="lazyInitOverrideBeanFactoryPostProcessor" class="de.hybris.platform.spring.LazyInitOverrideBeanFactoryPostProcessor" > ... </bean>
				<bean id="listMergeBeanPostProcessor" class="de.hybris.platform.spring.config.ListMergeDirectiveBeanPostProcessor"/>
				<bean id="listMergeDirective" class="de.hybris.platform.spring.config.ListMergeDirective" abstract="true"/>

In a custom web context, add the following beans to enable listMergeDirective:

<bean id="webLazyInitOverrideBeanFactoryPostProcessor" parent="lazyInitOverrideBeanFactoryPostProcessor" />
					<bean id="listMergeBeanPostProcessor" class="de.hybris.platform.spring.config.ListMergeDirectiveBeanPostProcessor"/>

You don't have to redefine the listMergeDirective bean because it is visible and accessible by all web contexts in Platform. It is important to use the same bean id because if an addon also declared BeanPostProcessor with a different name, then given listMergeDirective would be applied twice.

Using listMergeDirective

Minimum Requirements

The minimum declaration of listMergeDirective looks as follows:
<bean depends-on="targetListBean" parent="listMergeDirective">
						<property name="add" ref="myBeanToInsert" />
						</bean>

It adds myBeanToInsert into a Spring list named targetListBean. The depends-on attribute should contain the name of the bean that should be modified. The parent attribute should always be set to listMergeDirective. The add property can contain the bean to be inserted into a given list, or just a plain value.

Additional Options

listMergeDirective has a few other properties that affect how and where you can add a given item.

The following item positioning conditions define where in a target list an item should be added:
  • afterClasses defines a list of classes after which an item is inserted

  • beforeClasses defines a list of classes before which an item is inserted

  • afterBeanNames defines a list of names of beans in a list after which an item is inserted

  • beforeBeanNames defines a list of names of beans in a list before which an item is inserted

  • afterValues defines a list of values in a list after which an item is inserted

  • beforeValues defines a list of values in a list before which an item is inserted

To define a target list, use these properties:
  • listPropertyDescriptor 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 which will be modified

Defining Item Position

By default, an item is added at the end of a list. When defining a position of an item in a target list, it is possible to combine multiple positioning conditions such as beforeClasses and afterBeanNames. In such a case, remember that it is not possible to specify a precise position. It may also be not possible for algorithm to fulfill item positioning conditions. Then an item is inserted in the best fitting position in a list.

Here are a few examples of defining a position of an item in a target list:

Example 1:

<!-- Adds value 'bbb' into stringList after value 'aaa' -->
					<bean depends-on="stringList" parent="listMergeDirective">
					<property name="add" value="bbb" />
					<property name="afterValues">
					<list value-type="java.lang.String">
					<value>aaa</value>
					</list>
					</property>
					</bean>

Example 2:

<!-- Adds value 'zzz' into stringList after value 'bbb' but before value 'ccc' -->
					<bean depends-on="stringList" parent="listMergeDirective">
					<property name="add" value="zzz" />
					<property name="beforeValues">
					<list value-type="java.lang.String">
					<value>ccc</value>
					</list>
					</property>
					<property name="afterValues">
					<list value-type="java.lang.String">
					<value>bbb</value>
					</list>
					</property>
					</bean>

Example 3:

<!-- Adds Character 'd' into objectList before class 'java.lang.Integer' -->
					<bean depends-on="objectList" parent="listMergeDirective">
					<property name="add">
					<value type="java.lang.Character">d</value>
					</property>
					<property name="beforeClasses">
					<list value-type="java.lang.Class">
					<value>java.lang.Integer</value>
					</list>
					</property>
					</bean>

Example 4:

<!-- Adds Long '2' into objectList before items with classes Integer and BigDecimal but after items with class String -->
					<bean depends-on="objectList" parent="listMergeDirective">
					<property name="add">
					<value type="java.lang.Long">2</value>
					</property>
					<property name="beforeClasses">
					<list value-type="java.lang.Class">
					<value>java.lang.Integer</value>
					<value>java.math.BigDecimal</value>
					</list>
					</property>
					<property name="afterClasses">
					<list value-type="java.lang.Class">
					<value>java.lang.String</value>
					</list>
					</property>
					</bean>

Example 5:

<!-- Adds bean 'decimal2' into decimalList before bean with name 'decimal3' but after items with class 'java.lang.String'-->
					<bean depends-on="decimalList" parent="listMergeDirective">
					<property name="add" ref="decimal2" />
					<property name="beforeBeanNames">
					<list>
					<value>decimal3</value>
					</list>
					</property>
					<property name="afterClasses">
					<list value-type="java.lang.Class">
					<value>java.lang.String</value>
					</list>
					</property>
					</bean>

Example 6:

<!-- Adds bean 'decimal1' into decimalList after beans with name 'decimal0' and 'decimal7' and after all items with class 'java.lang.String' -->
					<bean depends-on="decimalList" parent="listMergeDirective">
					<property name="add" ref="decimal1" />
					<property name="afterBeanNames">
					<list>
					<value>decimal0</value>
					<value>decimal7</value>
					</list>
					</property>
					<property name="afterClasses">
					<list value-type="java.lang.Class">
					<value>java.lang.String</value>
					</list>
					</property>
					</bean>

Defining Target Location

By default, a target list id is defined by the depends-on attribute of a bean. In case a list which should be modified is not defined as a Spring list bean (util:list) we can use the listPropertyDescriptor or fieldName properties.

listPropertyDescriptor is a property descriptor defining which property in a target bean includes the list we want to modify. It is possible to use an indexed and/or nested name of the property to be extracted.

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

See examples:

Example 1:

<!-- Adds value 'xxx' into objectList property of mergeFieldTestBean bean -->
					<bean depends-on="mergeFieldTestBean" parent="listMergeDirective">
					<property name="listPropertyDescriptor" value="objectList" />
					<property name="add" value="xxx" />
					</bean>

Example 2:

<!-- Adds value 'xxx' into objectList field of mergeFieldTestBean bean -->
					<bean depends-on="mergeFieldTestBean" parent="listMergeDirective">
					<property name="fieldName" value="objectList" />
					<property name="add" value="xxx" />
					</bean>

How It Works

The de.hybris.platform.spring.config.ListMergeDirectiveBeanPostProcessor class is the heart of the listMergeDirective mechanism. ListMergeDirectiveBeanPostProcessor is a BeanPostProcessor class that during creation of beans reacts to beans of the de.hybris.platform.spring.config.ListMergeDirective type, and applies modifications stored in them.

listMergeDirective beans are only data structures that hold information about where and what needs to be added. They contain no logic at all.

Because ListMergeDirectiveBeanPostProcessor reacts only to created beans, it is important to have listMergeDirective beans eagerly loaded. This is done using LazyInitOverrideBeanFactoryPostProcessor that ensures that important configuration beans are loaded eagerly.