AEM - Sling | Community
Skip to main content
April 10, 2025
Solved

AEM - Sling

  • April 10, 2025
  • 3 replies
  • 862 views

Hi @16227148,

 

In Adobe Experience Manager (AEM), when defining a Sling Model with the following adaptation:

@Model(adaptables = { Resource.class, SlingHttpServletRequest.class })

 

What is the purpose of still using the @Self annotation for fields such as:

@Self
private Resource resource;

@Self
private SlingHttpServletRequest request;

How does the @Self annotation interact with the @Model(adaptables) declaration, and what is the significance of using both in a Sling Model?

 

Best answer by tushaar_srivastava

Hi @yaminikolhe 

You can go through including videos: http://www.sgaemsolutions.com/2017/08/deep-dive-on-sling-models-part-1.html 
https://satyamblogs.medium.com/sling-model-annotation-detailed-in-aem-4af0345d43ec 
https://taradevko.com/aem/sling-models-self-annotation/ 

Kindly go through all 3 links in detail and let's discuss any specific questions on it.

3 replies

tushaar_srivastava
tushaar_srivastavaAccepted solution
April 10, 2025
April 11, 2025

@tushaar_srivastava Thank you for sharing such helpful information.

tushaar_srivastava
April 11, 2025

if that information solves your question do mark the above comment as correct reply, so that other can also get benefited!

Thanks for raising this important question

PRATHYUSHA_VP
Community Advisor
Community Advisor
April 10, 2025
April 11, 2025

@prathyusha_vp Thank you for sharing such helpful information.

Jagadeesh_Prakash
Community Advisor
Community Advisor
April 11, 2025

@yaminikolhe 

 

Great question—this touches on a subtle but important aspect of how Sling Models resolve dependencies using annotations like @Model and @Self.

 

Purpose of @Model(adaptables = { ... })

@Model(adaptables = { Resource.class, SlingHttpServletRequest.class })

You're telling Sling Models that this class can be instantiated (adapted) from either a Resource or a SlingHttpServletRequest

So when code like this runs:

modelFactory.getModelFromWrappedRequest(request, MyModel.class);

or

resource.adaptTo(MyModel.class);

It will look for a matching adaptable type and attempt to inject the model.

 

What does @Self mean then?

 

The @Self annotation is used on fields or constructor parameters inside the model to inject the same object that was used to adapt the model.

For ex:

@Self
private Resource resource;


Here’s what this does:
- If the model was adapted from a Resource, resource will be injected with that same Resource.
- If the model was adapted from a SlingHttpServletRequest, the framework will try to get a Resource from the request (i.e., request.getResource()) and inject that.

 

Hope this is clear

April 11, 2025