0

我添加了以下捆绑包,可以将战争(Web 应用程序)部署到 OSGi 框架中。我使用“Apache Felix 可分发二进制文件”:

   32|Active     |   20|OPS4J Pax Logging - API (1.7.0)
   33|Active     |   20|OPS4J Pax Logging - Service (1.7.1.SNAPSHOT)
   34|Active     |   20|OPS4J Pax Url - mvn: (1.3.5)
   35|Active     |   20|OPS4J Pax Url - war (1.3.5)
   36|Active     |   20|OPS4J Pax Url - wrap: (1.3.5)
   37|Active     |   20|OPS4J Pax Web - API (1.1.12)
   38|Active     |   20|OPS4J Pax Web - FileInstall Deployer (1.1.12)
   39|Active     |   20|OPS4J Pax Web - Extender - WAR (1.1.12)
   40|Active     |   20|OPS4J Pax Web - Extender - Whiteboard (1.1.12)
   41|Active     |   20|OPS4J Pax Web - Jetty Bundle (1.1.12)
   42|Active     |   20|OPS4J Pax Web - Jsp Support (1.1.12)
   43|Active     |   20|OPS4J Pax Web - Runtime (1.1.12)
   44|Active     |   20|OPS4J Pax Web - Service SPI (1.1.12)

因此,我使用了一个简单的 struts 2 应用程序(Hello world)进行测试,我创建了战争,然后将其放在“文件安装”目录中,它变得活跃,我可以看到我的欢迎页面。
在此之后,我尝试检索 BundleContext 以使用战争中注册的服务,我添加了:

BundleContext ctx = (BundleContext) ServletActionContext.getServletContext().getAttribute("osgi-bundlecontext");

但是,我发现了异常:

“java.lang.ClassCastException:org.apache.felix.framework.BundleContextImpl 无法转换为 org.osgi.framework.BundleContext”

可能是什么问题?

谢谢

4

1 回答 1

0

从评论中的讨论中做出回答:

您在类路径中有两次提到的类。存在于 OSGi 容器中的和包含在 jar 文件中的。PAX 模块从 OSGi 容器导入类,而您的 web 应用程序从外部 jar 文件导入它。

在您的情况下,jar 包含在 WEB-INF/lib 中。当您使用 Maven 时,您应该将 osgi-core 依赖项的范围设置为提供。

于 2013-05-22T09:33:01.883 回答