Registering Bean Definitions
Registering Entity Beans
Elastic Path Core Commerce uses the Spring bean factory to create entity beans when required. Default beans are defined in core’s commerce-engine/core/-ep-core/src/main/resources/spring/prototypes/prototypes.xml
file.
To register entity beans, add the bean definition to your extensions
module’s Spring configuration file.
If you’re overriding an existing bean definition, make sure the bean’s id
attribute value is the same as the original, and that the class points to your extension class.
warning
Be sure to set the bean’s scope
attribute to prototype
.
For example, prototypes.xml
contains the following definition for the product
bean:
<bean id="product" scope="prototype" class="com.elasticpath.domain.catalog.impl.ProductImpl"/>
Suppose you override this class with com.extensions.domain.catalog.impl.ExtProductImpl
. You just need to add the following bean definition to to your extension Spring configuration:
<bean id="product" scope="prototype" class="com.extensions.domain.catalog.impl.ExtProductImpl"/>
Bean Definition Clutter
Continually adding bean definitions to a single extensions
module Spring configuration file can lead to clutter. To avoid this, use Spring’s import
element, to store your custom and override bean definitions in files within a directory structure that mirrors the default application directory structure.
For example, to override ep-core
module bean definitions in the resources/spring/models/domainModel.xml
and resources/spring/service/service.xml
files, create extension Spring configuration files in the same locations as ep-core
with corresponding names that have a prefix of ext-
. Then import them into the ep-core-plugin.xml
file as follows:
<import resource="classpath*:spring/models/ext-domainModel.xml"/>
<import resource="classpath*:spring/service/ext-service.xml"/>
Note that these files should only contain your custom and overriding bean definitions. Do not include any default bean definitions.
For more information on configuring entity beans, refer to Extending the Domain Model.
Registering Non-Entity Beans
To register a non-entity beans, you add the bean definition to your extension project Spring configuration.
If you’re overriding an existing bean definition, make sure the bean id
attribute is the same as the originalm and that the class attribute points to your extension class.
For example, the ep-core/src/main/resources/spring/service/service.xml
class contains the following bean definition for productXmlService
:
<bean id="productXmlService" class="com.elasticpath.service.catalog.impl.ProductXmlServiceImpl">
<property name="productLookup" ref="productLookup"/>
<property name="categoryLookup" ref="categoryLookup"/>
<property name="beanFactory" ref="coreBeanFactory"/>
</bean>
You have overridden this with this custom class com.elasticpath.extensions.service.catalog.impl.ExtProductXmlServiceImpl
. To enable your overridden service, you need to add the following bean definition to your core extension:
<bean id="productXmlService" class="com.elasticpath.service.catalog.impl.ExtProductXmlServiceImpl">
<property name="productLookup" ref="productLookup"/>
<property name="categoryLookup" ref="categoryLookup"/>
<property name="beanFactory" ref="coreBeanFactory"/>
</bean>
note
If your service only applies to a specific module project, you only need to add it to that module’s ep-<module>-plugin.xml
file.
Bean Definition Clutter
Continually adding bean definitions to a single extensions module Spring configuration file can lead to clutter. To avoid this, use Spring’s import element, to store your custom and override bean definitions in files within a directory structure that mirrors the default application directory structure.
For example, to override ep-core
module bean definitions in the resources/spring/models/domainModel.xml
and resources/spring/service/service.xml
files, create extension Spring configuration files in the same locations as ep-core
with corresponding names that have a prefix of ext-
. Then import them into the ep-core-plugin.xml
file as follows:
<import resource="classpath*:spring/models/ext-domainModel.xml"/>
<import resource="classpath*:spring/service/ext-service.xml"/>
Note that these files should only contain your custom and overriding bean definitions. Do not include any default bean definitions.