簡體   English   中英

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

[英]Spring configuration XML schema: with or without version?

我是春天的新手。 讓我感到困惑的一件事是,有時我會看到帶有版本化模式的 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 模式的列表?

建議使用“無版本”XSD,因為它們映射到您在應用程序中使用的框架的當前版本。

應用程序和工具不應該嘗試從 Web 獲取這些 XSD ,因為這些模式包含在 JAR 中。 如果是這樣,這通常意味着您的應用程序正在嘗試使用比您正在使用的框架版本更新的 XSD,或者您的 IDE/工具配置不正確。

據我所知,只有一種情況需要使用特定的 XSD 版本:嘗試使用在較新版本中已棄用/修改的 XML 屬性時。 至少可以說,這種情況並不經常發生。

無論如何,Spring 團隊應該放棄 Spring 5.0 的版本化模式,請參閱SPR-13499

有關“無版本 == 當前版本”的更多信息:

這些 XSD 文件包含在 Spring JAR 中——“無版本”XSD 在構建期間映射到最新版本(請參閱實際創建該鏈接的spring.schema文件)。 此外,在線可用的文件以相同的方式構建(請參閱gradle build 中的“schemaZip”目標)。

我不確定它們是否是指導,但我個人的偏好是參考非版本化模式 - 通常,如果您正在使用 Spring 項目的更新版本(Spring 核心、集成等),那么您可以請參閱未版本化的架構。

未版本化的模式指向項目的最新版本,因此如果您使用的是 Spring 的真正舊版本(例如 2.5 版與當前發布的 4.0 版),它們可能不是正確的語法,在這種情況下可能會更好指向版本化的模式。

這里要說明的另一點是,如果可能的話,最好完全避免使用 xml,並使用基於 Java 的 @Configuration樣式來配置 Spring bean。

我知道這個問題已經有兩年多了,但我的看法是謹慎行事。

在我的例子中,我正在開發一個獨立的 CLI 工具,一個 jar of jars。 當我聲明的 XSD 是無版本的(原文如此)時,我會注意到非常奇怪的錯誤。 考慮以下 xml 片段:

在此處輸入圖像描述

如果沒有版本,屬性占位符中的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 的正確 jars(並確保我們在構建 uber 時正在使用正確的屬性與 shade 插件合並) -罐子。)

顯然是YMMV。 如果它適用於一個項目,那就更好了。 但是,如果您開始看到無法解釋的奇怪錯誤,並且您沒有足夠的能力將它們追查到根本原因,那么最好准確一點。

與此相反的論點是,如果您更改了 spring 依賴項的版本,則需要確保顯式 xsd 版本是正確的。 在軟件中,一切都是關於權衡(以及了解您的決策所涉及的內容。)

我已經報名參加udemy的春季課程。 我按照老師教我做的每一個步驟去做。 因此,如果您使用的是 spring mvc 和 hibernate,您可能會遇到此錯誤 Failed to read schema document ' 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

因此,在我看來,明確指定版本號是一個好習慣。 希望這可以幫助。 謝謝你。

你會在你的 Jar 中找到 META-INF/spring.schemas 文件。 該文件定義了所有兼容的 xsd 版本。 如果您指向沒有任何版本號的 URL,默認情況下它將與您的 Jar 文件兼容。 只要您在類路徑中沒有兩個不同版本的 spring jar,您的應用程序就不會出現任何運行時錯誤。

考慮使用無版本 xsd。 這將使工具獲取與您正在使用的 spring jar 版本匹配的 xsd 版本(查看 jar 中的 spring.schemas 文件)。 如果在升級 Spring 庫時出現任何不兼容(這應該很少見),您應該能夠在構建期間捕獲它。

這將提供一個機會來決定是否需要為該文件引入版本化的 xsd,或者實際上是從使用過時的和已棄用的屬性/元素演變而來的。 希望是后者。

否則風險是永遠使用可能已棄用的屬性/元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM