Using Elastic Path Spring Beans
Elastic Path Core Commerce uses Spring Beans to create reusable modules of code. Beans are customized and wired into various Core Commerce modules through modifying the module’s .xml file where the Spring Beans are declared.
This section describes how to override existing bean definitions and how to register entity beans and register non-entity beans.
Overriding Existing Bean Definitions
Replacing a bean definition
To override an existing bean definition in the extensions module:
Navigate to the
.xmlfile containing the original bean definition in Core Commerce and open the file.Note the following:
- The bean’s
idattribute - The module in which the bean is defined
- The name of the
.xmlfile in which the bean is defined
- The bean’s
Navigate to the appropriate
extensionsmodule to in which to override the bean.For example, a bean defined in the
commerce-engine/core/ep-coremodule is overridden in theextensions/core/ext-coremodule.In the
extensionsmodule, navigae to the/resources/spring/servicedirectory and create an new Spring bean definition file.Use the same name as the file in which the bean is defined in Core Commerce, prefixed with
ext-. For example, a bean defined inservice.xmlis overridden in anext-service.xmlfile in extensions.By default, the
/spring/servicemay not be present in the/resourcedirectory. If needed, create the/spring/servicedirectory.In the Spring bean definition file, define a bean with the same
idattribute as the bean you want to override.In the
extensionsmodule, locate the*-plugin.xmlfile.This file might be located in the module’s
/resources/META-INF.confor/resources/META-INF/confdirectory. For example, theext-coremodule has a plugin file at the following location:/ext-core/resources/META-INF/conf/ep-core-plugin.xml.In the
*-plugin.xmlfile, define an<import>element with aresourceattribute value of the classpath of your extension bean definition file created in step 3.
Example: Overriding the priceListLookupService bean
For example, to override the priceListLookupService bean:
Locate its definition in the
ep-core/src/main/resources/spring/service/service.xmlfile:<bean id="priceListLookupService" class="com.elasticpath.common.pricing.service.impl.PriceListLookupServiceImpl"> <property name="plStackLookupStrategy" ref="plaStackLookupStrategy"/> </bean>Create an
ext-service.xmlfile in theext-core/resources/spring/servicedirectory with the bean override:<bean id="priceListLookupService" class="com.elasticpath.extensions.common.pricing.service.impl.ExtPriceListLookupServiceImpl"> <property name="plStackLookupStrategy" ref="plaStackLookupStrategy"/> </bean>In the
extensions/core/ext-core/ep-core-plugin.xmlfile add the following:<import resource="classpath:spring/service/ext-service.xml"/>
Reconfiguring an existing bean
To change only the configuration parameters of an existing bean definition, or override just the implementation class, do one of the following:
- The
com.elasticpath.commons.util.impl.PluginBeanFactoryPostProcessorclass to add additional properties to an existing bean - Use an
ep.override.propertiesfile to override specific parameter values
Using PluginBeanFactoryPostProcessor
The com.elasticpath.commons.util.impl.PluginBeanFactoryPostProcessor class is an implementation of the Spring BeanFactoryPostProcessor interface. Spring invokes this after all of the configuration is discovered and loaded in memory, but before the actual objects are created.
Below is a PluginBeanFactoryPostProcessor bean definition:
<bean class="com.elasticpath.commons.util.impl.PluginBeanFactoryPostProcessor">
<property name="extensionBeanName" value="EXTENSION_BEAN_NAME"/>
<property name="extensionClassName" value="EXTENSION_CLASS_NAME"/>
<property name="propertyName" value="PROPERTY_NAME"/>
<property name="propertyValue" ref="PROPERTY_VALUE"/>
</bean>
To override or extend an existing built-in bean, add a PluginBeanFactoryPostProcessor bean definition in your extension Spring configuration and specify the following properties:
extensionBeanNameThe name of the bean you want to reconfigure.
extensionClassNameThe class to set the bean to use. This property can be omitted if you don’t need to change the bean class that was assigned in the original bean definition.
propertyNameThe name of the property to reconfigure or add. This can be omitted if you are not changing any properties.
propertyValueThe value you want to assign to the property
For example, to override the implementation class of the priceListLookupService bean without copying any bean properties, the definition is as follows:
<bean class="com.elasticpath.commons.util.impl.PluginBeanFactoryPostProcessor">
<property name="extensionBeanName" value="priceListLookupService"/>
<property name="extensionClassName" value="com.elasticpath.extensions.common.pricing.service.impl.ExtPriceListLookupServiceImpl">
</bean>
Overriding multiple properties
To override multiple properties on a particular bean, use the propertiesMap property as follows:
<property name="propertiesMap">
<map>
<entry key="customerService" ref="customerService"/>
<entry key="elasticPath" ref="elasticPath"/>
</map>
</property>
note
Functionality can be added to this architecture by modifying the PluginBeanFactoryPostProcessor.postProcessBeanFactory() method implementation.
For example, you could add an extendList property to allow a property value to add to a bean’s list property rather than overwrite it.
Using an ep.override.properties file to override specific parameters
As an alternative, it is also possible to override Spring parameter values via the ep.override.properties file. This is advantageous when the parameter value should differ between different deployment environments.
For more information refer to Creating an ep.override.properties file