如何使用 aries blue print 将 apache karaf 包作为服务注入到 Web 应用程序中?

How to inject apache karaf bundles as a service in the web application using aries blue print?

我有 servlet web 应用程序,想使用 aries 蓝图将 apache karaf 包作为服务注入到 web 应用程序中。

这些是注入包的步骤:

1) 在 blueprint.xml 中添加了带有 id 和接口值的引用标签 示例代码在这里

<reference id="jsonStore" interface="com.test.test.jsonstore.service.JsonClientStore" />

2) 添加了带有 ref 属性作为引用 id 的 bean 标签,用于捆绑我们在 blueprint.xml 文件中注入的内容。 示例代码在这里

<bean id="echo" class="com.test.test.core.jsonrequest.JsonServlet">
     <property name="jsonStore" ref="jsonStore"/>
   </bean>

3) blueprint.xml 文件位置作为上下文参数标记 web.xml。 示例代码在这里

<context-param>
      <param-name>blueprintLocation</param-name>
      <param-value>OSGI-INF/blueprint/blueprint.xml</param-value>
    </context-param>

4) 在 web.xml 中添加了监听器 class。 示例代码在这里

<listener>
      <listener-class>org.apache.aries.blueprint.web.BlueprintContextListener</listener-class>
    </listener>

5) @Inject 注释用于在 servlet 中注入特定的包 class。 示例代码在这里

@Inject(ref="jsonStore")
    JsonClientStore jsonStore = null;

以下link为参考文档 http://aries.apache.org/modules/blueprintweb.html

仍然没有注入包,请有人帮忙解决这个问题?

如何将这些 karaf 包作为服务注入 servlet 应用程序?

我在没有使用白羊座蓝图的情况下解决了上述问题。

我在 servlet 中添加了以下方法来获取注入包,我会将包名称作为 serviceName 参数发送到下面的方法,这将发回注入包所需的服务,通过使用我将使用该服务中存在的方法。

private <T> T getService(Class<T> serviceName) {
        Bundle bundle = FrameworkUtil.getBundle(serviceName);
        if ( bundle == null ) {
            return null;
        }       
        BundleContext bundleContext = bundle.getBundleContext();
        ServiceReference serviceRef = bundleContext.getServiceReference(serviceName);  
        if (serviceRef == null)          
            return null;
        return (T) bundleContext.getService(serviceRef);
    }

这段代码解决了我的问题。