Creating Your First Sample Component Using Sling Model

Aasishjain
2 min readFeb 19, 2021

--

Sling Models allows us to map resource properties, assign default values, inject OSGi services and much more.

For AEM 6.3+, we don’t need to install the Sling Models API and Implementation bundle. But we need to make sure that the bundle version of Sling Models API present in the AEM matches with the version declared in the POM file.

Below is a sample java code:

Let us understand the code of the class. The class is annotated with @Model and the adaptable class.

Fields which need to be injected are annotated with @Inject.

@PostConstruct annotation can be used to add methods which are invoked upon completion of all injections.

@Injectfields are assumed to be required. To mark them as optional, we use @Optional.If a majority of @Inject fields are optional, it is possible to change the default injection strategy by using adding defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL to the @Modelannotation

Below is the HTML file for the component:

I hope you enjoyed this post !

--

--