Model annotations for different adaptables | Community
Skip to main content
sreenu539
Level 7
January 25, 2023
Solved

Model annotations for different adaptables

  • January 25, 2023
  • 6 replies
  • 1791 views

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.

 

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 joerghoh

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

6 replies

Kiran_Vedantam
Community Advisor
Community Advisor
January 25, 2023

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.

sreenu539
sreenu539Author
Level 7
January 26, 2023

Any API implementation code to look at?

Anudeep_Garnepudi
Community Advisor
Community Advisor
January 26, 2023

@sreenu539 

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.

 

AG
VeenaVikraman
Community Advisor
Community Advisor
January 26, 2023
joerghoh
Adobe Employee
joerghohAdobe EmployeeAccepted solution
Adobe Employee
January 27, 2023

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

Nupur_Jain
Adobe Employee
Adobe Employee
January 28, 2023

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

joerghoh
Adobe Employee
Adobe Employee
February 4, 2023

The Sling Models documentation has been updated, and it mentions that the @scriptVariable annotation requires the adaptable to be a SlingHttpServletRequest.