Thursday, 1 June 2017

Sling models - Adobe Recommended Way Of Object Binding

Sling Models
Sling Models are widely used with in AEM now. They are simple POJO classes(support both class and interface) mapped automatically with Sling Objects( resources, also request objects). Example use cases in AEM are they allow us to access jcr node property values directly into java classes. Also helps to wire the OSGi Services. Sling Models uses annotations & annotations Mark a class as adaptable via Sling Models.


Why sling models:

  • Adobe recommended Sling Models as the best practice.
  • Provides convenient injection annotations for data retrieval.
  • Pure POJO classes. Entirely annotation driven.
  • Less code to write & easy to extend from other Sling Models.
  • Quite simple setup for unit testing.
  • Flexibility of smooth integration with JSP or sightly.


Two major differences with WCM Use class and Sling Model

  1. WCM Use classes expect you to overwrite the activate() method, But Sling Models provides @PostConstruct annotation, where the init method will be called.
  2. ECM Use classes use Felix annotation @Reference to reference the OSGI service, whereas in Sling Models, you will use @Inject or @OSGiService


Below given the complete list of annotation references


  • @Model - declares a model class or interface
  • @Inject - marks a field or method as injectable
  • @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.
  • @Filter - an OSGi service filter
  • @PostConstruct - methods to call upon model option creation (only for model classes)
  • @Via - use a JavaBean property of the adaptable as the source of the injection
  • @Default - set default values for a field or method
  • @Path - only used together with the resource-path injector to specify the path of a resource


While working with AEM latest versions, do we need any update for Sling models?
You can check the version of Sling Model from AEM Console.



If you are working with AEM 6.2, download the latest Sling Models API and Implementation bundles from Sling and then manually upload them to AEM bundles console, because AEM 6.2 has Sling Models API and Implementation version 1.2. If you are workig with AEM 6.3 you dont have to do this step.

In future when Sling new major release available, you can still manually import them into your 6.3 server.( Ensure to check Adobe’s documentation for additional dependency package to be installed.)

Sample Code: Sling Model Vs Sightly

// @Model declares a model class or interface
@Model(adaptables = Resource.class) public class MyCustomClass {
// @Inject marks a field or method as injectable
// age can be empty
@Inject @Optional
private String age;
// we expected always an firstName-property
@Inject
private String firstName;
// OSGi Externalizer Service
@Inject
private Externalizer externalizer;

    // Executed after the class is created
    @PostConstruct  protected void init() {
     //init here  
     }

 }

How sightly calls the Sling Model class

<div data-sly-use.customClass=customsite.customproject.MyCustomClass>
${ customClass.firstName }
</div>

No comments:

Post a comment