@AniketSi
The @PostConstruct annotation essentially serves as an alternative to a constructor. If you were to employ a constructor in your model, you would observe that, upon its execution, the fields annotated with @Inject are still unassigned. Attempting further initialization with these fields at this point would result in a NullPointerException.
The entire sequence unfolds as follows:
- Sling generates a new instance of your model (e.g.,
new MyModel()
).
- Sling populates all the dependencies you’ve declared using the @Inject annotation.
- Sling proceeds to invoke the method annotated with @PostConstruct.
Sling model annotations: Basics – Adobe Experience Manager Blog (techrevel.blog) (Refer to PostConstruct Annotation section)
Aanchal Sikka