Spring 的无版本模式(最新版本)

Spring 的无版本模式(最新版本)

原文: https://howtodoinjava.com/spring-core/do-not-specify-version-numbers-in-spring-schema-references/

如果您从事过 Spring 框架项目,那么您必须已经看到 spring 上下文配置文件(例如applicationContext.xml),其中在标头中为各种 spring 模块指定了模式引用(.xsd)。 在模式引用中,我们提到了 xml 名称空间和模式版本号。

那么根本不需要指定模式版本号,您可以忽略它。 如果确实如此,您应该一直忽略它。 将其视为要遵循的最佳实践

Spring 自动从项目依赖项(jar)中选择可用的最高版本。 另外,随着项目的发展和 Spring 版本的更新,我们不必维护所有 XML 配置文件即可看到新功能。

无版本 Spring 模式示例

有版本模式

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

  <!-- Other bean definitions-->

</beans>

无版本模式

可以这样写:

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

  <!-- Other bean definitions-->

</beans>

将我的问题放在评论部分。

学习愉快!