Hi,
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
Is there any specific documentation on which annotation is valid for which adaptable (Resource vs SlingHttpRequest), For example: I could not use @scriptvariable Page currentPage if adaptable is Resource.class. currentPage returns null.
I would need documentation or API which tells which annotations provide what all objects and which annotations are valid for Resource, SlingHttpRequest adaptables.
I looked at this documentation but doesnot clearly says what annotations are valid for which adaptables.
https://sling.apache.org/documentation/bundles/models.html
Any API classes implementation source code to look at also fine.
Solved! Go to Solution.
Views
Replies
Total Likes
Hard to say. It feels somehow intuitive for me, but I understand that this is just me.
@ScriptVariable only works in the context where such scripting variables are available and that is only the case when model is adapted from a request. Unfortunately the Sling Model documentation lacks this information, I will add it.
Thanks for bringing this up,
Jörg
HI @sreenu539
Unfortunately, there is no specific document that explains the adaptable w.r.t the annotations. But please look at the text that's mentioned in the same link that you have pasted for the exact usage of the adaptables
Hope this helps!
Thanks,
Kiran Vedantam.
Any API implementation code to look at?
Here is Apache Sling documentation on Sling Models.
https://sling.apache.org/documentation/bundles/models.html#available-injectors
The table clearly explains about each annotation.
@sreenu539 See if this cheat sheet from @BrianKasingli helps you https://sourcedcode.com/blog/aem/aem-sling-model-injectors-annotations-cheat-sheet-reference-guide
Hard to say. It feels somehow intuitive for me, but I understand that this is just me.
@ScriptVariable only works in the context where such scripting variables are available and that is only the case when model is adapted from a request. Unfortunately the Sling Model documentation lacks this information, I will add it.
Thanks for bringing this up,
Jörg
Hi @sreenu539
https://sourcedcode.com/blog/aem/aem-sling-model-injectors-annotations-cheat-sheet-reference-guide is the article I mostly refer and its great!
Hope it helps!
Thanks
Nupur
The Sling Models documentation has been updated, and it mentions that the @scriptVariable annotation requires the adaptable to be a SlingHttpServletRequest.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies