Tuesday, 13 February 2018

WCMUsePojo class & Alternatives in AEM?

AEM’s component development needs a back end logic to retrieve values from back end. Sightly is a templating language which together with WCMPojo helps to create components.

This approach(Sightly + WCMUseClass) provided better decoupling of the presentation layer vs business logic, thus code will be more maintainable and also easier to debug. AEM 6.1 or 6.2 uses WCMUsePojo class (or even Sling Models) for back end logic. Adobe recommends Sling Models as a best way of implementing AEM WCM Components with version AEM 6.3.

So in Brief,

We can use simple Pojo (without extending Adobe's class) with java-use-api. Cases where resources/services are not easily available, we can extend WCMUsePojo to get the ability to use resources/services. In an advanced way, we can go with Sling Models which will give more flexibility and ease, because it uses annotations.

Important Sling Annotation Reference are given below.

@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 : explicitly 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 : change 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

@Exporters/@Exporter/@ExporterOptions/@ExporterOption : for Exporter Framework

WCMUsePojo Vs Sling Models

  • Mixed POJOs - Pure POJOs
  • Extends from WCMUsePojo - Standalone class with '@Model' annotation and having no keyword
  • More code required to retrieve common objects or property values - Easier methods to retrieve common objects or property values
  • Uses  Felix annotation '@Reference' to reference to an available OSGI service - Uses '@Inject' or '@OSGiService'
  • In case of WCMUsePojo, we have to overwrite the activate() method - init() method will be called in the @PostConstruct annotation
  • Not annotation driven - Annotation driven

No comments:

Post a Comment