Using Sling Models to write Less Java Code | AEM Community Blog Seeding

Avatar

Avatar
Establish
Community Manager
kautuk_sahni
Community Manager

Likes

1,206 likes

Total Posts

6,421 posts

Correct reply

1,148 solutions
Top badges earned
Establish
Coach
Originator
Contributor 2
Contributor
View profile

Avatar
Establish
Community Manager
kautuk_sahni
Community Manager

Likes

1,206 likes

Total Posts

6,421 posts

Correct reply

1,148 solutions
Top badges earned
Establish
Coach
Originator
Contributor 2
Contributor
View profile
kautuk_sahni
Community Manager

22-06-2021

BlogImage.jpg

Using Sling Models to write Less Java Code by Nikhil Kumar

Abstract

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.

Read Full Blog

Using Sling Models to write Less Java Code

Q&A

Please use this thread to ask the related questions.