In this article, we will learn about the Complete Flow of Resource Resolvers that we use in the Sling Model.
What is ResourceResolver
in AEM Sling Model?
ResourceResolver
is an interface that provides access to resources in the JCR (Java Content Repository). It is part of the Sling API and serves as a bridge between your code and the JCR repository, allowing you to read, update, or manipulate resources (e.g., pages, components, nodes).Resource Resolver Flow
- The AEM instance starts running.
- Once the AEM instance is running, the Resource Resolver Factory executes and provides all possible Resource Resolvers. There can be one or multiple Resource Resolvers.
- In our Sling Model, the Resource used in the
@Model
adaptable annotation comes from the Resource Resolver provided by the Resource Resolver Factory. - Using this Resource, we can access the
getResourceResolver()
function. - Through this function, we obtain the QueryBuilder and Session objects.
- Predicates are the parameter that we get form Query Builder AEM instance to pass in QueryBuilder in Code.
Sling Model and its Annotations Explained
In AEM, Sling Models are a framework that allows developers to map Java objects to resources in the JCR (Java Content Repository). They simplify data access and manipulation in AEM components.
Key Annotations in Sling Models:
@Model
:This annotation defines a class as a Sling Model.
It specifies the adaptables
(the source object types the model can work with, such as
SlingHttpServletRequest
or Resource
)The
adaptables
attribute in the @Model
annotation specifies what the Sling Model can adapt to:
SlingHttpServletRequest.class
: Used when you want to adapt HTTP requests, often to access page-related objects likecurrentPage
.
example
@Model(adaptables = SlingHttpServletRequest.class) public class MyModel { // Model logic here }
Resource.class
: Used when you want to work with JCR nodes directly.Resource
public class MyModel { // Model logic here }
Some other UseFull annotation of Sling Modal
1. What is @RequestAttribute
The @RequestAttribute
annotation is used in Sling Models to inject a variable or value that is passed from Sightly (HTL) into the Sling Model. It provides a simple way to pass data dynamically from the front end to the back end.
Here, variableName='TestAttribute'
passes the value "TestAttribute"
as an attribute to the Sling Model.
Sling modal code
@RequestAttribute
injects thevariableName
value from Sightly into thereqVariable
field in the Sling Model.
2. @PostConstruct
The @PostConstruct
annotation marks a method to be executed after all injections (like @Inject
, @ValueMapValue
, @ScriptVariable
) are completed. It is primarily used for initialization logic in the Sling Model.
Use Case:
- When you need to process or prepare data after dependencies have been injected into the model.
Key Notes:
- This method is executed automatically after the object has been fully initialized.
- Use it for data manipulation, validation, or additional setup.
3. @OSGiService
The @OSGiService
annotation is used to inject OSGi services (either default or custom) into the Sling Model. These services are managed by the OSGi container and provide reusable business logic or utilities.
Use Case:
- When you need to call a default or custom OSGi service in your model for additional logic or processing.
Sling Model for multifield-like faqs with GraphQL and OSGI Service in AEM
- modal class
- OSGI Configuration
- Gquery.java
- ApiConstan.java
Key Points for Managing Multifields in AEM Sling Models:
- Using a Map: Best suited when all fields in the multifield are of the same type, such as
String
orBoolean
. - Using a Bean: Ideal when fields in the multifield have different types, allowing for better structure and type safety.
0 comments: