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 the 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?
Many thanks!
XML configuration is still officially supported by Spring.
xml file is required! When required, however, we can take control over parts of the configuration and override the conventions that Spring Boot puts in play.
In XMLbased configuration metadata, you use the id and/or name attributes to specify the bean identifier(s). This attribute specifies the scope of the objects created from a particular bean definition and it will be discussed in bean scopes chapter.
Advantages of Spring Boot:It avoids writing lots of boilerplate Code, Annotations and XML Configuration. It is very easy to integrate Spring Boot Application with its Spring Ecosystem like Spring JDBC, Spring ORM, Spring Data, Spring Security etc.
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).
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.
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.
One more point to make here is that if possible it is better to avoid xml altogether and go with Java based @Configuration style to configure your Spring beans.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With