diff b/w @self ,@ScriptVariable and @SlingObject annotation
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @user96222
@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.
@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.
@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.
Hi @user96222
@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.
@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.
@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.
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.
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:
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.
@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().
@user96222 : You can refer the documentation : https://sling.apache.org/documentation/bundles/models.html#available-injectors
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.
@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.
Views
Replies
Total Likes
Views
Like
Replies
Views
Likes
Replies