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

JBT*_*JBT 66 java spring

我是Spring的新手.有一件事让我感到困惑的是,有时我会看到带有版本化模式的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>
Run Code Online (Sandbox Code Playgroud)

有时像这样:

<?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>
Run Code Online (Sandbox Code Playgroud)

请注意,两个示例中的spring-beansspring-context模式不同.

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

一个附带问题是,我在哪里可以找到版本化的弹簧模式列表?

非常感谢!

Bri*_*zel 87

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

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

据我所知,只有一种情况需要使用特定的XSD版本:尝试使用在较新版本中弃用/修改的XML属性.至少可以说这种情况并不常见.

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

有关"versionless == current version"的更多信息:

这些XSD文件包含在Spring JAR中 - "无版本"XSD在构建期间映射到最新版本(请参阅实际创建该链接的spring.schemas文件).此外,在线可用的文件以相同的方式构建(请参阅gradle构建中"schemaZip"目标).