ef 关联依赖属性_基础巩固之:xmlns属性梳理
轉載自: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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://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">總結
以上是生活随笔為你收集整理的ef 关联依赖属性_基础巩固之:xmlns属性梳理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 玩家的青春记忆!神级软件重返江湖:Win
- 下一篇: 显卡涨价之后!现在买整机比自己组一台电脑