0
votes

I have a service (located inside core/services) and service implementation (located core/services/impl).

I have an existing class (located inside core/impl/view/components) that extends com.adobe.cq.sightly.WCMUsePojo. Using getSlingScripterHelper, this class can acceess the service I mentioned above.

I'm trying to access the service without using WCMUsePojo. How can I do it?

Thanks!

3
I am curious, Why do you not want to extend wcmusepojo?user8723658

3 Answers

3
votes

You can get a service directly from service registry-

    final Bundle bundle = FrameworkUtil.getBundle(this.getClass());
    final BundleContext bundleContext = bundle.getBundleContext();
    ServiceReference<MyService> ref = bundleContext.getServiceReference(MyService.class)
    MyService myService = bundleContext.getService(ref);
    // use the service
    bundleContext.ungetService(ref);
2
votes

You can use @Reference to call a service from any other class without using WCMUsePojo.

class MyClass
{
    @Reference
    private MyService myService;

    void myMethod()
    {
      myServie.callYourServiceMethod();
    }
}
0
votes

If you want to access the service from an HTL script backing bean you can use a Sling Model (instead of WcmUsePojo) and inject the reference to your service using the @Inject annotation.