Java Spring 配置 XML 模式:有或没有版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20894695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring configuration XML schema: with or without version?
提问by JBT
I am new to Spring. One thing confuses me is that sometimes I see XML configuration files with versioned schemas, yet sometimes with non-versioned ones. For example, sometimes I see something like
我是春天的新手。让我感到困惑的一件事是,有时我会看到带有版本模式的 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>
And sometimes like this:
有时是这样的:
<?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>
Note that the spring-beans
and spring-context
schemas are different in the two examples.
请注意,两个示例中的spring-beans
和spring-context
模式是不同的。
So, my question is, which style would you use and why? In particular, will the versioned schema become unavailable in the future, and will the non-versioned schema keep compatible with a current application when Spring updates the schema?
所以,我的问题是,你会使用哪种风格,为什么?特别是,版本化模式将来是否会变得不可用,当 Spring 更新模式时,非版本化模式是否会与当前应用程序保持兼容?
A side question is, where can I find a list of the versioned spring schemas?
一个附带问题是,我在哪里可以找到版本化 spring 模式的列表?
Many thanks!
非常感谢!
采纳答案by Brian Clozel
It is recommended to use the "versionless" XSDs, because they're mapped to the current version of the framework you're using in your application.
建议使用“无版本”XSD,因为它们映射到您在应用程序中使用的框架的当前版本。
Applications and tools should never try to fetch those XSDs from the web, since those schemas are included in the JARs. If they do, it usually means your app is trying to use a XSD that is more recent than the framework version you're using, or that your IDE/tool is not properly configured.
应用程序和工具不应尝试从 Web 获取这些 XSD,因为这些模式包含在 JAR 中。如果出现这种情况,通常意味着您的应用程序正在尝试使用比您正在使用的框架版本更新的 XSD,或者您的 IDE/工具未正确配置。
To my knowledge, there's only one case where you'd want to use specific XSD versions: when trying to use a XML attribute that's been deprecated/modified in a more recent version. That doesn't happen often to say the least.
据我所知,只有一种情况您希望使用特定的 XSD 版本:尝试使用在较新版本中已弃用/修改的 XML 属性时。至少可以说,这种情况并不经常发生。
Anyway the Spring team should drop the versioned schemas for Spring 5.0, see SPR-13499.
无论如何,Spring 团队应该放弃 Spring 5.0 的版本化模式,请参阅SPR-13499。
More on "versionless == current version":
更多关于“无版本 == 当前版本”:
Those XSD files are included in Spring JARs - the "versionless" XSD is mapped to the latest version during the build (see the spring.schemasfiles that actually make that link). Also, the files available online are built the same way (see the "schemaZip" target in the gradle build).
这些 XSD 文件包含在 Spring JAR 中——“无版本”XSD 在构建期间映射到最新版本(请参阅实际建立该链接的spring.schemas文件)。此外,在线可用的文件以相同的方式构建(请参阅gradle build 中的“schemaZip”目标)。
回答by Biju Kunjummen
I am not sure if their is a guidance, but my personal preference is to refer to the non-versioned schemas - in general if you are working against the more recent versions of the Spring projects(Spring core, integration etc), then you can refer to the unversioned schemas.
我不确定它们是否是指导,但我个人的偏好是参考非版本化模式 - 一般来说,如果您正在使用较新版本的 Spring 项目(Spring 核心、集成等),那么您可以请参阅未版本控制的架构。
The unversioned schemas point to the latest version of the projects so it is possible that they may not be the correct grammar if you are using really old version of Spring (say version 2.5 against currently released 4.0 version), in such cases it may be better to point to the versioned schemas.
未版本控制的模式指向项目的最新版本,因此如果您使用的是真正旧版本的 Spring(例如 2.5 版与当前发布的 4.0 版),它们可能不是正确的语法,在这种情况下可能会更好指向版本化模式。
One more point to make here is that if possible it is better to avoid xml altogether and go with Java based @Configurationstyle to configure your Spring beans.
这里要说明的另一点是,如果可能,最好完全避免使用 xml,并使用基于 Java 的 @Configuration样式来配置 Spring bean。
回答by luis.espinal
I know that the question is over two years old, but my take is to proceed with caution.
我知道这个问题已经两年多了,但我的看法是谨慎行事。
In my case I was developing a stand-alone CLI tool, a jar of jars. And when my declared XSDs were versionless(sic), I would notice very strange errors. Consider the following xml fragment:
就我而言,我正在开发一个独立的 CLI 工具,一罐罐子。当我声明的 XSD 是无版本的(原文如此)时,我会注意到非常奇怪的错误。考虑以下 xml 片段:
Without the versions, the value-separator
attribute in the property placeholder will cause the following error:
如果没有版本,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)
One could try to see what on earth is being pulled via transitive dependencies (even though careful inspection showed we were pulling the right jars containing the right xsds (and ensured we were doing the right property merging with the shade plugin when we were building the uber-jars.)
人们可以尝试通过传递依赖来查看到底是什么被拉取(尽管仔细检查表明我们正在拉取包含正确 xsds 的正确 jars(并确保我们在构建 uber 时将正确的属性与 shade 插件合并) -罐子。)
Obviously YMMV. If it works for one project, the better. But if you ever start seeing strange errors that defy explanation and you do not have the bandwidth to chase them down to the root cause, better to be precise.
显然是YMMV。如果它适用于一个项目,那就更好了。但是,如果您开始看到无法解释的奇怪错误,并且您没有足够的时间将它们追查到根本原因,那么最好是准确的。
The counter-argument of this is that if you ever change the version of your spring dependencies, you need to make sure the explicit xsd version are correct. In software, it is all about trade-offs (and knowing what you get into with your decisions.)
对此的反驳是,如果您更改了 spring 依赖项的版本,则需要确保显式 xsd 版本是正确的。在软件中,一切都与权衡有关(并了解您的决定。)
回答by Bakul Brahmbhatt
You will find META-INF/spring.schemas file in your Jar. That file defines all compatible xsd versions. If you point to URL without any version number, by default it would be compatible with your Jar file. As far as you don't have two different version of spring jar in classpath, your application will not have any runtime errors.
您将在 Jar 中找到 META-INF/spring.schemas 文件。该文件定义了所有兼容的 xsd 版本。如果您指向没有任何版本号的 URL,默认情况下它将与您的 Jar 文件兼容。只要您在类路径中没有两个不同版本的 spring jar,您的应用程序就不会出现任何运行时错误。
回答by Shafqat Shafi
I have enroled for spring course on udemy. I followed every step that my instructor show me to do. So if you are using spring mvc and hibernate you may encounter this error Failed to read schema document 'http://www.springframework.org/schema/tx/spring-tx.xsd' etc for:
我已经注册了 udemy 的春季课程。我按照老师教我做的每一步。因此,如果您使用 spring mvc 和 hibernate,您可能会遇到此错误 Failed to read schema document ' http://www.springframework.org/schema/tx/spring-tx.xsd' etc for:
<mvc:annotation-driven/> and <tx:annotation-driven transaction-manager="myTransactionManager" /> elements
in my spring configuration file i had these two urls
在我的 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
in xsi:schemaLocation, which i replaced with
在 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
actually i visited these two sites http://www.springframework.org/schema/mvc/and http://www.springframework.org/schema/tx/and just added the latest version of spring-mvc and spring-tx i.e, spring-mvc-4.2.xsd and 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
So, in my opinion specifying version no explicitly is a good practice. Hope this helps. Thank you.
因此,在我看来,明确指定版本号是一个好习惯。希望这可以帮助。谢谢你。
回答by Abhijith Madhav
Consider using versionless xsd's. This will make the tooling pick up the version of the xsd matching the version of the spring jar you are using(look at the spring.schemas file in your jar). In case of any incompatibility when you upgrade your spring libraries(which should really be rare) you should be able to catch it during build.
考虑使用无版本的 xsd。这将使工具选择与您正在使用的 spring jar 版本匹配的 xsd 版本(查看 jar 中的 spring.schemas 文件)。如果在升级 spring 库时出现任何不兼容(这应该很少见),您应该能够在构建期间捕获它。
This will provide an opportunity to decide whether there is a need to introduce versioned xsd for that one file or actually evolve from using an archaic and deprecated attribute/element. Hopefully it will be the later.
这将提供一个机会来决定是否需要为该文件引入版本化的 xsd,或者实际上是从使用过时且已弃用的属性/元素演变而来。希望是后者。
Else the risk is of using a possible deprecated attribute/element forever.
否则,风险是永远使用可能已弃用的属性/元素。