Spring version-less schema for latest version

If you have worked on Spring framework projects, then you must have seen spring context configuration files (e.g. applicationContext.xml) where in header you specify the schema references (.xsd) for various spring modules. In schema references, we mention the xml namespace and as well as schema version number.

Well specifying schema version number is not mandatory at all, and you can omit it. If fact, you should omit it all the time. Consider it as a best practice to follow.

Spring automatically picks the highest version available from the project dependencies (jars). Also, as the project evolves and the Spring version will be updated, we won’t have to maintain all the XML config files to see the new features.

Example of version-less Spring schema

With Schema Version

<?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-3.0.xsd
  http://www.springframework.org/schema/context/ 
  http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  
  <!-- Other bean definitions-->
  
</beans>

WITHOUT Schema Version

This can be written as:

<?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">

  
  <!-- Other bean definitions-->
  
</beans>

Drop me your questions in comments section.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

13 thoughts on “Spring version-less schema for latest version”

  1. Hi,
    i’m new to Spring and while learning i encountered this error and could not resolve it.I did a little research and there is no article in stackOverflow that explains this.

    cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element ‘context:property-placeholder’.

    <?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:property-placeholder location="classpath:sport.properties"/>
                    
            <bean id = "myFortune"
                  class =  "com.love2code.springdemo.HappyFortuneService">  
            </bean>        
             
           <bean id = "myCoach"
                 class = "com.love2code.springdemo.TrackCoach">
           <constructor-arg ref= "myFortune" />
           </bean>          
           
           <bean id="myCricketCoach"
                class = "com.love2code.springdemo.CricketCoach">
                <property name = "fortuneService" ref = "myFortune" />
                
                <!-- reference the loaded values from the properties file -->
                <property name = "emailAddress" value = "${foo.email}"/>
                <property name="name" value = "${foo.name}"/>
          </bean>
    </beans>
    
  2. How to remove version in this “”http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”>””
    This is showing error in Web.xml
    Thanks

  3. Hello,
    I’m using Spring 3.0.7.RELEASE but i have some problems with eclipse validation, the attribute local in “” cannot found in XSD if i remove the version in schemaLocation.

    How can I do ?

    My application-context :

    Thanx for help

  4. That’s nice to know. Are there any impact because of this feature ?

    For instance we might be developing a project currently on 3.2.0 and at a later period if it refers to 5.0,…… any impact ?
    How how it affects related to the maven pom dependencies ?

    Thanks
    Jay

Comments are closed.

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.