Spring配置文件之引入命名空间

如何为Spring配置文件引入命名空间是我在初接触Spring时有点头疼的问题。现在稍作整理

(一)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/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之前:
这里写图片描述
引入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

①前一句是命名空间,后一句是该命名空间的schama位置
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 的含义:Spring源码包下,schema下,aop下的spring-aop-4.0.xsd文件。在aop下有不同版本的xsd文件,建议使用最新的吧
这里写图片描述
你可以打开这些xsd文件看看,这里面也写了较多的命名空间,可供拷贝。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值