spring applicationContext.xml 文件头的配置说明

环境及方法

spring 4.2.7
使用log4j 记得打开debug模式.而且自己把java类及方法多少行都打印出来

头文件举例

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

xmlns:xxx 后面的是命名空间
xsi:schemaLocation 中每一个命名空间对应一个xsd文件.
xmlns=”http://www.springframework.org/schema/beans” 默认命名空间

使用不同的标签要配置spring不同的组件

比如

对应
xmlns:context=”http://www.springframework.org/schema/context”
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
但是程序是如何寻找xsd.来验证xml中自己配置的合法性.已经xsd的版本如何选择.

寻找

就以context为例.我们寻找到spring-context-4.2.7.Release.jar找到
org.springframework.context.config包下发现.
spring-context-2.5.xsd
spring-context-3.0.xsd
spring-context-3.1.xsd
spring-context-3.2.xsd
spring-context-4.0.xsd
spring-context-4.1.xsd
spring-context-4.2.xsd
这里面能够找到一个与上面配置一样的xsd文件.但是这么多,spring为什么要保留这么多.而不用一个最新的就可以了.据说是因为.当spring版本升级的时候,可以不用修改xml文件(到底是不是这个原因,还真不知道.)
但是大家又看到了,我自己每次配置去寻找这个.xsd吗?而且我自己现在的版本是4.2.7.但是并没有.
有人可能要说,人家取前两个数字的版本.但是我记得spring3.5这个版本是有的.并没有发现3.5版本的xsd
那么到底我们配置的时候要怎么配置.

不配置版本号

经研究发现.配置还有别的地方.而且我们配置的时候网络路径.如果连接不上网.那岂不是很尴尬.
经过查看spring启动的log发现.

Loading schema mappings from [META-INF/spring.schemas]
Loaded schema mappings:{http://www.springframework.org/schema/tx/spring-tx-2.5.xsd=org/springframework/transaction/config/spring-tx-2.5.xsd, http://www.springframework.org/schema/cache/spring-cache-4.2.xsd=org/springframework/cache/config/spring-cache-4.2.xsd, http://www.springframework.org/schema/aop/spring-aop-4.1.xsd=org/springframework/aop/config/spring-aop-4.1.xsd, http://www.springframework.org/schema/context/spring-context-3.1.xsd=org/springframework/context/config/spring-context-3.1.xsd, http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd=org/springframework/jdbc/config/spring-jdbc-4.1.xsd, http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd=org/springframework/web/servlet/config/spring-mvc-4.1.xsd, http://www.springframework.org/schema/util/spring-util-3.0.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd, 
.....................后面太多,我省略掉了

很明显,我们虽然配置的是网络路径,但是log中有一个mapping可以去本地的classpath中寻找.
但是还有一个问题.我们还是不知道如何配置版本.
可以去百度搜索(spring xsd文件最好不要配置版本)
那么到底是为什么那?
上面我省略的太多.我们可以看一个jar下的spring.schemas文件.

{http\://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsd
http\://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsd
http\://www.springframework.org/schema/context/spring-context-3.1.xsd=org/springframework/context/config/spring-context-3.1.xsd
http\://www.springframework.org/schema/context/spring-context-3.2.xsd=org/springframework/context/config/spring-context-3.2.xsd
http\://www.springframework.org/schema/context/spring-context-4.0.xsd=org/springframework/context/config/spring-context-4.0.xsd
http\://www.springframework.org/schema/context/spring-context-4.1.xsd=org/springframework/context/config/spring-context-4.1.xsd
http\://www.springframework.org/schema/context/spring-context-4.2.xsd=org/springframework/context/config/spring-context-4.2.xsd
http\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-4.2.xsd
此处仍然省略一些.
}

我们看到当我们不配置版本号的xsd的时候.那么会去自动寻找jar最新版本的xsd文件.
好了,到此大家明白了spring xml文件头文件的设置
怎么配置,以及到底要不要版本号的问题.

给出正确的配置版本

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

当然spring2.0之前还在用dtd文件不是这个原理.

新的发现

当在applicationContext.xml import其他xml文件的时候.比如

下面是文件头

<?xml version="1.0" encoding="UTF-8"?>
<b:beans xmlns:b="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/security" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

会发现.文件的根节点不在是 而是
而且xmlns发生了变化.也就是说这个xml的默认命名空间已经不是在beans的按个uri了.而是
http://www.springframework.org/schema/security
而要想像applicationContext.xml中增加一个bean的话.
则变成了这样.

<b:bean id="defaultAccessDeniedHandler" class="com.renderbus.cm.security.DefaultAccessDeniedHandler"></b:bean>
    原文作者:hassen2010
    原文地址: https://blog.csdn.net/harrison2010/article/details/61248811
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞