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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1729307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:58:24  来源:igfitidea点击:

Spring schemaLocation fails when there is no internet connection

javaxmlspringxsd

提问by Altug

I am using Spring and in application-context.xmlI have the following definitions:

我正在使用 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"
   >

.....

.....

When my internet connection is lost, I cannot run my application via tomcat or jetty.

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

It gives:

它给:

[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........

Any suggestions how to fix it?

任何建议如何解决它?

采纳答案by David Resnick

There is no need to use the classpath: protocol in your schemaLocation URL if the namespace is configured correctly and the XSD file is on your classpath.

如果命名空间配置正确并且 XSD 文件在您的类路径上,则无需在您的 schemaLocation URL 中使用 classpath: 协议。

Spring doc "Registering the handler and the schema" shows how it should be done.

Spring 文档“注册处理程序和架构”展示了它应该如何完成。

In your case, the problem was probably that the spring-context jar on your classpath was not 2.1. That was why changing the protocol to classpath: and putting the specific 2.1 XSD in your classpath fixed the problem.

在您的情况下,问题可能是您的类路径上的 spring-context jar 不是 2.1。这就是为什么将协议更改为类路径:并将特定的 2.1 XSD 放在类路径中解决了问题。

From what I've seen, there are 2 schemas defined for the main XSD contained in a spring-* jar. Once to resolve the schema URL with the version and once without it.

据我所知,spring-* jar 中包含的主要 XSD 定义了 2 个模式。一次使用版本解析架构 URL,一次不使用版本解析。

As an example see this part of the spring.schemas contents in spring-context-3.0.5.RELEASE.jar:

作为示例,请参阅 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

This means that (in xsi:schemaLocation)

这意味着(在 xsi:schemaLocation 中)

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

will be validated against

将被验证

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

in the classpath.

在类路径中。

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

or

或者

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

will be validated against

将被验证

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

in the classpath.

在类路径中。

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

is not defined so Spring will look for it using the literal URL defined in schemaLocation.

未定义,因此 Spring 将使用 schemaLocation 中定义的文字 URL 查找它。

回答by bmargulies

You need to add schema locations to your bean definition, and then they can be found in classpath instead of fetched over the net. Given your formatting problems, I'm not 100% sure that you aren't doing this already.

您需要将模式位置添加到 bean 定义中,然后它们可以在类路径中找到,而不是通过网络获取。鉴于您的格式问题,我不能 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>

回答by Thomas Jung

You should check that the spring.handlersand spring.schemasfiles are on the classpath and have the right content.

您应该检查spring.handlersspring.schemas文件是否在类路径上并且具有正确的内容。

This can be done with ClassLoader.getResource(..). You can run the method with a remote debuggerin the runtime environment. The extensible XML authoring setup is described in the Spring Reference B.5. Registering the handler and the schema.

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

Normally, the files should be in the spring jar (springframework.jar/META-INF/) and on the classpath when Spring can be initiated.

通常,当 Spring 可以启动时,这些文件应该在 spring jar (springframework.jar/META-INF/) 和类路径中。

回答by Altug

I solved it

我解决了

<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.xsdis the key for working offline mode (no internet connection). Also i copied spring-context-2.1.xsdnear (same directory) the application-context.xml file

classpath:spring-context-2.1.xsd是离线工作模式(无互联网连接)的关键。我还复制了spring-context-2.1.xsd附近(同一目录)application-context.xml 文件

回答by godlikeNoob

Something like this worked for me.

像这样的东西对我有用。

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"

回答by Jose Luis Mendez

The problem lies in the JAR files that you use in your application.

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

What I did, which worked, was to get inside the JARs for SPRING-CORE, SPRING-BEANS, SPRING-CONTEXT, SPRING-TX that match the version I am using. Within the META-INF folder, I concatenated all the spring.handlers and spring.schemas that come in those JARs.

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

I killed two birds with one stone, I solved the problem of the schemas so this also works correctly in offline mode.

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

P.S. I tried the maven plugin for SHADE and the transformers but that did not work.

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

回答by jyz

We solved the problem doing this:

我们解决了这个问题:

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");

Please note that our application is a Java standalone offline app.

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

回答by Roshan Khandelwal

If you are using eclipse for your development , it helps if you install STS plugin for Eclipse [ from the marketPlace for the specific version of eclipse .

如果您正在使用 eclipse 进行开发,那么如果您为 Eclipse 安装 STS 插件会有所帮助 [来自特定版本的 eclipse 的 marketPlace。

Now When you try to create a new configuration file in a folder(normally resources) inside the project , the options would have a "Spring Folder" and you can choose a "Spring Bean Definition File " option Spring > Spring Bean Configuation File .

现在,当您尝试在项目内的文件夹(通常为资源)中创建新配置文件时,选项将具有“Spring 文件夹”,您可以选择“Spring Bean 定义文件”选项 Spring > Spring Bean 配置文件。

With this option selected , when you follow steps , it asks you to select for namespaces and the specific versions :

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

And so the possibility of having a non-existent jar Or old version can be eliminated .

因此可以消除不存在 jar 或旧版本的可能性。

Would have posted images as well , but my reputation is pretty low.. :(

也会发布图片,但我的声誉很低.. :(

回答by Ed .

In case anyone arrives here via the same root I did - I hit this problem because I was building a single JAR with all dependencies, including Spring JARs. As a result the spring.schemasfile in some of the META-INFdirectories of Spring JARs was overwritten.

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

I found suggested solutions here: How to create spring-based executable jar with maven?

我在这里找到了建议的解决方案:如何使用 maven 创建基于 spring 的可执行 jar?

回答by zackurey

Ran into a similar issue today. In my case, it was the shade plugin that was the culprit, in addition to springframework.org having an outage. The following snippet cleared things up:

今天遇到了类似的问题。就我而言,除了 springframework.org 出现故障之外,罪魁祸首是 shade 插件。以下代码段清除了一切:

<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 someone

某人