Sling Models are annotation-driven Java "POJO's" (Plain Old Java Objects) that facilitate the mapping of data from the JCR to Java variables and provide several other niceties when developing in the context of AEM. When you adapt to a sling model, the model factory returns the specific model implementation; the returned model implementation is selected through the model pickers.
In this post, let us dive deep into how this model picking works internally.
The Sling Model Framework has the Implementation Picker to select the specific model implementation.
The Implementation Picker interface can be referred to here — ImplementationPicker (Apache Sling 8 API); the picker interface is enabled with a picker method responsible for selecting a specific model implementation.
The Sling Framework, by default, has the below picker implementations.
First Implementation Picker:
This is the default picker implementation; it picks the first implementation from the list alphabetically ordered by class name. If there are multiple implementations of the specific models, this one selects the first one and returns it to the requestor. First Implementation Picker will be used if no other implementations support picking the model implementation.
The service ranking is set to the highest (Integer.MAX_VALUE) to support more custom picker implementations.
Resource Type-Based Resource Picker:
This picks the specific model implementation based on the resource type. This picker's ranking is 0 to ensure this comes before First Implementation Picker.
The AEM core components have their picker implementation to pick the latest version of a model implementation for a specific model.
Latest Version Implementation Picker:
This picks the specific model implementation based on the resource type and the model version —Picks the latest model version of a core component model based on the version information in the package name. The ranking for this picker is specified as 1 to ensure this comes after the Resource Type Based Resource Picker.
All the core components reference the model interface directly — com.adobe.cq.wcm.core.components.models.Carousel; the model implementations are always enabled with a specific version, so the Resource Type Based Resource Picker will not pick any model implementation, and the picking is delegated to the Latest Version Implementation Picker, and this always returns the latest model implementation based on the version number — e.g., com.adobe.cq.wcm.core.components.internal.models.v1.CarouselImpl).
The External models that implement Core Component interfaces have precedence over internal models, allowing us to enable model delegation for the core components.
The custom picker implementation can be enabled by implementing the org.apache.sling.models.spi.ImplementationPicker interface and picker method with your custom model picker logic; the ranking should be enabled accordingly to ensure the default implementations are not impacted.
No comments:
Post a Comment