shazinltc
shazinltc

Reputation: 3666

What difference does an xmlns definition make in a spring configuration file

Learning Spring I have found two types of definition of xmlns in spring configuration file. One starting with this:

 <beans xmlns="http://www.springframework.org/schema/beans"

which I found in the spring documentation

And the other one starts with this:

 <beans:beans  xmlns="http://www.springframework.org/schema/mvc"

Both works fine. One difference I have observed is that you have to start all the tags with name beans if you are using the second definition, like this:

<beans:import resource="hibernate-context.xml" /> 

which otherwise can be written as

 <import resource="hibernate-context.xml" />

What major difference do they make?

Upvotes: 5

Views: 2422

Answers (1)

Biju Kunjummen
Biju Kunjummen

Reputation: 49935

This is not specific to Spring but is more about XML and Namespaces - a reference is here: http://www.w3schools.com/xml/xml_namespaces.asp, http://en.wikipedia.org/wiki/XML_namespace

Just to summarize: In the first instance

<beans xmlns="http://www.springframework.org/schema/beans"

makes the beans schema the default for this xml file, which will allow referring to the elements in this beans schema without a namespace prefix. So where is this schema defined - reference to the schema is typically included with the schemaLocations attribute something like this -

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd" 

What the above is saying is that the definition of the http://www.springframework.org/schema/beans is present in the corresponding .xsd file

In your second instance -

<beans:beans  xmlns="http://www.springframework.org/schema/mvc" xmlns:beans="http://www.springframework.org/schema/beans"

You are now defining the mvc namespace as the default namespace, so in this case any elements in the mvc schema can be referred to without any prefix, but if you want to refer to any elements in the beans schema you will have to refer to it using the beans: prefix like for the beans:import in your sample

Upvotes: 6

Related Questions