我无法让我的Spring MVC应用程序作为OSGi包运行。这个让我很难过。
我正在尝试运行Spring 3 MVC捆绑包,但在启动时无法找到使用Spring所需的命名空间处理程序。我为org.springframework.context包含了必要的import-packages。我检查过Virgo服务器上安装了org.springframework.context.config.ContextNamespaceHandler。在服务器中启动捆绑包没有错误 - 因此我非常确定必须满足依赖关系。
这是我尝试向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 - 但是再次没有帮助。
此时,我迷失了想法。任何帮助,大受赞赏。
答案 0 :(得分: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());
}
}
答案 1 :(得分:0)
我对Spring MVC了解不多,但对于Spring,我知道有专门针对OSGi环境的SpringDM。也许你可以看看http://static.springsource.org/osgi/docs/1.2.1/reference/html/web.html#web:spring-mvc。希望它会帮助你一点点。