Monday, 16 May 2016

Spring in Hybris Post2:

Spring in Hybris Post2:

Spring Configuration:

Spring configuration of extension uses 3 xml files for your bean definations they are 1.global-<extenstion-name>.xml,2.<extension-name>-spring.xml and 3.web-application-config.xml 

Bean aliasing:

Sometimes desirable to give a single bean multiple names, otherwise known as bean aliasing.Spring provieds a <alias> tag.
Syntax:<alias name="bean-name" alias="alias-name-for-defined-bean"/>
In Hybris using <alias> to avoid overwrting Services.

For example:

ExtensionOne:-  
<alias name="demoservice" alias="demo1Service"/>
<bean name="Demo1Service" class="ABC"/>
ExtensionTwo:-
<alias name="demoService" alias="newDemoService"/>
<bean name="newDemoService" class="XYZ"/> 
 Two are more extensions uses same bean name Services  and one extension depend on another extension it means
<requires-extension name="ExtensionOne"/> from above example. We uses alias name of bean to avoid overwriting Services.

Spring and Hybris Bean scopes:

The Spring Framework supports five scopes they are singleton, prototype,request, session, global-session three of which are available only if you use a web-aware ApplicationContext they are request,session,global-session.
In side the <bean> tag we have an attribute option scope.This is used for which scope is applicable to that bean.

For example:-

<bean id=”…” class=”…” scope=”singleton”>
<!—configuration for this bean and  properties go there-->
</bean>

Spring provides @Scope annotation to keep the bean in desired scope.We need to  Annotate the class with this annotation and provide the bean scope value. 

For example:

@Scope(singleton”)
Public class BeanScopeDemo {}

 Apart from this  spring scopes hybris supports two extra bean scopes they are yrequest and tenant.
        yrequest scope is same as  request scope in spring.The yrequest scope may be used for binding beans to the application context for a single request. The global-spring- core.xml file of the core extension adds a special scope named yrequest to ApplicationContext. The yrequest scope is used out side the web application context. It means that you can access these beans even in absence of a web application.If a session is invalidated, instances are destroyed  the scope quit to exist from the applicationcontext.

Example for yrequest scope:

<bean id="..." class=".." scope="yrequest">
<!—configuration for this bean and  properties go there-->
</bean>
Tenant scope is same as singleton scope of spring. Hybris has deprecated tenant scope in version 5.0.The Tenant scope makes sure that the bean is instantiated once per tanant of the hybris. Whereas singleton would create only one instance for all tenants to use.

Example for tenant scope:

<bean id=”…” class=”…” scope=”tenant”>
<!—configuration for this bean and  properties go there-->
</bean>

No comments:

Post a Comment