【问题标题】:How to inject apache karaf bundles as a service in the web application using aries blue print?如何使用 aries blue print 在 Web 应用程序中将 apache karaf 包作为服务注入?
【发布时间】:2019-01-26 13:43:28
【问题描述】:

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

这些是注入捆绑包所遵循的步骤:

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

<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 中添加了侦听器类。 示例代码在这里

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

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

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

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

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

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

【问题讨论】:

  • 您使用的是什么版本的 Karaf?启动捆绑包时,您是否在日志中看到任何错误?
  • 感谢您的回复,我使用的是 karaf vesion 4.2.0 并尝试在 servlet 应用程序中注入依赖项,我检查了 karaf 日志,没有提到捆绑包的错误。我在上述步骤中遗漏了什么吗?
  • 如果您没有看到任何错误,我认为您的捆绑包没有启动。是否有捆绑发布com.test.test.jsonstore.service.JsonClientStore 服务?

标签: java apache-karaf blueprint-osgi aries


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多