Showing posts with label Sling Models. Show all posts
Showing posts with label Sling Models. Show all posts

Friday, 2 June 2017

Sling Models in AEM

We used to develop back-end login to components either by using WCMUse or WCMUsePojo or even JSP. With the release of AEM 6.3 and AEM Core WCM Components, we see that using Sling Models have been advocated by Adobe as the best practice. Now let’s take a look how we can use Sling Models.


You can work with Sling Models when developing with Adobe Experience Manager (AEM). That is, when developing an AEM project, you can define a model object (a Java object) and map that object to Sling resources. For more information, see Sling Models.
A Sling Model is implemented as an OSGi bundle. A Java class located in the OSGi bundle is annotated with @Model and the adaptable class (for example, @Model(adaptables = Resource.class). The data members (Fields) use @Inject annotations. These data members map to node properties.

Monday, 20 February 2017

javax.inject,version=[0.0,1) -- Cannot be resolved

Sling Models is a way to create model objects which are automatically mapped from Sling objects, typically resources but also request objects.
This article explains how to use sling models in AEM. But, If you want to use Sling models with AEM 6.2, you might encounter some dependency issues.
Recently I faced a problem when my AEM project stopped working. The project was created using Maven archetype 10. It was working fine until I was on AEM 6.1. When I tried deploying it to AEM 6.2, the bundle was not coming to Active state. Later while debugging, I found out that it was not able to find a particular dependency. I was seeing something like below in the console.
javax.inject,version=[0.0,1) -- Cannot be resolved
Solved by adding this line in yourproject.core pom file
(with in the maven-bundle-plugin)
<Import-Package>javax.inject;version=0.0.0,*</Import-Package>

Here is the plugin after adding that line


<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <extensions>true</extensions>
    <configuration>
    <instructions>
    <!-- Import any version of javax.inject, to allow running on multiple versions of AEM -->
    <Import-Package>javax.inject;version=0.0.0,*</Import-Package>
       <Sling-Model-Packages>
             com.yourproject.core
        </Sling-Model-Packages>
      <Export-Package>com.yourproject.core.*</Export-Package>
     </instructions>
     </configuration>
</plugin>