Expand my Community achievements bar.

SOLVED

what is diff between @self ,@ScriptVariable and @SlingObject annotation in simple words

Avatar

Level 3

diff b/w  @self ,@ScriptVariable and @SlingObject annotation

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @user96222 

  1. @self: The @self annotation is used to inject the current resource into a Sling Model. It allows you to access properties and child resources of the current resource directly within the Sling Model class. It is useful when you need to work with the specific resource that the Sling Model is associated with.

  2. @ScriptVariable: The @ScriptVariable annotation is used to inject variables from the current Sling request into a Sling Model. It allows you to access request-level variables such as request, response, currentPage, currentStyle, etc., within the Sling Model class. It is helpful when you need to access information related to the current request.

  3. @SlingObject: The @SlingObject annotation is used to inject any OSGi service or Sling object into a Sling Model. It allows you to access services and objects such as the ResourceResolver, ResourceResolverFactory, PageManager, etc., within the Sling Model class. It is beneficial when you need to interact with various services or objects provided by AEM.

 @self is used to work with the current resource,
 @ScriptVariable is used to access request-level variables,
 @SlingObject is used to interact with OSGi services or Sling objects within a Sling Model. These annotations provide a convenient way to access and utilize different aspects of the AEM environment within your Sling Model classes.



View solution in original post

6 Replies

Avatar

Correct answer by
Community Advisor

Hi @user96222 

  1. @self: The @self annotation is used to inject the current resource into a Sling Model. It allows you to access properties and child resources of the current resource directly within the Sling Model class. It is useful when you need to work with the specific resource that the Sling Model is associated with.

  2. @ScriptVariable: The @ScriptVariable annotation is used to inject variables from the current Sling request into a Sling Model. It allows you to access request-level variables such as request, response, currentPage, currentStyle, etc., within the Sling Model class. It is helpful when you need to access information related to the current request.

  3. @SlingObject: The @SlingObject annotation is used to inject any OSGi service or Sling object into a Sling Model. It allows you to access services and objects such as the ResourceResolver, ResourceResolverFactory, PageManager, etc., within the Sling Model class. It is beneficial when you need to interact with various services or objects provided by AEM.

 @self is used to work with the current resource,
 @ScriptVariable is used to access request-level variables,
 @SlingObject is used to interact with OSGi services or Sling objects within a Sling Model. These annotations provide a convenient way to access and utilize different aspects of the AEM environment within your Sling Model classes.



Avatar

Level 10

@Self Annotation

According to the documentation, this annotation injects the adaptable object itself. If the @Self annotation is present it is tried to adapt the adaptable to the field type.

 

 

@ScriptVariable & @SlingObject


The key difference is in the way the object is retrieved. @ScriptVariable and  the injector that kicks in on fields annotated with @SlingObject is a bit more interesting in that it will inspect the adaptable (from which your Sling Model is being adapted) and obtain the resolver accordingly:

  • If the adaptable is a Resource, it'll return resource.getResourceResolver(), i.e. the resolver originally used to load the resource. This may be quite aribtrary.
  • If the adaptable is a SlingHttpServletRequest, it'll return request.getResourceResolver(), i.e. the resolver instantiated by Sling in association with the request and the session of the user issuing the request.
  • If the adaptable itself is a ResourceResolver, it will be returned directly.

In some cases, the difference between the three will be negligible, but sometimes it will be important which resolver you choose. If your model is, for example, supposed to be used outside the scope of a request (imagine it's instantiated by an OSGi service that runs on a schedule and there's no incoming request), you'll be better off using resource or resourceResolver as an adaptable.

Avatar

Level 10

@Self annotation injects the adaptable object itself. If the @Self annotation is present it is tried to adapt the adaptable to the field type. So anytime, when you need to extract some data from the request or resource in your model (like remote user id or parent resource path), you can create a model, which extract this information and then inject it with @Self into the model, where you need this information. The first model (which works with the external resource) can be treated as adapter. So you keep your logic in the model away from the external dependencies, because it’s adapter’s business, how to get some information from request/resource/etc.

Sling Object: Injects common Sling objects that can be derived from either a SlingHttpServletRequest, a ResourceResolver or a  Resource. The injection is class-based.

@ScriptVariable The injector that recognises the @ScriptVariable annotation can inject objects available as Scripting Variables. In case ot the resourceResolver, it should be the one associated with the request. Same as slingRequest.getResourceResolver().

Avatar

Community Advisor

Simply, each one of them injects different types of objects. You need to choose which to use depending on the type of object you need.



Esteban Bustamante

Avatar

Administrator

@user96222  Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni