如何使用 aries 蓝图将 apache karaf 包作为服务注入到 Web 应用程序中? [英] How to inject apache karaf bundles as a service in the web application using aries blue print?

查看:24
本文介绍了如何使用 aries 蓝图将 apache karaf 包作为服务注入到 Web 应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have servlet web application and want to inject apache karaf bundles as a service in the web application using aries blueprint.

这些是注入包的步骤:

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

1) added reference tag with id and interface values in the blueprint.xml sample code is here

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

2) 添加带有 ref 属性作为引用 id 的 bean 标记,我们在 blueprint.xml 文件中注入的包.示例代码在这里

2) added bean tag with ref attribute as reference id, of bundles what we are injecting in the blueprint.xml file. sample code is here

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

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

3) blueprint.xml file location as context-param tag the web.xml. sample code is here

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

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

4) added listner class in the web.xml. sample code is here

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

5) @Inject 注解用于在 servlet 类中注入特定包.示例代码在这里

5) @Inject annotation for injecting the particular bundle in the servlet class. sample code is here

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

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

仍然没有注入捆绑包,请有人可以帮助解决这个问题吗?

Still the bundles are not injected please some one can help on this ?

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

how to inject these karaf bundles as a service to the servlet application?

推荐答案

我在没有使用aries蓝图的情况下解决了上述问题.

I solved the above problem without using aries blueprint.

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

The following method i added in the servlet to get injected bundles, i will send the bundle name as serviceName parameter to the below method, this will send back the required service of injected bundle, by using that i will use the methods present in that service.

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);
    }

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

This code solved my problem.

这篇关于如何使用 aries 蓝图将 apache karaf 包作为服务注入到 Web 应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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