当没有互联网连接时,Spring schemaLocation失败

我正在使用Spring,并在application-context.xml我有以下定义:

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:security="http://www.springframework.org/schema/security" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd" > 

…..

当我的互联网连接丢失时,我无法通过tomcat或jetty运行我的应用程序。

它给:

 [main] WARN org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Ignored XML validation warning org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document '/spring-beans-2.0.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>. at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195) at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.warning(ErrorHandlerWrapper.java:96) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:380) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318) at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaErr(XSDHandler.java:2541) at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaWarning(XSDHandler.java:2532) at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getSchemaDocument(XSDHandler.java:1836) at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:531) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:552) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.findSchemaGrammar(XMLSchemaValidator.java:2408) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1753) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:685) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:626) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3095) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:921) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107) at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:225) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:283) at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149) at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124) at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:92) at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123) at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:423) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:353) at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45) at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:548) at org.mortbay.jetty.servlet.Context.startContext(Context.java:136) at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1250) at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:517) at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:467) at org.mortbay.jetty.plugin.Jetty6PluginWebAppContext.doStart(Jetty6PluginWebAppContext.java:115) at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50) at org.mortbay.jetty.handler.HandlerCollection.doStart(HandlerCollection.java:152) at org.mortbay.jetty.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:156) at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50) at org.mortbay.jetty.handler.HandlerCollection.doStart(HandlerCollection.java:152) at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50) at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130) at org.mortbay.jetty.Server.doStart(Server.java:224) at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50) at org.mortbay.jetty.plugin.Jetty6PluginServer.start(Jetty6PluginServer.java:132) at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:441) at org.mortbay.jetty.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:383) at org.mortbay.jetty.plugin.AbstractJettyRunMojo.execute(AbstractJettyRunMojo.java:210) at org.mortbay.jetty.plugin.Jetty6RunMojo.execute(Jetty6RunMojo.java:184) at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:483) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:678) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:553) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:523) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:371) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:332) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:181) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:356) at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:137) at org.apache.maven.cli.MavenCli.main(MavenCli.java:356) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315) at org.codehaus.classworlds.Launcher.launch(Launcher.java:255) at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430) at org.codehaus.classworlds.Launcher.main(Launcher.java:375) 2009-11-13 15:31:25,675 [main] ERROR org.springframework.web.context.ContextLoader - Context initialization failed org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 23 in XML document from class path resource [application-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'. at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:404) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinit........ 

任何build议如何解决它?

如果命名空间configuration正确且XSD文件位于类path中,则不需要在schemaLocation URL中使用classpath:protocol。

Spring doc“ 注册处理程序和模式 ”显示了它应该如何完成。

就你而言,问题可能是你的类path上的spring-context jar不是2.1。 这就是为什么要将协议更改为classpath:将特定的2.1 XSD放入你的classpath中解决了这个问题。

从我所看到的,有2个模式定义为包含在spring-* jar中的主要XSD。 一旦解决与该版本的架构的URL和一次没有它。

作为例子,请参阅spring-context-3.0.5.RELEASE.jar中spring.schemas内容的这一部分:

 http\://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsd http\://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsd http\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-3.0.xsd 

这意味着(在xsi:schemaLocation中)

 http://www.springframework.org/schema/context/spring-context-2.5.xsd 

将被validation反对

 org/springframework/context/config/spring-context-2.5.xsd 

在类path中。

 http://www.springframework.org/schema/context/spring-context-3.0.xsd 

要么

 http://www.springframework.org/schema/context/spring-context.xsd 

将被validation反对

 org/springframework/context/config/spring-context-3.0.xsd 

在类path中。

 http://www.springframework.org/schema/context/spring-context-2.1.xsd 

没有定义,所以Spring将使用schemaLocation中定义的文字URL来查找它。

我解决了它

 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:security="http://www.springframework.org/schema/security" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/context classpath:spring-context-2.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd" > 

classpath:spring-context-2.1.xsd是工作在离线模式(没有互联网连接)的关键。 此外,我复制spring-context-2.1.xsd附近(相同的目录)application-context.xml文件

这样的事情为我工作。

 xsi:schemaLocation= "http://www.springframework.org/schema/beans classpath:org/springframework/beans/factory/xml/spring-beans-3.0.xsd http://www.springframework.org/schema/context classpath:org/springframework/beans/factory/xml/spring-context-3.0.xsd" 

如果有人通过同一个根来到这里 – 我遇到了这个问题,因为我正在构build一个包含Spring JAR的所有依赖关系的JAR。 因此,Spring JAR的一些META-INF目录中的spring.schemas文件被覆盖。

我在这里find了build议的解决scheme: 如何用maven创build基于spring的可执行jar文件?

今天遇到类似的问题。 在我的情况下,除了springframework.org发生故障之外,它是罪魁祸首的阴影插件。 下面的代码清理了一些事情:

 <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> 

HTH的人

你应该检查spring.handlersspring.schemas文件是否在classpath中,并且有正确的内容。

这可以通过ClassLoader.getResource(..) 。 您可以在运行时环境中使用远程debugging器运行该方法。 在Spring Reference B.5中描述了可扩展的XML创作设置。 注册处理程序和模式 。

通常情况下,这些文件应该在弹簧jar(springframework.jar / META-INF /)中,并且在启动Spring时可以放在类path中。

您需要将模式位置添加到您的bean定义中,然后可以在classpath中find它们,而不是通过networking获取。 鉴于你的格式问题,我不是100%确定你没有这样做。

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- empty: the beans we use are in the base class's context. --> </beans> 

我们解决了这个问题:

 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(false); // This avoid to search schema online factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", "TransactionMessage_v1.0.xsd"); 

请注意,我们的应用程序是一个Java独立的离线应用程序。

如果你正在使用eclipse进行开发,那么如果你安装了Eclipse的STS插件[来自eclipse的特定版本的marketPlace。

现在,当您尝试在项目中的文件夹(通常是资源)中创build一个新的configuration文件时,选项将会有一个“Spring文件夹”,您可以select“Spring Bean定义文件”选项Spring> Spring Bean Configuation File。

select此选项后,当您按照步骤操作时,会要求您select名称空间和特定的版本:

所以有一个不存在的jar子或旧版本的可能性可以消除。

也会张贴图片以及,但我的声誉是相当低.. 🙁

问题在于您在应用程序中使用的JAR文件。

我所做的工作就是进入与我使用的版本相匹配的SPRING-CORE,SPRING-BEANS,SPRING-CONTEXT,SPRING-TX的JAR。 在META-INF文件夹中,我连接了所有在这些JAR中的spring.handlers和spring.schemas。

我用一块石头杀死了两只鸟,我解决了模式的问题,所以这也可以在离线模式下正常工作。

PS我尝试了SHADE和变压器的maven插件,但没有奏效。

我有这个问题。 为了后代,我的例外是:

 org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 117; schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/beans/spring-beans-4.2.xsd' 

当我意识到我的Maven pom.xml指定的spring-beans的版本是4.1.x ,我解决了这个问题,因此在类path中找不到具体的.xsd

由于我平时上网,所以被掩盖了很长时间,所以我认为脱机已经“打破”了构build。 但是这与它无关。 更新我的pom.xml以指定正确版本的spring-beans ,即4.2.x ,修复了它。

find课程path

如果你使用的是eclipse,请点击相应的jar文件。 转到 – > META-INF->打开文件spring.schemas

你会看到如下所示的线条。

http://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-3.1.xsd

复制后=和configuration豆像下面。

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/rabbit classpath:org/springframework/amqp/rabbit/config/spring-rabbit-1.1.xsd http://www.springframework.org/schema/beans classpath:org/springframework/beans/factory/xml/spring-beans-3.1.xsd http://www.springframework.org/schema/context classpath:org/springframework/context/config/spring-context-3.1.xsd http://www.springframework.org/schema/util classpath:org/springframework/beans/factory/xml/spring-util-3.1.xsd">

我想补充一些这方面的讨论。 在Windows操作系统中,我观察到,当一个包含模式的jar文件存储在一个其path包含空格字符的目录中时,例如下面的例子

 "c:\Program Files\myApp\spring-beans-4.0.2.RELEASE.jar" 

那么当您正在开发一些独立的应用程序也应该离线工作时,以下面的方式指定模式位置URL是不够的

 <beans xsi:schemaLocation=" http://www.springframework.org/schema/beans org/springframework/beans/factory/xml/spring-beans-2.0.xsd" /> 

我已经了解到,这种模式定位URLparsing的结果是一个具有如下path的文件

 "c:\Program%20Files\myApp\spring-beans-4.0.2.RELEASE.jar" 

当我从其他path中没有包含空格字符的其他目录开始我的应用程序时,架构位置parsing工作正常。 也许有人遇到类似的问题? 不过,我发现classpath协议在我的情况下工作正常

 <beans xsi:schemaLocation=" http://www.springframework.org/schema/beans classpath:org/springframework/beans/factory/xml/spring-beans-2.0.xsd" /> 

我也遇到了类似的问题。 就我而言,我的决议是完全不同的。 这是我的spring上下文xml文件:

 ... <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> ... 

我没有指定任何xsd版本,因为我想spring使用spring依赖关系内的最新的xsd版本。 我的应用程序使用的spring版本是spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE ,当我将应用程序组装到jar中时,所有spring依赖项都存在于我的类path中。 但是,我在启动Spring应用程序上下文时收到以下错误:

 org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/beans/spring-beans.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>. 

经过一些难以解决的问题,我发现这个问题是由于我的jar文件的META-INF文件夹中的index.list 。 使用index.list文件,Spring命名空间处理程序无法find正确parsingspring应用程序上下文xml。 你可以阅读更多关于这个spring的问题SPR-5705

通过从我的maven-jar插件删除索引,我设法解决这个问题。 希望这会为有同样问题的人节省一些时间。

只要确保相关的spring jar文件在你的运行时类path中。 在我的情况下,我们缺less运行时类path中的spring-tx-4.3.4.RELEASE.jar。 join这个jar之后,问题就解决了。

如果您的平台上没有互联网连接,并使用Eclipse,请按照以下步骤操作(解决了我的问题)

  1. find确切的xsd文件(你可以从它们的jar文件中解压缩这些文件,例如spring-beans-xyzRELEASE.jar中的spring-beans-xyxsd)
  2. 将这些xsd文件添加到Eclipse XML目录。 (首选项 – > XML-> XML目录,添加文件)
  3. 将这些文件位置添加到configuration文件。 (小心,写出文件的确切版本)

例:

xsi:schemaLocation =“ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-xyxsd

当我使用spring-context版本4.0.6和spring-security版本4.1.0时,我遇到了同样的问题。 当在我的pom和security xml – > schemaLocation中将spring-security版本更改为4.0.4(因为spring-security的4.0.6不可用),它会在没有Internet的情况下编译。

所以这意味着你也可以通过解决这个问题

*将spring-security更改为比spring-context更早或更早的版本。

*将spring-context更改为比spring-security更新或更新的版本。

(任何方式春季上下文是春季安全更新或相同的版本)

删除最近在web-inf – > lib中添加的jar。 例如jstljar子。