XML 命名空间 是由国际化资源标识符 (IRI) 标识的 XML 元素和属性集合。在XML中,元素名称是由开发者定义的,当两个不同的文档使用相同的元素名时,就会发生命名冲突。

  • 命名空间的声明与使用

下面由浅入深讲述Spring的配置文件的命名空间。

  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.xsd">  

声明的命名空间的范围起始于声明该命名空间的元素,并应用于该元素的所有内容,直到被具有相同前缀名称的其他命名空间声明覆盖。上面的命名空间是在 <beans> </beans>元素中声明的,所有其中声明的命名空间在这两个标签中有效。

xmlns:类似于一个保留字,它只用于声明命名空间。换言之,xmlns 用于绑定命名空间,但其本身并不绑定到任何命名空间。

xmlns:aop="http://www.springframework.org/schema/aop"

aop:实际上是与命名空间"http://www.springframework.org/schema/aop"绑定在一起。通常我们会用一个比较简短或约定俗成的名字来作为命名空间的前缀(例如这里的aop),使用有意义的命名空间前缀增强了XML档的清晰性。有利于xml配置代码阶段的清晰

    
    

这里我们在配置面向切面编程的内容时,使用aop前缀,代表后面的元素(config,aspect等)都是在http://www.springframework.org/schema/aop中定义的。

  • 单个默认命名空间

在配置文件中,beans,bean等元素我们是没有使用命名空间前缀的。声明一个默认命名空间无论在任何时候都只能存在一个默认命名空间声明范围内的任何元素未使用前缀显式限定,则该元素将被隐式限定。与带前缀的命名空间一样。

  •  Spring配置文件配置命名空间

通常情况下,命名空间对应的URI是一个存放XSD的地址,尽管规范没有这么要求。如果没有提供schemaLocation,那么Spring的XML解析器会从命名空间的URI里加载XSD文件。

schemaLocation提供了一个xml 命名空间到对应的XSD(Xml Schema Definition)文件的一个映射,它的值由一个或多个URI引用对组成,
两个URI之间以空白符分隔(空格和换行均可)。第一个URI是定义的 XML命名空间的值,第二个URI给出Schema文档的实际位置,
Schema处理器将从这个位置读取Schema文档,该文档的targetNamespace必须与第一个URI(XML命名空间的值)相匹配。

在xsi:schemaLocation后面配置的字符串都是成对的,前面的是命名空间的URI,后面是xsd文件的URI;xsi是在xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 声明的。

http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

这里命名空间aop的值是“http://www.springframework.org/schema/aop”,它对应的xsd文件的位置为“http://www.springframework.org/schema/aop/spring-aop-3.0.xsd”。我们打开http://www.springframework.org/schema/aop/spring-aop-3.0.xsd,可以看到xsd文件中targetNamespace的值和命名空间的值一样。

  • 本地检验XMl的XSD文件

Spring默认在启动时是要从配置的命名空间的位置加载XSD文件来验证xml文件的,所以如果有的时候断网了,或者一些开源软件切换域名,那么就很容易碰到应用启动不了。为了防止这种情况,Spring提供了一种机制,即默认从本地加载XSD文件,当本地没有时才根据实际的URI去联网获得。

依次打开如下图所示位置:

(这里是我项目版本以及地址,)

打开文件可以看见所用版本以及以前的版本对应的路径,按照路径打开

Spring是把XSD文件放到本地了,再在spring.schemas里做了一个映射,优先从本地里加载XSD文件。并且把spring旧版本的XSD文件也全放了。这样可以防止升级了Spring版本,而配置文件里用的还是旧版本的XSD文件,然后断网了,应用启动不了。

我们在写命名空间值对应的xsd文件位置时,可以不用写版本号,它默认的是本地spring相关版本的对应xsd版本。

  • 下面我们来完善

  • util

util标签用来配置集合、常量等的

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

  使用:分别使用<util:list>、<util:map>、<util:set>、<util:properties>等标签,用来 

     取代ListFactoryBean、MapFactoryBean、SetFactoryBean、PropertiesFactoryBean。

  用途一:直接使用<util:properties id="appProps" location="classpath:META-INF/app.properties" />指定了配置文件
    以前需要使用
      <!-- creates a java.util.Properties instance with values loaded from the supplied location -->
        <bean id="jdbcConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
          <property name="location" value="classpath:com/foo/jdbc-production.properties"/>
        </bean>
    spring中id就是这个类的一个别名
    现在只需使用
      <!-- creates a java.util.Properties instance with values loaded from the supplied location -->
        <util:properties id="jdbcConfiguration" location="classpath:com/foo/jdbc-production.properties"/>

  • jee

jee标签用来处理javaee标准相关的问题,例如查询一个jndi对象以及定义一个ejb的引用等

xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation中需要定义
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
  • lang 

lang用来将那些已经被定义在一些动态语言(例如Jruby和Groovy)中的对象作为beans中的对象存放到spring容器中

xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation中需要定义
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
  • jms 

xmlns:jms="http://www.springframework.org/schema/jms"
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd
  • tx (transaction) 

使用时建议配合aop

xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
  • aop 

xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
  • context 

xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  • jdbc 

xmlns:jdbc="http://www.springframework.org/schema/jdbc"
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
  • cache 

xmlns:jdbc="http://www.springframework.org/schema/cache"
http://www.springframework.org/schema/cache http://www.springframework.org/schema/jdbc/spring-cache.xsd
  • 一个完整的配置文件头

以上是我学习本阶段为了弄明白命名空间的作用查找资料总结,特别感谢我所参考的博客!

学着前辈高手开源分享原则,我将整理学习的分享出来,这是一部分,后面学习会进一步深化学习与分享!