Getting OSGi services from a bundle in Sling/CQ

前端 未结 3 1617
野趣味
野趣味 2021-02-08 04:21

I am using Day CQ. I want to store some data in a SQL Server DB, using the connection pool available in the Felix console. I can do this from a JSP, by using the \"sling\" objec

3条回答
  •  长发绾君心
    2021-02-08 04:48

    You can use the BundleContext to get to the Service, by using the #getServiceReference and #getService methods. For example, if you were interested in the ResourceResolverFactory, you could get it like so:

    BundleContext bundleContext = FrameworkUtil.getBundle(MyClass.class).getBundleContext();
    ServiceReference factoryRef =
         bundleContext.getServiceReference(ResourceResolverFactory.class.getName());
    ResourceResolverFactory resolverFactory = 
        (ResourceResolverFactory) bundleContext.getService(factoryRef);
    

提交回复
热议问题