May 26, 2020
Estimated Post Reading Time ~

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 the 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.

For Answering the first question, I have created an OSGi Service that has a dummy method as follows-

@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, it is 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?

@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 a 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-

@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-

<div data-sly-use.serviceInjector="sling.models.ServiceInjector">
   ${serviceInjector.firstName}
</div>

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


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.