It doesn't matter with Spring - XML โโmust be valid so that Spring can understand it what it is. It is up to you which format you choose. Usually you use the default namespace to not print too much (all examples are based on Appendix C. Configuration based on XML schema ):
<?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="..."/> </beans>
Attribute
xmlns="..." defines the default namespace (the one used if you donโt specify any namespace at all, for example <beans/> . This is fine if you use only one beans namespace and sometimes several declarations from other Namespaces:
<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <bean id="..." class="..."> <property name="isolation"> <util:constant static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/> </property> </bean> </beans>
But sometimes you will notice that you are using more nodes from different namespaces than the default beans namespace. A good example is Spring Configuration Configuration :
<beans xmlns:security="http://www.springframework.org/schema/security" 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 http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.3.xsd"> <security:http auto-config='true'> <security:intercept-url pattern="/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY"/> <security:intercept-url pattern="/**" access="ROLE_USER" /> <security:form-login login-page='/login.jsp'/> <security:session-management> <security:concurrency-control max-sessions="1" /> </security:session-management> <security:openid-login> <security:attribute-exchange> <security:openid-attribute name="email" type="http://axschema.org/contact/email" required="true" /> <security:openid-attribute name="name" type="http://axschema.org/namePerson" /> </security:attribute-exchange> </security:openid-login> </security:http> <security:authentication-manager> <security:authentication-provider user-service-ref='myUserDetailsService'/> </security:authentication-manager> <bean id="myUserDetailsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
You see how inconvenient this is because the beans namespace is so rarely used by default, but the whole file must be cluttered with the security: prefix? What about this (note how xmlns declarations have changed):
<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="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 http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.3.xsd"> <http auto-config='true'> <intercept-url pattern="/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY"/> <intercept-url pattern="/**" access="ROLE_USER" /> <form-login login-page='/login.jsp'/> <session-management> <concurrency-control max-sessions="1" /> </session-management> <openid-login> <attribute-exchange> <openid-attribute name="email" type="http://axschema.org/contact/email" required="true" /> <openid-attribute name="name" type="http://axschema.org/namePerson" /> </attribute-exchange> </openid-login> </http> <authentication-manager> <authentication-provider user-service-ref='myUserDetailsService'/> </authentication-manager> <beans:bean id="myUserDetailsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl"> <beans:property name="dataSource" ref="dataSource"/> </beans:bean> </beans:beans>
These two configuration files are semantically equivalent (this is just another way of encoding the same information). But the latter is much readable. Just use the default namespace, which namespace is used the most.
source share