Spring 配置 XML 模式: 有版本还是没有版本?

我是春天的新来者。有一件事让我感到困惑,有时我看到 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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    

<context:annotation-config/>
<context:component-scan base-package="base.package"/>
    

</beans>

有时像这样:

<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
    

<context:annotation-config/>
<context:component-scan base-package="base.package"/>
    

</beans>

请注意,在这两个示例中,spring-beansspring-context模式是不同的。

所以,我的问题是,你会用哪种风格,为什么?特别是,版本化的模式将来是否不可用,以及当 Spring 更新模式时,非版本化的模式是否与当前应用程序保持兼容?

另一个问题是,我在哪里可以找到版本化的 Spring 模式列表?

77398 次浏览

我不确定它们是否是一种指导,但我个人倾向于引用非版本化的模式——一般来说,如果您正在处理 Spring 项目的最新版本(Spring 核心、集成等) ,那么您可以引用非版本化的模式。

未版本化的模式指向项目的最新版本,因此如果您使用的是真正的老版本 Spring (比如2.5版本对当前发布的4.0版本) ,它们可能不是正确的语法,在这种情况下,最好指向已版本化的模式。

这里还需要指出的一点是,如果可能的话,最好完全避免使用 xml,使用 基于 Java 的@Configuration样式来配置 Spring bean。

建议使用“无版本”XSD,因为它们映射到您在应用程序中使用的框架的当前版本。

应用程序和工具不应该尝试从 web 获取这些 XSD,因为这些模式包含在 JAR 中。如果他们这样做,通常意味着您的应用程序正在尝试使用比您正在使用的框架版本更新的 XSD,或者您的 IDE/工具没有正确配置。

据我所知,只有一种情况下需要使用特定的 XSD 版本: 当尝试使用在最近的版本中已经废弃/修改的 XML 属性时。这种事可不常发生。

无论如何,Spring 团队应该放弃 Spring 5.0的版本化模式,参见 SPR-13499

更多关于「无版本 = = 当前版本」的资料:

这些 XSD 文件包含在 Spring JAR 中——在构建期间,“无版本”XSD 被映射到最新版本(参见实际创建该链接的 SpringSchemas文件)。 另外,在线可用的文件也是以同样的方式构建的(参见 渐变构建中的“ schemaZip”目标)。

我知道这个问题已经存在两年多了,但我认为应该谨慎行事。

在我的案例中,我正在开发一个独立的 CLI 工具,一个罐子。当我声明的 XSD 是无版本的(原文如此)时,我会注意到非常奇怪的错误。考虑下面的 xml 片段:

enter image description here

如果没有版本,属性占位符中的 value-separator属性将导致以下错误:

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 22 in XML document from class path resource [META-INF/slitools/sli-cli-applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.3.2.2: Attribute 'value-separator' is not allowed to appear in element 'context:property-placeholder'.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:399)

人们可以尝试通过传递依赖关系来看看到底是什么在被拉动(即使仔细检查显示我们正在拉动包含正确 xsds 的正确的罐子(并确保我们正在做正确的属性合并与阴影插件当我们构建超级罐子)

显然是 YMMV。如果对一个项目有效,那就更好了。但是,如果你开始看到无法解释的奇怪错误,而且你没有足够的带宽去追查它们的根本原因,最好是准确的。

与此相反,如果您更改了 Spring 依赖项的版本,则需要确保显式的 xsd 版本是正确的。在软件中,所有的一切都是关于权衡的(并且知道你的决定会涉及到什么)

您将在 Jar 中找到 META-INF/spring.schemas 文件。该文件定义了所有兼容的 xsd 版本。如果您指向没有任何版本号的 URL,默认情况下它将与您的 Jar 文件兼容。只要在类路径中没有两个不同版本的 spring jar,应用程序就不会有任何运行时错误。

我已经报名参加了乌迪米大学的春季课程,我按照导师教我的每一个步骤去做。 因此,如果使用 spring mvc 并休眠,则可能会遇到此错误 未能读取模式文档‘ http://www.springframework.org/schema/tx/spring-tx.xsd’等:

<mvc:annotation-driven/> and <tx:annotation-driven transaction-manager="myTransactionManager" /> elements

在我的 Spring 配置文件中有两个 url

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


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

在 xsi: schemaLocation 中,我将其替换为

    http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd


http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

实际上我去过这两个地方 Http://www.springframework.org/schema/mvc/ http://www.springframework.org/schema/tx/ 并且刚刚添加了 spring-mvc 和 spring-tx 的最新版本,即 spring-mvc-4.2. xsd 和 spring-tx-4.2. xsd

因此,在我看来,指定版本没有明确是一个好的做法。 希望这个能帮上忙。 谢谢你。

考虑使用无版本的 xsd。这将使工具获取与您正在使用的 spring jar 版本相匹配的 xsd 版本(请查看 jar 中的 spring.schemas 文件)。如果在升级 Spring 库时出现任何不兼容的情况(这种情况应该很少发生) ,那么您应该能够在构建期间捕捉到它。

这将提供一个机会来决定是否需要为该文件引入版本化的 xsd,或者实际上是从使用过时和不推荐的属性/元素演变而来。希望是晚一点。

否则,风险就是永远使用可能不推荐的属性/元素。