We all end up creating a Java class for any data manipulation or for some business logic. So let’s understand how we can use Sling Models for writing less Java code. Less code means fewer unit tests to cover because you have much less coverage to hit. And leveraging sling model helps us in better organization and modularization of Java code.
Before going into how we can leverage Sling Models to keep our code modular and clean you can also refer my article on using Project Lombok in combination of Sling Models implementation.
Leveraging Sling Model Framework:
Sling Models are annotation driven java bean classes which allow us to access JCR node property values directly into java classes via annotations. Sling Models supports both class and interface.
We all know the simple use-cases but let’s what goes on in the background.
Let’s take the use-case of AEM Granite Multifield, As it’s the one of the widely used field across AEM components. Let’s understand below points while going through it’s implementation using Sling Models.
Understanding the injectors and how to leverage them.
Replacing legacy code of extending multifield into simpler Sling Model method.
Default Injection strategy and annotations.
We can consider below cq:dialog as an example. It has a text and multifield. The multifield is a composite of text and path fields.