Spring configuration XML schema: with or without version?
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).
Spring schema folder & Spring XML namespaces
You can choose the symbol (p
, util
, jee
, beans
, ...) by yourself. These are namespaces, and they work by adding the xmlns
attribute like:
<beans xmlns:util="http://www.springframework.org/schema/util">
<!-- Content -->
</beans>
In this case we said that util:
will be used by the util schema, so you'll have to use <util:properties>
to access things from this namespace. But you could also have said xmlns:foobar="http://www.springframework.org/schema/util"
in which case you could have used things like <foobar:properties>
.
But you also need to provide the location of the XSD of that namespace by using xsi:schemaLocation
:
<beans xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- Content -->
</beans>
In this case the XSD for http://www.springframework.org/schema/util
is available at http://www.springframework.org/schema/util/spring-util.xsd. The http://www.springframework.org/schema/util
part is just a label and can be chosen as well. The only thing that has to match is the XSD schema.
For more information about XML namespaces, you should look at this question and its answers.
A list of common XML schema's with Spring can be found in their documentation (33. XML Schema-based configuration). However, these only list the core schemas. Some projects (like Spring Web Services, ...) have their own namespaces like:
- http://www.springframework.org/schema/web-services/web-services.xsd
- http://www.springframework.org/schema/oxm/spring-oxm.xsd
- ...
You can find the entire list by visiting the Index of /schema. However, like I mentioned, most of these are only used for specific Spring projects, don't just import them, read the specific documentation to find out what you need. The documentation about the constructor namespace (c:
) can be found in 7. The IoC container.
What is good about writing Java code in XML format as in Spring configuration?
XML and Properties files do not need to be re-compiled, allowing you to make in-deployment changes on a server environment.
For example, you can have 2 bean implementations and swap which one is injected:
<bean id="impl1" ... />
<bean id="impl2" ... />
<bean id="dependent" ... >
<constructor-arg ref="impl1"/>
</bean>
You can add or remove items to Collection
type items:
<util:set id="some_set">
<value>value #1</value>
</util:set>
Also for controlling environments for unit testing, if you have a set of XML files, one of which is for DB connections, then that's the one XML file you replace for your unit tests which need an in-memory DB, and can't connect to the real macoy at dev time:
src/main/resources/
- META-INF/spring/
service-context.xml
dao-context.xml
datasource-context.xml
src/test/resources/
- META-INF/spring/
datasource-context.xml // this is the test version of that context
Related Topics
How to Represent Double Values as Circles in a 2D Matrix in Java
Java Keylistener Not Registering Arrow Keys
Most Efficient Way to Increment a Map Value in Java
Difference Between Jar and War in Java
Static Nested Class in Java, Why
How to Implement a Re-Try-Catch
Is There a Good Natural Language Processing Library
Read Properties File Outside Jar File
Getting the Class Name from a Static Method in Java
Execute Method on Startup in Spring
What's the Difference Between <> and <? Extends Object> in Java Generics
How to Sort an Array of Ints Using a Custom Comparator
How to Watch for Single File Change with Watchservice (Not the Whole Directory)
Printing All Variables Value from a Class