ef 关联依赖属性_基础巩固之:xmlns属性梳理

5c42e2cbd2fc845a55f4da57630d3771.png

转载自:https://www.cnblogs.com/osttwz/p/6892999.html

<?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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="xxx.xxx.controller" />
     
    <context:annotation-config/>
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>
     
    <mvc:resources mapping="/images/**" location="/images/" />
     
    <bean id="xxx" class="xxx.xxx.xxx.Xxx">
        <property name="xxx" value="xxxx"/>
    </bean>
</beans>

在配置文件中,我们经常会看到如上的配置内容,其中的含义必须弄明白,否则一旦拷贝出错,但是不知道错在哪里就会在工作中引入不必要的麻烦。 xmlns就是xml nameSpace的缩写,即xml的命名空间。

1 为什么需要xmlns

考虑有如下两个文档,表示HTML表示元素的

<table>
  <tr>
    <td>Apples</td>
    <td>Bananas</td>
  </tr>
</table>

和描述一张桌子的

<table>
  <name>African Coffee Table</name>
  <width>80</width>
  <length>120</length>
</table>

假设两个xml文档一起被使用,由于两个文档都包含带有不同内容和定义的

元素,就会发生命名冲突。xml解析器无法确认如何处理这类冲突,因此引入xmlns。

2 如何使用xmlns

使用语法:xmlns:namespace-prefix="namespaceURI",其中,namespace-prefix为自定义前缀,只要这个xml文档中保证前缀不重复即可;namespaceURI是这个前缀对应的XML Namespace的定义。例如

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

这一句定义了一个http://www.springframework.org/schema/context的Namespace(和java类的包的声明很相似),并将其和前缀context绑定,所以spring XML文档中会有这么一句:

<context:component-scan base-package="xxx.xxx.controller" />
     
<context:annotation-config/>

这里的component-scan和annotation-config元素就来自于context的XML Namespace,也就是在http://www.springframework.org/schema/context中定义。其中,component-scan标签用于扫描指定包。

<context:annotation-config>配置节点用于向Spring容器中注册:

  • AutowiredAnnotationBeanPostProcessor:@Autowired注解依赖
  • CommonAnnotationBeanPostProcessor:@Resource/@PostConstruct/@PreDestory等注解依赖;
  • PersistenceAnnotationPostProcessor::@PersistentenceContext注解依赖
  • RequiredAnnotationBeanPostProcessor:@Required注解依赖

我们可以将前缀定义为abc,如:

xmlns:abc="namespaceURI"

这里再使用namespaceURI的元素时,需要以abc为前缀,例如<abc:xxx/>

<!-- 这里xmlns:h="url1"表示这个table是用h作为标记,table的写法在url1中定义 -->
<h:table xmlns:h="url1">
  <h:tr>
    <h:td>Apples</h:td>
    <h:td>Bananas</h:td>
  </h:tr>
</h:table>
<!-- 这里xmlns:f="url2"表示这个table是用f作为标记,table的写法在url2中定义 -->
<f:table xmlns:f="url2">
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>

后者与前者仅仅使用不同前缀,我们为

标签添加了一个 xmlns 属性,这样就为前缀赋予了一个与某个命名空间相关联的限定名称。此时再把它们放在一起,XML解析器就不会报错了。

注意:当xmlns被定义在元素的开始标签中(如这里的<f:table/>)时,所有带有相同前缀的子元素都会与同一个Namespace相关联(即<f:table/>里面的<f:name/>和<f:width/>也会使用url2定义的写法)。

3 xmlns和xmlns:xsi区别

xmlns表示默认的Namespace,例如Spring XML文档中

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

表示该文档默认的XML Namespace为http://www.springframework.org/schema/beans,对于默认的Namespace元素,可以不使用前缀,例如:

<bean id="xxx" class="xxx.xxx.xxx.Xxx">
    <property name="xxx" value="xxxx"/>
</bean>

xmlns:xsi表示使用xsi作为前缀的Namespace,当然前缀xsi需要在文档中声明。

4 xsi:schemaLocation作用

xsi:schemaLocation属性是Namespace为http://www.w3.org/2001/XMLSchema-instance里的schemaLocation属性,正是因为我们一开始声明了:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

这里才写作xsi:schemaLocation。它定义了XML Namespace和对应的XSD(XML Schema Definition)文档位置的关系。它的值由一个或这个URI引用对组成,两个URI之间以空白符分割(空格或者换行也可以)。第一个URI是定义XML Namespace的值,第二个URI给出schema文档的位置,Schema处理器将从这个位置读物Schema文档,该文档的targetNamespace必须与第一个URI相匹配,如:

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

这里表示Namespace为http://www.springframework.org/schema/context的Schema的位置为http://www.springframework.org/schema/context/spring-context.xsd。这里我们可以打开这个Schema的位置,下面是这个文档的开始部分:

<xsd:schema xmlns="http://www.springframework.org/schema/context"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:beans="http://www.springframework.org/schema/beans"
            xmlns:tool="http://www.springframework.org/schema/tool"
             
            <!-- 这里的targetNamespace和上方xsi:schemaLocation中的第一个URI匹配 --> 
            targetNamespace="http://www.springframework.org/schema/context"
            elementFormDefault="qualified"
            attributeFormDefault="unqualified">
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值