2

我无法让我的 Spring MVC 应用程序作为 OSGi 包运行。这个完全把我难住了。

我正在尝试运行 Spring 3 MVC 包,但在启动时找不到使用 Spring 所需的命名空间处理程序。我已经为 org.springframework.context 包含了必要的导入包。我检查了 org.springframework.context.config.ContextNamespaceHandler 是否安装在 Virgo 服务器上。在服务器中启动捆绑包没有错误 - 所以我很确定必须满足依赖关系。

这是我尝试向 MVC 调度程序 servlet 发出请求时遇到的异常;-

org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]; nested exception is org.springframework.beans.FatalBeanException: NamespaceHandler class [org.springframework.context.config.ContextNamespaceHandler] for namespace [http://www.springframework.org/schema/context] not found; nested exception is java.lang.ClassNotFoundException: org.springframework.context.config.ContextNamespaceHandler not found from bundle [com.osgi-test.spring-mvc-bundle]
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:412)
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
org.eclipse.gemini.blueprint.context.support.OsgiBundleXmlApplicationContext.loadBeanDefinitions(OsgiBundleXmlApplicationContext.java:170)
org.eclipse.gemini.blueprint.context.support.OsgiBundleXmlApplicationContext.loadBeanDefinitions(OsgiBundleXmlApplicationContext.java:140)
org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:467)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:397)
org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext.access$301(AbstractDelegatedExecutionApplicationContext.java:60)
org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext$1.run(AbstractDelegatedExecutionApplicationContext.java:168)
org.eclipse.gemini.blueprint.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85)
org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext.normalRefresh(AbstractDelegatedExecutionApplicationContext.java:164)
org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext$NoDependenciesWaitRefreshExecutor.refresh(AbstractDelegatedExecutionApplicationContext.java:78)
org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext.refresh(AbstractDelegatedExecutionApplicationContext.java:157)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
javax.servlet.GenericServlet.init(GenericServlet.java:244)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
  org.eclipse.virgo.web.tomcat.support.ApplicationNameTrackingValve.invoke(ApplicationNameTrackingValve.java:33)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
java.lang.Thread.run(Thread.java:722)

捆绑包中的清单文件如下所示;-

Manifest-Version: 1.0
Export-Package: com.osgi-test.external;version="0.0.1.SNAPSHOT"
Bundle-ClassPath: .,WEB-INF/classes
Built-By: seth
Tool: Bnd-1.50.0
Bundle-Name: spring-mvc-bundle
Created-By: Apache Maven Bundle Plugin
Build-Jdk: 1.7.0_06
Bundle-Version: 0.0.1.SNAPSHOT
Bnd-LastModified: 1347110247719
Bundle-ManifestVersion: 2
Bundle-SymbolicName: com.osgi-test.spring-mvc-bundle
Import-Package: com.osgi-test.external,javax.servlet.http;version="
[2.4,3)",org.eclipse.virgo.web.dm;version="[3.0,4)",org.springfram
ework.context;version="[3.0,4)",org.springframework.stereotype;ver
sion="[3.0,4)",org.springframework.validation;version="[3.0,4)",or
g.springframework.web;version="[3.0,4)",org.springframework.web.bi
nd.annotation;version="[3.0,4) ",org.springframework.web.servlet;v
ersion="[3.0,4)"
Archiver-Version: Plexus Archiver

我读了一篇文章,建议使用

org.eclipse.virgo.web.dm.ServerOsgiBundleXmlWebApplicationContext

http://www.eclipse.org/forums/index.php/m/635463/

可能会有所帮助-但是对我没有任何影响。

我也尝试过删除 web.xml 中的 ContextLoaderListener - 但同样没有帮助。

在这一点上,我迷失了想法。任何帮助,非常感谢。

4

2 回答 2

3

http://thomaskratz.blogspot.in/2012/03/osgi-spring-dispatcherservlet.html

请检查此站点,我认为 spring 直接注册调度程序 servlet 存在问题。但是,如果您使用上述博客中提到的解决方法,它应该可以工作。

我实际上是在添加我在这里尝试过的代码片段以供参考:

将以下内容添加到插件的 Activator 中:

Activator.context = bundleContext;

ServiceReference ref = context.getServiceReference(HttpService.class.getName());
HttpService service = (HttpService) context.getService(ref);

DispatcherServlet servlet = new DispatcherServlet();

Dictionary<String, String> initparam = new Hashtable<String, String>();
initparam.put("contextConfigLocation", "/WebContent/WEB-INF/spring-servlet.xml");
initparam.put("contextClass", "spring3osgi.BAC");

ClassLoader loader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());

// this is to register the controller
service.registerServlet("/Spring3OSGi",  servlet, initparam, null);

// provide alias to the WebContent folder
service.registerResources("/Spring3OSGiWebContent", "/WebContent", service.createDefaultHttpContext());

创建班级 BAC

import org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext;

public class BAC extends OsgiBundleXmlWebApplicationContext {

    public BAC() {
        super();
        setBundleContext(Activator.getContext());

    }

}
于 2012-12-13T12:39:42.473 回答
0

我对 Spring MVC 了解不多,但对于 Spring,我知道有专门用于 OSGi 环境的 SpringDM。也许你可以看看http://static.springsource.org/osgi/docs/1.2.1/reference/html/web.html#web:spring-mvc。希望它会对你有所帮助。

于 2012-09-09T11:58:19.337 回答