Use of Sling Framework in Aem

Most of the people are confused about the role of Sling in aem. let’s try to understand this.
Like simple java POJO classes that provide Object properties/functionality to java classes that work as an entity class suitable for working with ORM like Hibernate.
In Abode Experience Manager to achieve this functionality, we use the Sling Framework and like simple java POJO class, we used the Sling model. And to provide the modularity to the Application Aem uses the OSGI bundle concept. which enables us to deploy our web-application as bundles, actually AEM architecture defines all these things clearly.

Sling models are pure POJO which automatically mapped the sling model properties with JCR node properties using some kind of injector annotations by injecting properties we mapped the properties with node properties from the sling objects, typically resources and request objects. The adapter factory is used to map our sling model (POJO) to sling object (Resource) which is generally mapped after injecting the POJO properties using @Inject annotations. We can also use the other sling OSGI services in the model (POJO). So Sling bundle models allow us to map the Sling model to sling objects. So let’s try to understand need or uses of Sling in Adobe Experience Manager.
why the sling model?
1.simple java plain old java objects (Pojo).
2.sling model uses the standard sling annotations.
3.plugable-due to annotations are pluggable i.e used in the model to work with any other OSGi annotations or customizable annotations.
4.OOTB, support for Resource properties using value map, sling binding, OSGi services, request attributes.
5.sling annotation can work for both classes and interface.
6.sling annotations can work with existing sling infrastructure there is no need to change them in order to work with new annotations.

we can adopt multiple resources using single annotation using minimum resources like @Model(adaptables=Resource.class, SlingHttpServletRequest.class) But before getting starting with Sling we have to add dependencies in pom.xml, which provides support for Sling Framework classes and methods.
Dependencies used for sling objects:
org.apache.sling org.apache.sling.models.API 1.2.2 provided 1.0
org.apache.geronimo.specs geronimo-atinject_1.0_spec provided


The first dependency is for Sling Model API and the second one is used for annotations.here is the simple example:

 @Model(adaptables = Resource.class) 
 public class TestModel {      
 @Inject          
 Resource resource;
 public String getPath() 
{     
 return resource.getPath();
    }   
} 


Let us explain the above example step by step:
by using @Model annotation we declare our class will be a model object and inside the model we define the adaptable class, here we adopt the Resource.class which means, we adapt an instance of Resource to the TestModel instance. This can be adapt using adaptTo() method as resource.adaptTo(TestModel.class).
By using @inject we inject the node properties into the class properties, that match all model object properties to node properties.
construction injection is also available for adaptable as:

@Model(adaptables=Resource.class)  
public class MyModel
 {
 public MyModel(Resource resource) 
{  
   this.resource = resource;
 } 
private final Resource resource; 
@Inject private String propertyName;
 }


when a resource is adaptable to a model class, it’s child resource is automatically adopts, as:

@Model(adaptables=Resource.class)
public interface MyModel 
{
 @Inject ImageModel getImage();
 }
 @Model(adaptables=Resource.class) 
 public interface ImageModel
 {
 @Inject String getPath(); 
}