小编典典

Spring配置XML模式:with or without version?

spring

我是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更新架构时,非版本化的架构会与当前应用程序保持兼容吗?

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

非常感谢!


阅读 571

收藏
2020-04-11

共1个答案

小编典典

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

应用程序和工具切勿尝试从Web上获取那些XSD,因为JAR中包含了这些模式。如果这样做,通常意味着你的应用正在尝试使用比你使用的框架版本更新的XSD,或者你的IDE /工具配置不正确。

据我所知,只有一种情况需要使用特定的XSD版本:当尝试使用在较新版本中已过时/修改的XML属性时。至少可以这样说,这通常不会发生。

2020-04-11