从Sling/CQ中的捆绑包获取OSGi服务 [英] Getting OSGi services from a bundle in Sling/CQ

查看:90
本文介绍了从Sling/CQ中的捆绑包获取OSGi服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Day CQ.我想使用Felix控制台中可用的连接池将一些数据存储在SQL Server DB中.我可以通过JSP来做到这一点,方法是使用defineObjects标记中定义的SlingScriptHelper类型的"sling"对象

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" object of type SlingScriptHelper defined in the defineObjects tag

sling.getService(DataSourcePool.class).

但是,我想使用在OSGi捆绑软件中创建的servlet来处理来自客户端的请求.该servlet没有defineObjects标记,因此未定义"sling"对象.我看不到在servlet中创建有效的SlingScriptHelper对象的方法,但看来这是必须的.

However, I want to use a servlet created in an OSGi bundle to handle requests from the client. The servlet doesn't have a defineObjects tag, so the "sling" object is not defined. I don't see a way to create a valid SlingScriptHelper object in my servlet, but it seems like it has to be possible.

有办法吗?

推荐答案

要从Java OSGi组件获取服务,您不需要SlingScriptHelper,可以使用BundleContext.getService(...)方法,也可以使用使用SCR注释使SCR将服务注入到您的组件中.

To get a service from a java OSGi component you don't need the SlingScriptHelper, you can either use the BundleContext.getService(...) method, or use SCR annotations to let SCR inject the service in your component.

作为示例,您可以查看Sling的 Slingbucks中的某些组件如何样本使用SCR注释,

As an example, you can look at how some components in Sling's Slingbucks sample use SCR annotations, the ConfirmedOrdersObserver class for example gets the SlingRepository in this way:

   @Reference
   private SlingRepository repository;

请参见 http://felix.apache.org/site /apache-felix-maven-scr-plugin.html 用于处理这些注释的Maven插件.

See http://felix.apache.org/site/apache-felix-maven-scr-plugin.html for the Maven plugin that handles these annotations.

这篇关于从Sling/CQ中的捆绑包获取OSGi服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆