Adobe AEM / OSGI:如何从任何类访问OSGI服务?

问题描述 投票:0回答:3

我有一个服务(位于核心/服务内)和服务实现(位于核心/服务/ impl)。

我有一个扩展com.adobe.cq.sightly.WCMUsePojo的现有类(位于core / impl / view / components内)。使用getSlingScripterHelper,这个类可以接受我上面提到的服务。

我试图在不使用WCMUsePojo的情况下访问该服务。我该怎么做?

谢谢!

osgi aem sling
3个回答
3
投票

您可以直接从服务注册表获得服务 -

    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
投票

您可以使用@Reference从任何其他类调用服务,而无需使用WCMUsePojo。

class MyClass
{
    @Reference
    private MyService myService;

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

0
投票

如果要从HTL脚本支持bean访问服务,可以使用Sling Model(而不是WcmUsePojo)并使用@Inject注释将引用注入服务。

© www.soinside.com 2019 - 2024. All rights reserved.