What Is Sling Models in AEM?

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

What is sling models in AEM?

Sling Models are annotation driven Java “POJO's” (Plain Old Java Objects) which are
mapped automatically with Sling Objects (like Resource, Request….) and allow us to
access JCR node property values directly into java classes via annotations.
Sling Models supports both class and interface.
A sling Model is implemented as an OSGi Bundle.

How do we find sling models in AEM?


Add sling models package in core/pom.xml

How do you make AEM Sling Model?


1. We are using @Model annotation to specify that this model class is a Sling Model.
2. Each data member is annotated with @Inject.
3. This class will be mapped to a resource in JCR.

 A Sling Model is implemented as an OSGi bundle.


 A Java class located in the OSGi bundle is annotated with @Model and the adaptable
class (for example, @Model (adaptables = Resource. class).
 The data members (Fields) use @Inject annotations.

Advantages of using Sling Models


1. Pure POJO classes.
2. Entirely annotation driven (Need to write less code).
3. Can adapt multiple objects – minimal required Resource and SlingHttpServletRequest.
4. OOTB, support resource properties (via ValueMap), SlingBindings, OSGi services,
request attributes

Basic Usage:
@Model (adaptables=Resource.class)
Public class UserModel {
@Inject
private String firstName; //property name or variable name or data member
@Inject
private String lastName;
}

Sling Models vs WCMUsePojo

1. WCMUsePojo will need to be extend from WCMUsePojo class, whereas Sling Models
can be standalone class with @Model annotation and no keyword
2. With Sling Models, it’s simpler and cleaner to retrieve common objects or property
values, instead of writing more line of code to use API
3. You may use Felix annotation @Reference to reference to an available OSGI service,
whereas in Sling Models, you will use @Inject or @OSGiService
4. With Sling Models API 1.3, you can serialize the model and export it as a JSON file with
Jackson exporter, so your front-end application can leverage the same model. It’s not
available for WCMUsePojo.
5. For WCMUsePojo, you will need to overwrite the activate () method, whereas in Sling
Models, your init () method will be called in the @PostConstruct annotation

Sling Model Exporter:


Apache Sling Model Exporter comes with a Sling provided Jackson Exporter that
automatically serializes an “ordinary” Sling Model object into JSON. The Jackson Exporter,
while quite configurable, at its core inspects the Sling Model object, and generates JSON
using any “getter” methods as JSON keys, and the getter return values as the JSON values.

Sling Context-Aware Configurations

Your site may need different configurations for different sites regions for instance where
some parameters may be shared requiring inheritance for nested contexts and global
fullback values. AEM leverages Sling context aware configurations, which enable this
possibility.

You might also like