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-
1 2 3 4 5 6 7 8 9 10 | @Component(immediate = true,enabled = true,metatype = false) @Service (DemoService.class) public class DemoService { private Logger logger = LoggerFactory.getLogger(DemoService.class); public void dummyMethod(){ logger.info("Inside Dummy Method, its working fine. "); } } |
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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | @Model(adaptables = Resource.class) public class ServiceInjector { @Inject @Default(values = "Ankur Chauhan") private String firstName; @Inject @Source("osgi-services") DemoService demoService; @OSGiService DemoService demoServiceX; @PostConstruct public void activate(){ demoService.dummyMethod(); demoServiceX.dummyMethod(); } public String getFirstName() { return firstName; } } |
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-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | @Model(adaptables = Resource.class) public class ServiceInjector { @Inject @Default(values = "Ankur Chauhan") private String firstName; @Inject @ResourcePath(path = "/content/geometrixx/en") Resource tempRes; @PostConstruct public void activate(){ System.out.println( tempRes + " = Resource Path"); } public String getFirstName() { return firstName; } } |
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-
1 2 3 | <div data-sly-use.serviceInjector="sling.models.ServiceInjector"> ${serviceInjector.firstName} </div> |
For complete working code, I am sharing the Git repository link.
https://bitbucket.org/accunitysoft/accunity-blog-snippets
Happy Coding..!!
Ankur Chauhan
Tech Lead