Posts

Sling Models with Sightly Part – III (Key Annotations – I)

The title of this blog is “Key Annotations – I” because In this post, I will explain two important annotations related to Sling Models and continue explaining other annotations in my coming posts. So for explaining two annotations, I have selected following two questions for this post.

1). How to include OSGI Services in Sling Model?
2). What is the use of @ResoucePath annotation?

I am assuming that you are using the latest versions of Sling Model APIs that I described in my previous blog.
For Answering first question, I have created an OSGi Service that have a dummy method as follows-

Now, You can include OSGi Services in your Sling Models using two annotations, these are-

1. @OSGiService Annotations
2. @Inject Annotation

Here is the Sling Model class, that shows how to use these annotations?

When you call this Model class then you will see that both of these two annotations working in the same manner.

Q1. What is the use of @ResoucePath annotations?
@ResoucePath annotation is a very handy annotation provide by Sling and using this annotation, you can convert a path into its resource object without writing any code. Let’s suppose you have a predefined path (e.g. /content/geometrixx/en) of the resource and want to convert that path into resource object then you can use @ResourcePath annotation.

Q2. How to use @ResoucePath annotation?
Here the code snippet that shows you the use of this annotation-

Q3. How am I testing these annotations?
I have created a dummy component and that component calls these Sling Model classes. Sightly code snippet is-

For complete working code, I am sharing the Git repository link.
https://bitbucket.org/accunitysoft/accunity-blog-snippets

Happy Coding..!!

Ankur Chauhan
Tech Lead