Spring schemaLocation在没有网络连接的情况下会失败

15 浏览
0 Comments

Spring schemaLocation在没有网络连接的情况下会失败

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



.....

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

它会给出以下错误:

[main] WARN  org.springframework.beans.factory.xml.XmlBeanDefinitionReader - 忽略XML验证警告
org.xml.sax.SAXParseException: schema_reference.4: 无法读取模式文档'/spring-beans-2.0.xsd',因为1)找不到该文档;2)无法读取文档;3)文档的根元素不是。
    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)
    ...

有什么建议如何修复它吗?

0
0 Comments

在没有网络连接时,Spring的schemaLocation会失败。解决方法是在schemaLocation中使用classpath:spring-context-2.1.xsd作为路径,并将spring-context-2.1.xsd文件复制到与application-context.xml文件相同的目录中。对于使用Intellij的用户,即使Intellij不认识schemaLocation中的classpath语法并将其标记为错误,这种方法也适用。

0
0 Comments

当没有网络连接时,Spring的schemaLocation会失败。问题的原因是schemaLocation中引用的xsd文件无法从互联网上下载。为了解决这个问题,可以将xsd文件打包在Spring的jar文件中,然后通过classpath引用这些文件。以下是解决方案的代码示例:

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"

通过使用classpath引用内部jar中的xsd文件,可以避免在没有网络连接的情况下无法下载xsd文件的问题。这样就可以成功解析schema并避免出现"cvc-elt.1: Cannot find the declaration of element 'beans'."的错误。

0
0 Comments

在没有互联网连接时,Spring的schemaLocation会失败的原因是无法在类路径中找到相应的XSD文件。解决方法是通过正确配置命名空间和将XSD文件放在类路径中。在这种情况下,问题可能是你类路径上的spring-context jar不是2.1版本。将协议更改为classpath:并将特定的2.1 XSD放在类路径中可以解决该问题。

从我所见,对于spring-* jar中包含的主XSD,有2个模式被定义。一次用于解析带有版本的模式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将在类路径中与org/springframework/context/config/spring-context-2.5.xsd进行验证。http://www.springframework.org/schema/context/spring-context-3.0.xsd或http://www.springframework.org/schema/context/spring-context.xsd将在类路径中与org/springframework/context/config/spring-context-3.0.xsd进行验证。http://www.springframework.org/schema/context/spring-context-2.1.xsd没有定义,因此Spring将使用schemaLocation中定义的字面URL进行查找。

总结一下:Spring无法在类路径中找到模式。Spring核心JAR包含了一个模式->文件名映射,该映射包含在JAR包中。如果无法解析这个映射,Spring会去访问互联网。

我已经声明为"http:////www.springframework.org/schema/context http:///www.springframework.org/schema/context/spring-context-4.0.xsd"。我在Glassfish/domains/domain1/lib中有4.0版本的jar包。每当我部署项目时,Glassfish都会访问互联网。这个问题是否是特定于Glassfish-4.0?

我认为新的"Registering the handler and the schema"链接应该在这里:docs.spring.io/spring/docs/current/spring-framework-reference/… - 链接似乎出错了。

是的,原始链接似乎恢复了,我建议的链接是错误的。这是最新的文档链接:docs.spring.io/spring/docs/current/spring-framework-reference/…

0