March 16, 2020
Estimated Post Reading Time ~

Injector specific Annotations

Injector-specific annotations are used to reduce the overall internal execution work as they specify the source explicitly. that also reduces the overall execution time.
there are lots of injector specif annotations for different purposes which can be seen inside the Sling Adaptor in your Adobe Experience Manager Felix console, only when your class is working as a Sling model.

1.@ScriptVariable-can be used to get page manager, current page, design, page properties etc. this injector adaptable the request object by adapting the SlinghttpServletRequest.If It has two attributes:
a).name-specify the name of the resource as a parameter.
b).injectionStrategy: that defines which kind of strategy should be used while adapting the request object, which can be Default, Optional, or Required.
let us understand it with an example:
@Model(adaptables = SlingHttpServletRequest.class)
public class TestModel {
// Injects currentPage using ScriptVariable annotation
@ScriptVariable(name="currentPage")
Page page;

public String getPagePath() {
return page.getPath();
}
}

Note: name must be defined to inject the script resource if not set it will set the name from the method or field name.
here we get the current page and would be used in sightly(we use this in the example) or JSP page.
2.childResource:-it’s adoptable is a resource and used to get the child resource of the adaptable resource. Child resource can be get using the name
it has three attributes.
a).name-specify the name of the resource as a parameter.
b).injectionStrategy-that define which kind of strategy should be used while adapting the request object, which can be Default, Optional, or Required.
c).via- the adaptable is a resource and via is used to tell the property should be fetched using via (means the property should be fetched as via(resource specified))
the property should be fetched bypassing resource specified property
Here is an example:
@Model(adaptables = SlingHttpServletRequest.class)//adaptable as request
public class TestModel {
// Injects the child of the resource using ChildResource annotation
@ChildResource(name="content",injectionStrategy= InjectionStrategy.OPTIONAL,via = "resource")//child resource should be fetch via resource
Resource child;

public String getChildPath() {
return child.getPath();
}
}

3.@ValueMapValue-It is adaptable to resources and used to directly inject the resource property in the sling model. if the name is not set it is automatically derived from the method or field name, even if via is not set, it will automatically take resource if the adaptable is the SlingHttpServletRequest.
The attributes of the ValueMapValue is:
1.name
2.injectionStrategy
example:-
@Model(adaptables = SlingHttpServletRequest.class)
public class Test {
// Injects title from ValuMap annotation using its attributes
@ValueMapValue(name = "title",via = "resource",injectionStrategy = InjectionStrategy.REQUIRED)
String pageTitle;
public String getTitle() {
return pageTitle;
}
}

4.@ResourcePath:-inject a resource by resource path or by reading a property with the given name. You can directly inject a path as a resource using this annotation.
The attributes of the ResourcePath annotation is:
1.name
2.injectionStrategy
3.path
4.paths[]
@Model(adaptables = Resource.class )
public class TestModel {
//directly inject a path as a resource
@ResourcePath(path = ”/etc/social”)
Resource pathResource;
@ResourcePath(name = "path")//inject the resource using resource property by name (if any)
Resource resourcePath;
@ResourcePath(paths = {"/etc/social","/etc/tags"})
Resource[] paths;
}

5.@OSGiService-we can inject the OSGI service using this annotation will inject the OSGI service by type. filters can be used to inject the OSGI service.
attribute:-
1.injectionStrateg
2.filter

6.@slingObject:-Injects commonly used sling objects if the field matches with the class: request, response, resource resolver, current resource, SlingScriptHelper.
attribute:
1.injectionStrategy
example:-
@Model(adaptables = SlingHttpServletRequest.class)
public class TestModel {
@SlingObject
Resource resource;

@SlingObject
ResourceResolver resourceResolver;
}

7.@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
attribute:
1.injectionStrateg
example:
@Model(adaptables = SlingHttpServletRequest.class)
public class SelfExampleModel {
@Self
Node node;
public String getNodePath() throws RepositoryException {
return node.getPath();
}
}

8.@RequestAttribute:-if there is an input parameter and we need this parameter while initializing the sling model, we can use using RequestParameter annotation.
The attributes of RequestAttribute annotation are:
1.name
2.injectionStrategy
@Model(adaptables = SlingHttpServletRequest.class)
public class TestModel {
//Injects all the input parameters from sightly injects as RequestAttribute in Sling Models
@RequestAttribute(name = "color")
String param;

public String getParam() {
return param;
}
}

The sightly script calls the Sling Model using input parameter :${example.param}



By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.