Modifying Lists and Maps in Spring Context
Platform provides a mechanism for manipulating lists and maps in the Spring context. It is useful in SAP extension architecture as it fully enables extensibility on all system layers. We use various custom BeanPostProcessors to provide this functionality.
Business Scenario
In the following scenario we show you how to extend data declared in one extension for multiple extensions. We declared data in Extension A. We extend it for Extension B and Extension C.
Extension A
The following code snippets show how Extension A defines the userModel and the userData classes. Extension A also defines a populator for these types, and a converter. The converter is used in multiple services in Extension A.
extensionA-items.xml:
<itemtype code="User" autocreate="true" generate="true">
<attributes>
<attribute qualifier="username" type="java.lang.String" />
<attribute qualifier="password" type="java.lang.String" />
</attributes>
</itemtype>
extensionA-bean.xml
<bean class="de.hybris.platform.data.UserData">
<property name="username" type="String"/>
<property name="password" type="String"/>
</bean>
extensionA-spring.xml
<bean id="userPopulator" class="de.hybris.platform.extensionA.populators.UserPopulator" />
<alias name="defaultUserConverter" alias="userConverter"/>
<bean id="defaultUserConverter" parent="abstractPopulatingConverter">
<property name="targetClass" value="de.hybris.platform.extensionA.model.user.UserModel" />
<property name="populators">
<list>
<ref bean="userPopulator" />
</list>
</property>
</bean>
Assume that Extension B and Extension C extend the User definition in the system in the following way:
Extension B
extensionB-items.xml
<itemtype code="User" autocreate="false" generate="false">
<attributes>
<attribute qualifier="email" type="java.lang.String" />
</attributes>
</itemtype>
extensionB-bean.xml
<bean class="de.hybris.platform.data.UserData">
<property name="email" type="String"/>
</bean>
Extension C
extensionC-items.xml
<itemtype code="User" autocreate="false" generate="false">
<attributes>
<attribute qualifier="role" type="java.lang.String" />
</attributes>
</itemtype>
extensionC-bean.xml
<bean class="de.hybris.platform.data.UserData">
<property name="role" type="String"/>
</bean>
Problem Statement
Solution
Spring Native Approach #1
A native approach would use <list merge="true"> in a Spring configuration:
extensionB-spring.xml
<bean id="userMailPopulator" class="de.hybris.platform.extensionB.populators.UserMailPopulator" />
<alias name="userMailConverter" alias="userConverter"/>
<bean id="userMailConverter" parent="defaultUserConverter" >
<property name="populators">
<list merge="true">
<ref bean="userMailPopulator"/>
</list>
</property>
</bean>
extensionC-spring.xml
<bean id="userRolePopulator" class="de.hybris.platform.extensionC.populators.UserRolePopulator" />
<alias name="userRoleConverter" alias="userConverter"/>
<bean id="userRoleConverter" parent="defaultUserConverter" >
<property name="populators">
<list merge="true">
<ref bean="userRolePopulator"/>
</list>
</property>
</bean>
Unfortunately, this solution can't work the way we want it to because it uses only one of the new converters. In addition, our populators property can contain either userPopulator and userMailPopulator, or userPopulator and userRolePopulator, but never userPopulator, userMailPopulator and userRolePopulator.
Spring Native Approach #2
If solution #1 doesn't work because both extensions explicitly override the same bean, then maybe they should use an aliased bean as a parent?
extensionB-spring.xml
<bean id="userMailPopulator" class="de.hybris.platform.extensionB.populators.UserMailPopulator" />
<alias name="userMailConverter" alias="userConverter"/>
<bean id="userMailConverter" parent="userConverter" >
<property name="populators">
<list merge="true">
<ref bean="userMailPopulator"/>
</list>
</property>
</bean>
However, this solution is not possible at all and Spring context will fail to initialize with a "No bean named userConverter is defined" error.
BeanPostProcessor Solution
Because the native Spring solutions don't work, we developed our own solution for SAP Commerce in the form of BeanPostProcessors.
-
listMergeDirective mechanism enables you to add items to a list, which is a spring bean, or a spring bean property
-
mapMergeDirective mechanism enables you to add items to a map, which is a spring bean, or a spring bean property
-
modifyPopulatorList mechanism designed to work with populators and populator lists; enables you to add and remove populators from given populator lists
-
configurablePopulatorModification mechanism designed to work with configurable populators; enables you to add and remove populators from configurable populators
BeanFactoryPostProcessor
Because all BeanPostProcessors (BPP) work during bean creation and are sensitive only to special configuration beans, a problem arises when the whole context is lazy initialized.
-
spring.lazy.load.singletons - for a global application context
-
web.spring.lazy.load.singletons - for any web context
When configuration beans are lazy loaded, there is usually nothing in the system that requires them directly. It means that they are never created, and modifications described by those beans are never applied. For this reason, all configuration beans should be eagerly loaded. You can achieve it using LazyInitOverrideBeanFactoryPostProcessor, which is BeanFactoryPostProcessor (BFPP). When BPP works on bean instances during their creation, BFPP works on a bean definition before any bean is created. Thanks to this, we can force Spring context to load all configuration beans even when the rest of the context is lazy initialized.
Limitations
The mechanism for modifying Spring lists and maps doesn't support inner beans. Such modifications violate the bean processing lifecycle of the Spring framework, and are not allowed. References that end up represented in BeanPostProcessor as instances of the BeanDefinitionHolder, BeanDefinition, and ManagedArray classes aren't supported by ListMergeDirective and MapMergeDirective.
For example, you can't use mapMergeDirective as follows because <util:constant/> is treated as an inner bean:
<bean depends-on="mapBeanTest" parent="mapMergeDirective"> <property name="mapPropertyDescriptor" value="stringMap"/> <property name="key"> <util:constant static-field="de.hybris.platform.servicelayer.TpBeanTest.FOO" /> </property> <property name="value" value="valueValue"/> </bean>
However, you can define a normal bean with an id:
<util:constant id="foo" static-field="de.hybris.platform.servicelayer.TpBeanTest.FOO" /> <bean depends-on="mapBeanTest" parent="mapMergeDirective"> <property name="mapPropertyDescriptor" value="stringMap"/> <property name="key" ref="foo"/> <property name="value" value="valueValue"/> </bean>