Issue
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 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 these two examples.
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?
A side question is, where can I find a list of the versioned spring schemas?
Solution
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.
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.
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.
Anyway the Spring team should drop the versioned schemas for Spring 5.0, see 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.schemas files that actually make that link). Also, the files available online are built the same way (see the "schemaZip" target in the gradle build).
Answered By - Brian Clozel
Answer Checked By - Gilberto Lyons (JavaFixing Admin)