Elastic Path Commerce Development

Overriding Existing Bean Definitions

Overriding Existing Bean Definitions

Replacing a bean definition

To override an existing bean definition in the extensions module:

  1. 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.
  2. 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 the extensions/core/ext-core module.

  3. 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 in service.xml. is overridden in an ext-service.xml file in extensions.

    Note: By default, the /spring/service may not be present in the /resource directory. If needed, create the /spring/service directory.
  4. In the Spring bean definition file, define a bean with the same id attribute as the bean you want to override.
  5. 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, the ext-core module has a plugin file at the following location: /ext-core/resources/META-INF/conf/ep-core-plugin.xml.

  6. In the *-plugin.xml file, define an <import> element with a resource attribute 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:

  1. 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>
  2. Create an ext-service.xml file in the ext-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>
  3. 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.

Using PluginBeanFactoryPostProcessor

Overview

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:

Property Description

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 ommitted 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.

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.