...
Once the injector is created in your activator, it must be stored somewhere, so that it can be reused to provide models for you components. Thanks to this, there will be no need to recreate injector for every subsequent request. The place where created and configured injectors live is called Injectors Repository.
...
<slice:lookup>
allows you to specify which injector to choose (using its appName
property). However, if you leave it blank, the tag will try to detect the application name. It's done by going up in the content tree and looking property called injectorName
. If no injectorName
property has been found, the appName
is computed basing on the current resource path - it is part of path directly after apps. For /apps/testApp/...
it will return testApp
. Therefore, it is suggested to call the injector after the name of the application in the content structure. Additionally, the name of the app (if not defined) is being found only once per request and then cached (to avoid content traversing). If an application uses various app names at the same time, the appName attribute must be explicitly defined so that a proper app name is selected
Execution stack
Execution stack is something very internal in Slice - you will never ever see it in action. However, it's really worth knowing that Slice is based on it and what exactly you can achieve thanks to it.
Every call of ModelProvider#get(class, resource)
within a single request causes that a resource (or path) passed as a parameter is pushed to the stack of current execution, then the guice injector is called, and after the object has been created the resource is popped from the stack. This resource is available for all other classes as a so called current resource and can also be injected to any object. In the below example the resource injected through constructor is just the current resource - the resource from the top of the execution stack. It should be noted that at the beginning of execution (most usually in <slice:lookup>
), the resource from sling request (request.getResource()
is passed to ModelProvider
(exactly as it is described in here).
Code Block | ||||
---|---|---|---|---|
| ||||
public class SimpleModel { private final String text; @Inject public SimpleModel(final Resource resource) { ValueMap valueMap = resource.adaptTo(ValueMap.class); text = valueMap.get("text"); } ... } |
...
Info |
---|
In above examples we injected resource and then adapted it to |