Sling Annotations | Community
Skip to main content
Level 2
January 9, 2024
Solved

Sling Annotations

  • January 9, 2024
  • 3 replies
  • 1170 views

I am new to AEM world and I am still learning about AEM Sling Models, Components and Services. I really find it hard to select what annotations needs to be used and when. Can someone help me with a crisp info about the different annotations

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Madhur-Madan

Hi @ankit_gangishetti,

Annotation Description
@Model declares a model class or interface
@Inject marks a field or method as injectable
@Self Injects the adaptable itself. If the field type does not match with the adaptable it is tried to adapt the adaptable to the requested type
@Named declare a name for the injection (otherwise, defaults based on field or method name)
@Optional marks a field or method injection as optional
@Source explictly tie an injected field or method to a particular injector (by name). can also be on other annotations.
@PostConstruct methods to call upon model option creation (only for model classes)
@Via change the adaptable as the source of the injection
@Default set default values for a field or method
@ScriptVariable Injects the script variable defined via Sling Bindings. If name is not set the name is derived from the method/field name
@ValueMapValue Injects a ValueMap value. If via is not set, it will automatically take resource if the adaptable is the SlingHttpServletRequest. If name is not set the name is derived from the method/field name
@RequestAttribute Injects a request attribute by name. If name is not set the name is derived from the method/field name
@OSGiService Injects an OSGi service by type. The filter can be used give an OSGi service filter
@SlingObject Injects commonly used sling objects if the field matches with the class: request, response, resource resolver, current resource, SlingScriptHelper

3 replies

Madhur-Madan
Community Advisor
Madhur-MadanCommunity AdvisorAccepted solution
Community Advisor
January 9, 2024

Hi @ankit_gangishetti,

Annotation Description
@Model declares a model class or interface
@Inject marks a field or method as injectable
@Self Injects the adaptable itself. If the field type does not match with the adaptable it is tried to adapt the adaptable to the requested type
@Named declare a name for the injection (otherwise, defaults based on field or method name)
@Optional marks a field or method injection as optional
@Source explictly tie an injected field or method to a particular injector (by name). can also be on other annotations.
@PostConstruct methods to call upon model option creation (only for model classes)
@Via change the adaptable as the source of the injection
@Default set default values for a field or method
@ScriptVariable Injects the script variable defined via Sling Bindings. If name is not set the name is derived from the method/field name
@ValueMapValue Injects a ValueMap value. If via is not set, it will automatically take resource if the adaptable is the SlingHttpServletRequest. If name is not set the name is derived from the method/field name
@RequestAttribute Injects a request attribute by name. If name is not set the name is derived from the method/field name
@OSGiService Injects an OSGi service by type. The filter can be used give an OSGi service filter
@SlingObject Injects commonly used sling objects if the field matches with the class: request, response, resource resolver, current resource, SlingScriptHelper

iamnjain
Community Advisor
Community Advisor
January 9, 2024

Hi @ankit_gangishetti 

 

There are lot of information available about the same but it might feel a tech jargon instead of simplifying the main purpose of it.

I would recommend you to go through this article and later on official sling documentation page.

P.S. Practice it in a project for better understanding 🙂

https://sourcedcode.com/blog/aem/aem-sling-model-injectors-annotations-cheat-sheet-reference-guide

 

https://sling.apache.org/documentation/bundles/models.html#available-injectors

 

sherinregi-1
Community Advisor
Community Advisor
January 9, 2024

Hi @ankit_gangishetti 

 

Please follow the below link to know about the sling models and the annotations also the use cases where each one needs to be used.

 

It has explanations and code snippets 

https://techrevelhub.wordpress.com/2023/10/11/sling-model-annotations-best-practices/