Sling Annotations in AEM

In our previous post, we learned about the Sling framework and its role . And in this post, we will learn about Sling annotations. So let’s start working sling annotation used in Adobe Experience Manager

1.@Inject-: By using @inject we inject the node properties into the class properties, that match all model object properties to node properties. The specified property name is adapted using valuemap() then looked up form the Resource, then injected to the sling model(as POJO attribute). for both interface and class, it will the same.
example as:

@Model(adaptables=Resource.class)
 public interface myinterface {
@Inject
private String propertyName;
}

we can also inject the OSGI service, in that case, we just need a class name as,
@inject
private ResourceResolver resource resolver;

2.@Named-: If there is a difference b/w node properties and model properties so to differentiate them we use @Named annotation so by using this annotation we can resolve this issue.
example:

@ChildResource @Named("contactImage") 
 private Resource contactImageResource; 
 private String contactImage;
@inject @Default(intValues={1,2,3,4,5})
 privte int[] myarray;
 }

Here we inject the child resource of a resource class is adopting, but the name of child resource is not matched in the model class, we inject this by using @Named annotation.
3.@Default-: if we want to assign default value to the model property we can give the default value to String, primitive or array using @Default annotation as,

@Model(adaptables=Resource.class)
 public interface myinterface {
@Inject 2Default(values="lastName")
private String propertyName;
@inject @Default(intValues={1,2,3,4,5})
 privte int[] myarray;
 }

Here lastName is the default values for String (propertyName) or array.
4.@Required/@Optional-: In sling model by default all (POJO) properties should be required it is mandatory. but there may be some cases where they can be optional so to specify them as optional and requires we use these annotations. in case there are many optional fields we have to change the injection strategy(default strategy) in the @model annotation.
example:

@Model(adaptables=Resource.class)
 public class myclass {
 @Inject @Optional
    String path;
@Inject @Required
    String title
 }

5.@source-: if there is ambiguity b/w given injector which can be used/inject by many injectors mean many injectors can use the same injection, so in that we can specify the which injector we want to use, we can specify that injector.we can use all the specific injector in @source as:

{
 @inject @sourc("Script-bind")
 PageManager pagemanager;
 }

6.@via-: this can be used in a case where we adapt the Resource as adaptable but we want to use different resource object(can be SlingHttpServletRequest) in our model object (POJO) class, this is achieved by @via annotation, By default, this can be done using a JavaBean property of the adaptable:
Example:

 @Model(adaptables=SlingHttpServletRequest.class)
 public interface MyModel {
// will return request.getResource().getValueMap().get("propertyName", String.class)
@Inject @Via("resource")
String getPropertyName();
}

Here, we adapt a request object and inject the resource as bean property
7.@Postconstruct-: if we want to add method in sling model we can use method using @postconstruct, method name does not matter whatever it can be, only the thing that matter is which method get annotated using @postconstructs.The method get executed only when all the fields are injected using @Inject annotation means method call automatically after injecting all fields/properties.
example:

@Model(adaptables=SlingHttpServletRequest.class)
 public interface MyModel {
// will return request.getResource().getValueMap().get("propertyName", String.class)
@Inject @Via("resource")
String getPropertyName();
@postconstruct
 private void sayHello()
 {
 logger.info("hello");
 }
 }

Other annotation:

8.@Filter: we can also apply a filter on OSGI services.as
 {
@Inject
     @Filter("(paths=/bin/something)")
     private List servlets;} 


9.@Path-: it can be used with resource-path injector to specify the path of a resource.