hybris impex 文件在初始化和更新时的自动执行情况
Posted wahaha603
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hybris impex 文件在初始化和更新时的自动执行情况相关的知识,希望对你有一定的参考价值。
It is possible to hook ServiceLayer code into the hybris initialization and update life-cycle events, thus providing means for creating essential and project data for a given extension. It can be done using @SystemSetup annotation in any ServiceLayer class, in any extension. You can define when a method should be executed, and pass context information to it. By using annotations, no interfaces have to be implemented or parent classes extended, keeping the classes loosely coupled. Methods are called in order that respects the extension build order.
This document provides you with information on how to hook into the init and update life-cycle events.
Audience: Developers, software architects
Related extension: core Extension
Validity:
4.2.1 and higherBased on hybris version:
4.2.1 BetaSpring Configuration of Java Class
The Java classes, in which the @SystemSetup annotation is used, must be registered as Spring beans in the ApplicationContext:
< bean id = "someclass" class = "de.hybris.sample.extension.SomeClass" scope = "tenant" /> |
Java Class Annotations
Annotate the classes whose methods are to be executed. In the following example the method createImportantModelDuringInitProcess
is executed during the initialization process when the essential data for extension myextension is created.
@SystemSetup (extension = MyExtension.EXTENSIONNAME) public class SomeClass ... { @SystemSetup (extension = MyExtension.EXTENSIONNAME, process = Process.INIT, type = Type.ESSENTIAL) public void createImportantModelDuringInitProcess(){ //create the model here } ... |
Note that:
-
The @SystemSetup annotation is recognized for public methods only.
- If the annotation is attached to a class, the defined values are used as default for all annotated methods within the class.
- It is possible to annotate any number of methods for a class.
-
If the annotation attribute extension is not defined, the default value is an empty String. It means that the annotated code is not executed as there is no extension with the name "".
-
If the annotation attributes process and type are not defined, the default ALL is used
Below are further examples:
//AnotherJavaClass.java @SystemSetup (extension = MyExtension.EXTENSIONNAME, process = Process.UPDATE, type = Type.ESSENTIAL) public class AnotherJavaClass { @SystemSetup protected void ignoredMethodOne() {} public void ignoredMethodTwo() {} //Neither of the above will be executed because only public methods with the annotation will be recognized @SystemSetup (extension = "myOtherExtension" ) {} //Although this class is located in myextension, it is called during the update //process and creating essential data for the extension myOtherExtension } |
@SystemSetup (extension = "myextension" ) public class DifferentJavaClass { @SystemSetup (type = Type.ESSENTIAL) public methodOne() {} @SystemSetup (type = Type.PROJECT) public methodTwo() {} // methodOne will be executed during creation of essential data in myextension, in both init and update processes // methodTwo will be executed during creation of project data in the same extension, in both init and update // processes } |
Annotation Attributes
The following annotation arguments are available:
-
extension: Defines, in which extension the code should be executed. The order of extensions is given by the build order. Use the extensionname constant, for example MyExtension.EXTENSIONNAME
-
process: Defines, whether the code should be executed during the initialization or update process.
-
INIT: Code is executed only during the initialization process.
-
UPDATE: Code is executed only during the update process.
-
ALL (Default): Code is executed twice, during the initialization and during the update process.
-
-
type: Defines if the code should be executed during the essential data creation or project data creation process.
-
ESSENTIAL: Code is executed during the essential data creation process.
-
PROJECT: Code is executed during the project data creation process.
-
ALL (Default): Code is executed twice, during the essential data creation and during the project data creation process.
-
Usage of Parameters
If you need any additional information in your annotated methods, you can use the SystemSetupContext. For details see the according API documentation.
The following code sample shows how to use the SystemSetupContext:
@DataSetup (type = Type.PROJECT, method = Method.ALL, extension = "myExtension" ) public void createData(SystemSetupContext context) { if (context.getParameterMap().containsKey( "createAdditionalStuff" ) && "true" .equalsIgnoreCase(context.getParameterMap().get( "createAdditionalStuff" ).toString())) { if (context.getMethod().isInit()) { // for example, delete everything first } // create additional stuff } // create normal stuff } |
Usage of User-Defined Parameters
Sometimes it is helpful to ask the customer what to do, for example:
For defining user-specific parameters, you have to add a method annotated with @SystemSetupParameterMethod inside a SystemSetup annotated class that returns a list of parameters. Here is the example according to the screenshot above:
@SystemSetupParameterMethod public List<SystemSetupParameter> getSystemSetupParameters() { final List<SystemSetupParameter> params = new ArrayList<SystemSetupParameter>(); final SystemSetupParameter customDataParameter = new SystemSetupParameter( "createCustomData" ); customDataParameter.setLabel( "Create custom data?" ); customDataParameter.addValue( "true" ); customDataParameter.addValue( "false" , true ); params.add(customDataParameter); final SystemSetupParameter imports = new SystemSetupParameter( "imports" ); imports.setMultiSelect( true ); imports.setLabel( "Data to import : " ); imports.addValue( "users" , true ); imports.addValues( new String[] { "groups" , "tenants" , "grandmas" , "grandpas" , "uncles" }); params.add(imports); return params; } |
Note that:
-
customData parameter is a single selection list box.
-
imports parameter is a multiple selection list box.
After the customer has made his decision according to the parameters you offered, you can access the result by evaluating the SystemSetupContext. The parameters are stored in the map with the prefix <extension>_ to avoid having duplicate parameters. If you are using a single selection list box, then you can easily use the SystemSetupContext.getParameter(key) method. SystemSetupContext.getParameters(key) method returns a String[] with all selected options.
@SystemSetup (type = Type.PROJECT, process = Process.ALL) public void createProjectData( final SystemSetupContext context) throws Exception { LOG.info( "-----> createCustomData : " + context.getParameter(CoreConstants.EXTENSIONNAME + "_createCustomData" )); LOG.info( "-----> imports :" ); for ( final String imp : context.getParameters(CoreConstants.EXTENSIONNAME + "_core_imports" )) { LOG.info( "------------------> " + imp); } } |
Method Calling Order
The call order is controlled by your classloader and also depends on when the according bean is defined. Beans are normally created in the extension build order.
Manual Call for a Special Extension
For testing purposes you might want to execute all those beans for your extension. This can be done by the SystemSetupCollector:
public
class
MyTest
{
private
ApplicationContext applicationContext;
@Before
public
void
setUp()
{
final
SystemSetupContext systemSetupContext =
new
SystemSetupContext(
null
, Type.ESSENTIAL,
"MyExtension"
);
applicationContext.getBean(SystemSetupCollector.
class
).executeMethods(systemSetupContext);
}
public
void
setApplicationContext(ApplicationContext applicationContext)
{
this
.applicationContext = applicationContext;
}
}
以上是关于hybris impex 文件在初始化和更新时的自动执行情况的主要内容,如果未能解决你的问题,请参考以下文章