HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Spring version-less schema for latest version

By Lokesh Gupta | Filed Under: Spring Core

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 !!

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

13
Leave a Reply

This comment form is under antispam protection
6 Comment threads
7 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
7 Comment authors
This comment form is under antispam protection
  Subscribe  
newest oldest most voted
Notify of
Arpit Chinmay

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>
Vote Up0Vote Down  Reply
1 year ago
Amol Gangawane

Is it applicable to spring -security xsd also??

Vote Up0Vote Down  Reply
3 years ago
Lokesh Gupta

Yes.

Vote Up0Vote Down  Reply
3 years ago
Gaurav Sachdeva

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

Vote Up0Vote Down  Reply
4 years ago
Lokesh Gupta

Please check the version of servlets jar in classpath. It should match with xsd version. More info : https://stackoverflow.com/questions/12146247/java-web-app-what-determines-my-servlet-api-version-does-it-get-specified-in

Vote Up0Vote Down  Reply
4 years ago
Kwen

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

Vote Up0Vote Down  Reply
5 years ago
Lokesh Gupta

Please put the code in code tag

Vote Up0Vote Down  Reply
5 years ago
Kwen

Sorry, i replace some characters by “-”

-?xml version=”1.0″ encoding=”UTF-8″?-
-beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd“-
-bean id=”myDaoTarget” class=”com.test.dao.MyDaoImpl”/-
-bean id=”myDao” class=”org.springframework.aop.framework.ProxyFactoryBean”-
-property name=”proxyInterfaces” value=”com.test.dao.MyDao” /-
-property name=”target”–ref local=”myDaoTarget”/–/property-
-/bean-
-/beans-

Vote Up0Vote Down  Reply
5 years ago
Lokesh Gupta

The http://www.springframework.org/schema/beans/spring-beans.xsd actually points to the 4.0 XSD, which does NOT have local. Use schemaLocation or version number.

Vote Up0Vote Down  Reply
5 years ago
Kwen

Ok Thanx u πŸ™‚

Vote Up0Vote Down  Reply
5 years ago
Jayakumar Jayaraman

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

Vote Up0Vote Down  Reply
5 years ago
Lokesh Gupta

It will impact only when you use the config tags introduced in newer versions and you have old schema references.

Vote Up0Vote Down  Reply
5 years ago
goni

nice

Vote Up0Vote Down  Reply
5 years ago

Search Tutorials

Spring Tutorial

  • Spring – Introduction
  • Spring – IoC Containers
  • Spring – IoC vs. DI
  • Spring – Bean Scopes
  • Spring – Bean Life Cycle
  • Spring – Bean Postprocessors
  • Spring – Autowiring
  • Spring – Annotations
  • Spring – Stereotype Annotations
  • Spring – Task Scheduling
  • Spring – Timer Task
  • Spring – Events
  • Spring – Message Source
  • Spring – ResourceLoader
  • Spring – Property Editor
  • Spring – Send Email
  • Spring – Version-less Schema
  • Spring – Interview Questions
  • Spring – Best Practices

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz