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
.xml
file containing the original bean definition in Core Commerce and open the file.Note the following:
- The bean’s
id
attribute - The module in which the bean is defined
- The name of the
.xml
file in which the bean is defined
- The bean’s
Navigate to the appropriate
extensions
module to in which to override the bean.For example, a bean defined in the
commerce-engine/core/ep-core
module is overridden in theextensions/core/ext-core
module.In the
extensions
module, navigae to the/resources/spring/service
directory 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.xml
is overridden in anext-service.xml
file in extensions.By default, the
/spring/service
may not be present in the/resource
directory. If needed, create the/spring/service
directory.In the Spring bean definition file, define a bean with the same
id
attribute as the bean you want to override.In the
extensions
module, locate the*-plugin.xml
file.This file might be located in the module’s
/resources/META-INF.conf
or/resources/META-INF/conf
directory. For example, theext-core
module has a plugin file at the following location:/ext-core/resources/META-INF/conf/ep-core-plugin.xml
.In the
*-plugin.xml
file, define an<import>
element with aresource
attribute value of the classpath of your extension bean definition file created in step 3.
priceListLookupService
bean
Example: Overriding the For example, to override the priceListLookupService
bean:
Locate its definition in the
ep-core/src/main/resources/spring/service/service.xml
file:<bean id="priceListLookupService" class="com.elasticpath.common.pricing.service.impl.PriceListLookupServiceImpl"> <property name="plStackLookupStrategy" ref="plaStackLookupStrategy"/> </bean>
Create an
ext-service.xml
file in theext-core/resources/spring/service
directory 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.xml
file 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.PluginBeanFactoryPostProcessor
class to add additional properties to an existing bean - Use an
ep.override.properties
file to override specific parameter values
PluginBeanFactoryPostProcessor
Using 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:
extensionBeanName
The name of the bean you want to reconfigure.
extensionClassName
The 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.
propertyName
The name of the property to reconfigure or add. This can be omitted if you are not changing any properties.
propertyValue
The 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.
ep.override.properties
file to override specific parameters
Using an 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