【Spring】对于spring配置文件中前缀的一些简单见解

今天突然看到自己的一个小笔记是关于spring配置文件前缀的总结,心血来潮就详细的写写吧=。=

啥?你问我spring是啥?对不起,咱们不认识=。=哈哈

做java的小伙伴对spring可谓是再熟不过了,那可是所谓的大头龙哈,不过,可能有些小伙伴只停留在会用的层面,那么今天呢,咱们简单来聊聊spring配置文件头部标签前缀​这点事。

下面的代码是不是可熟悉=。=

<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:context="http://www.springframework.org/schema/context"                     
       xmlns:p="http://www.springframework.org/schema/p"
	   xmlns:aop="http://www.springframework.org/schema/aop"     
       xmlns:tx="http://www.springframework.org/schema/tx"
	   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.2.xsd
	                       http://www.springframework.org/schema/context             
                           http://www.springframework.org/schema/context/spring-context.xsd
	                       http://www.springframework.org/schema/aop                 
                           http://www.springframework.org/schema/aop/spring-aop.xsd 
	                       http://www.springframework.org/schema/tx 
                           http://www.springframework.org/schema/tx/spring-tx.xsd
	                       http://www.springframework.org/schema/util         
                           http://www.springframework.org/schema/util/spring-util.xsd">

</beans>

一、

那就先从xmlns说起吧:

xmlns是XML Namespaces的缩写,中文名称是XML命名空间。
XML 命名空间属性被放置于元素的开始标签之中,并使用以下的语法:

xmlns:namespace-prefix="namespaceURI"

简单来说就是:

命名空间前缀就是一个变量,以简短的名字表示整个命名空间uri。

比如我们常见的springmvc注解扫描配置:<mvc:annotation-driven/>

就是因为我们在开始配置了命名空间mvc,xmlns:mvc=Index of /schema/mvc

我们才可以简单的写成<mvc:annotation-driven/>

这样的写法与<Index of /schema/mvc: annotation-driven/>这种写法是等价的


ps:用于标示命名空间的地址(也就是xmlns后跟的地址)不会被解析器用于查找信息。其惟一的作用是赋予命名空间一个惟一的名称。不过,很多公司常常会作为指针来使用命名空间指向实际存在的网页,这个网页包含关于命名空间的信息

大家可以在地址栏输入:Index of /schema/beans就会发现如下截图

二、★★★★★★★★★★★★★★★★★★★★★

而对于<beans xmlns=Index of /schema/beans这个命名空间而言,

这样是声明了整个配置文件的默认命名空间为beans,

也就是说没有使用其他命名空间的标签都默认使用了这个beans命名空间,

比如我们都知道要创建一个bean对象时,需要写<bean id=”…” class=”…”></bean>

但其实这也是简化了的写法,实际上完整的标签是<beans:bean id=”…” class=”…”></beans:bean>

但我们直接可以省略beans,就是因为我们将beans配置为了整个xml文件的默认命名空间,

当我们需要写的标签前缀是beans开头的,我们就可以直接省略beans,

而对于其他的命名空间,如xmlns:tx=Index of /schema/tx

因为一开头前缀设置的是beans,所以对于tx这个约束,在写配置的时候,仍然要写上<tx>为前缀的标签,

如:<tx:annotation-driven transaction-manager="transactionManager"/>,

但是如果我们在一开头设置的前缀是<tx xmln=Index of /schema/tx,

如果这样的话,那么tx就成了默认的了,

我们在配置的时候就可以直接写<annotation-driven transaction-manager="transactionManager"/>,从而将tx省略掉,

但是这样对于bean标签的配置就需要写全了,就是<beans:bean id=”…” class=”…”></beans:bean>

我想通过上边这样一说,大家对于配置的其他的命名空间就自然而然也就理解了,那aop、tx、mvc、context等就不多说啦

三、

对于xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance来说,这是声明XML Schema 实例,声明后就可以使用 schemaLocation 属性了。

四、

而对于

xsi:schemaLocation="http://www.springframework.org/schema/beans     
                           http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	                       http://www.springframework.org/schema/context             
                           http://www.springframework.org/schema/context/spring-context.xsd
	                       http://www.springframework.org/schema/aop                 
                           http://www.springframework.org/schema/aop/spring-aop.xsd 
	                       http://www.springframework.org/schema/tx 
                           http://www.springframework.org/schema/tx/spring-tx.xsd
	                       http://www.springframework.org/schema/util         
                           http://www.springframework.org/schema/util/spring-util.xsd">

这部分来说,就是为上面声明的命名空间指定xsd规范文件,从而在进行下面具体配置的时候就会根据这些xsd规范文件给出相应的提示,比如每个标签是怎么写的,都有些什么属性都是可以提示出来的,这样从而避免了配置中出错而不太容易排查的问题,在启动服务的时候也会根据xsd规范对配置进行校验。

而你上边xmlns里面配置的所有命名空间如mvc、aop、tx、context等都需要配置上xsd规范文件

当然了在指定xsd规范文件的时候也可以指定具体版本号的xsd文件,当然也可以不加版本号,那么这样就使用了默认值,实质上这个默认值也是有一个具体的版本号的。

五、

那么怎么去查找这些xsd文件的路径呢,这就需要到用到jar包中去找了,比如,我的工程使用的是spring-beans-4.2.4RELEASE.jar,那么打开这个jar包(我是直接在eclipse中打开的),找到META-INF/spring.schemas文件,如下图所示:

打开这个文件你会看到如下图所示这样的一条:

根据=后边的目录地址,就可以找到相应的xsd约束文件,如下图所示:

也就是说最后会定位到这里的xsd文件。

六、

最后一句话总结一下吧:

命名空间前缀就是一个变量,以简短的名字表示整个命名空间uri。

从而将复杂的写法<Index of /schema/mvc: annotation-driven/>可以简单的写成<mvc:annotation-driven/>这种写法

好啦,到这就基本上就聊完啦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值