Expand my Community achievements bar.

SOLVED

Sling Annotations

Avatar

Level 2

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

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

Avatar

Community Advisor

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

 

Avatar

Community Advisor

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/