Spring配置文件之引入命名空间
2017-07-25 19:23
387 查看
如何为Spring配置文件引入命名空间是我在初接触Spring时有点头疼的问题。现在稍作整理
以上就是最简格式。对于XML文件,xmlns、xmlns:xsi、xsi:schemaLocation一般都是必须的。对于Spring配置文件,beans这个命名空间是不可缺少的,并且会把beans作为默认的命名空间。
(2)xmlns指定的命名空间是默认的命名空间,xmlns=”http://www.springframework.org/schema/beans”表示默认命名空间
(3)xmlns:xsi这个命名空间的prefix为xsi,因此这个命名空间里面的元素或者属性就必须要以xsi:这种方式来写,比如schemaLocation就是他的一个属性,所以写成xsi:schemaLocation
(4)xmlns是该XML文件默认的命名空间;
xmlns:xsi是指该XML文件遵守xml规范;
xsi:schemaLocation是指具体用到的schema资源
(5)一般地,xml文件都需要写xmlns, xmlns:xsi, xsi:schemaLocation这3项。
(2)会把beans作为默认的命名空间引入
(3)其他命名空间的引入方式:xmlns:前缀名
引入aop之前:
引入aop之后:
仔细观察一下,还是可以发现规律的。
以 命名空间aop为例:
(一)Spring配置文件的最简格式
<?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-4.0.xsd"> </beans>
以上就是最简格式。对于XML文件,xmlns、xmlns:xsi、xsi:schemaLocation一般都是必须的。对于Spring配置文件,beans这个命名空间是不可缺少的,并且会把beans作为默认的命名空间。
(二)关于XML文件
(1)在XML中,用xmlns或者xmlns:prefix指定命名空间(2)xmlns指定的命名空间是默认的命名空间,xmlns=”http://www.springframework.org/schema/beans”表示默认命名空间
(3)xmlns:xsi这个命名空间的prefix为xsi,因此这个命名空间里面的元素或者属性就必须要以xsi:这种方式来写,比如schemaLocation就是他的一个属性,所以写成xsi:schemaLocation
(4)xmlns是该XML文件默认的命名空间;
xmlns:xsi是指该XML文件遵守xml规范;
xsi:schemaLocation是指具体用到的schema资源
(5)一般地,xml文件都需要写xmlns, xmlns:xsi, xsi:schemaLocation这3项。
(三)beans已经引入,另引入aop,util,context,tx这4个命名空间
<?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:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/ 4000 util/spring-util-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> </beans>
(四)命名空间该怎么写?它对应的xsi:schemaLocation又该怎么写呢?
(1)在Spring4中,一共有如下命名空间(2)会把beans作为默认的命名空间引入
xmlns="http://www.springframework.org/schema/beans"
(3)其他命名空间的引入方式:xmlns:前缀名
引入aop | xmlns:aop=”http://www.springframework.org/schema/aop” |
---|---|
引入context | xmlns:context=”http://www.springframework.org/schema/context” |
引入tx | xmlns:tx=”http://www.springframework.org/schema/tx” |
引入util | xmlns:util=”http://www.springframework.org/schema/util” |
引入aop之后:
(五)xsi:schemaLocation的写法
先来观察一下:xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd">
仔细观察一下,还是可以发现规律的。
以 命名空间aop为例:
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd[/code]
①前一句是命名空间,后一句是该命名空间的schama位置
②http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 的含义:Spring源码包下,schema下,aop下的spring-aop-4.0.xsd文件。在aop下有不同版本的xsd文件,建议使用最新的吧
你可以打开这些xsd文件看看,这里面也写了较多的命名空间,可供拷贝。
相关文章推荐
- spring---------配置文件的命名空间
- spring的xml配置文件中命名空间
- Spring配置文件命名空间
- eclipse中spring配置文件的自动提示和命名空间的添加
- eclipse中spring配置文件的自动提示和命名空间的添加
- Spring学习1--配置文件的命名空间
- spring配置文件命名空间读取顺序
- eclipse安装spring tool suite插件便于xml配置文件选择命名空间添加
- eclipse中配置Spring配置文件自动提示和命名空间
- SPRING---------配置文件的命名空间
- spring---------配置文件的命名空间
- eclipse中spring配置文件的自动提示和命名空间的添加
- spring 配置文件 常使用的命名空间
- [原]eclipse中spring配置文件的自动提示和命名空间的添加
- 【spring】配置文件命名空间写法
- spring配置文件 --可恶的命名空间
- Spring配置文件的命名空间URI
- 在Spring的XML文件中通过命名空间配置的标签是怎么解析的?
- eclipse中spring配置文件的自动提示和命名空间的添加
- spring2.5之后为了简化命名空间.xml文件的配置,引入p命名空间,需要先导入命名空间