Spring主配置文件头的配置说明及常见问题处理

最近在重新看Spring,正好看到主配置文件的配置,以前都是找个配置文件的模板,然后改一下版本来用。这次正好查阅了许多资料,整理一下,作为笔记待以后查阅也希望能对初学Spring的朋友有所帮助。

Spring主配置文件的示例

首先先说一个误区,很多初学者会对这个配置文件的名称有疑惑,为什么有的人叫applicationContext.xml,有的却叫xmlApplicationContext.xml,这个名称有什么命名规则么?其实这个主配置文件的名称是没有任何限制的,只要跟你在web.xml中配置的文件名相同即可。
web.xml(配置了多个配置文件):这里的param-name固定为contextConfigLocation。

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/classes/applicationContext.xml,
            /WEB-INF/classes/quartzContext.xml,
            /WEB-INF/spring-cxf.xml
        </param-value>
    </context-param>

主配置文件示例(可将下面代码直接复制到你的主配置文件使用):

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

    <!-- 指定扫描包 -->
    <context:component-scan base-package="com.ugrow"/>

    <bean id="testManager" class="com.ugrow.manager.TestManager">   </bean>
    <bean id="testSpring" class="com.ugrow.controller.TestSpringController"></bean>


</beans>

主配置文件头讲解

我们看上面的主配置文件示例,发现在文件的开始有很多代码,而且看起来都很像,这些是什么?
答:命名空间。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

上面粘贴的这一部分代码应该是大部分初学者认为不清楚的部分,每次使用spring时就把这块复制一下就完事,要问这块代码如何使用的,能不能修改?不知道。(我之前也是^_^)。下面就来讲讲这块内容。
我们细心看一下上面的代码发现,其实这么长的代码,主要包含了三种标签及对应的值:
① xmlns=value
② xmlns:key=value
③ xsi:schemaLocation=value
分别讲一下这三种标签:
①②其实是一类,xmlns是一个引入命名空间的关键词,如xmlns=”http://www.springframework.org/schema/beans” 这个语句就是讲Spring的beans组件命名空间引入,这样主配置文件中就可以使用类似这样的组件了。②的作用同样是引入命名空间。相比较①多的key这一部分的作用,其实是类似别名的意思。如上例中,我们引入了beans,context,aop等多个命名空间,为了防止语义混乱,在使用除beans的其他命名空间下的组件的时候,都需要指明组件的命名空间,这样这里给个key就方便主配置文件中的组件使用了。如:就是使用了context下的组件。
那有些朋友会问,为什么beans不用添加呢?其实原因也很简单,首先beans是spring必须使用的组件,如果每个组件都加一个bean来区分,会很繁琐,所以spring就直接默认命名空间为beans,这样主配置文件中的组件不指明命名空间的就是beans下的组件,方便使用。所以①就是默认命名空间为beans,②为引入指定的命名空间。
那③xsi标签的意思呢?细心的朋友会发现,xsi标签其实是xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”这里引入的。这里的作用是找到命名空间对应的XMLSchema实例。我们来看xsi的值会发现它其实是成对出现的:
(a)http://www.springframework.org/schema/mvc
(b)http://www.springframework.org/schema/mvc/spring-mvc.xsd
(c)http://www.springframework.org/schema/beans (d)http://www.springframework.org/schema/beans/spring-beans.xsd
a和c分别是我们前面引入的命名空间,而b和d则是a和c对应的XMLSchema实例的位置。我们发现b和d对应一个网址,那难道每次加载命名空间的时候都要访问网络上的位置?也不是不可以 嘿嘿。不过一般来说还是会先读取本地的文件的。以b为例,我们来找一下b的位置:首先我们根据名称可以知道这是mvc的jar包,我们找一下jar包:
这里写图片描述
我们发现jar包下META-INF文件夹下有个spring.schemas的文件,打开看一下发现里面有这些内容:

http\://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd=org/springframework/web/servlet/config/spring-mvc-3.0.xsd
http\://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd=org/springframework/web/servlet/config/spring-mvc-3.1.xsd
http\://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd=org/springframework/web/servlet/config/spring-mvc-3.2.xsd
http\://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd=org/springframework/web/servlet/config/spring-mvc-4.0.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/mvc/spring-mvc-4.2.xsd=org/springframework/web/servlet/config/spring-mvc-4.2.xsd
http\://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd=org/springframework/web/servlet/config/spring-mvc-4.3.xsd
http\://www.springframework.org/schema/mvc/spring-mvc.xsd=org/springframework/web/servlet/config/spring-mvc-4.3.xsd

现在就明白了,前面b中的值(网址)其实是在这个配置文件中映射了一个本地文件的。我们根据路径找一下:
这里写图片描述

现在我们就明白了,原来我们引入的命名空间实际是应用了本地的文件。这也是我们要先导入jar包才能用spring组件的原因。

小技巧:
我们找到文件路径的时候发现,较新的jar包中,会包含有之前版本的命名空间文件,这也是为了防止升级spring时出现问题。那我们应该如何选择呢?
方式一:在xsi引入的时候,直接指定确切的版本:http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
方式二:不指定具体版本,让spring自己加载合适的版本(推荐):http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd

配置后遇到的问题

这样关于spring主配置文件的配置就讲完了,是不是很简单。但我实际来配置的时候,却出现了这样几个问题:
(一)context的组件不能用
我实际已经引入了context的命名空间,但是context下面的组件却不能用,而且也没有提示信息。这个问题在网上查了挺多资料,并没有人的答案比较靠谱,于是我在确认了配置信息正常后,突然想到,是不是我引入命名空间后,myecplise并没有把命名空间引入呢?于是我检查了下它的命名空间:
这里写图片描述
发现这里确实没有将context引入。在这里勾选应用后,组件可以正常使用。

(二)配置均正常,但文件顶部报红色叉号
报错如图,只提示参考文件有错误,但没有具体信息:
这里写图片描述

这个问题很让人纠结,后来我想到我们引入的jar包中,命名空间有很多个版本,而我写引入文件时,有的并没有指定具体版本,会不会是因为一个引用对应了多个文件导致的呢?经我测试,果然是因为这个原因。那这样就有两个解决办法:
方法一:为每个命名空间指定具体的版本。
方法二:对myecplise的xml验证做一下调整。preference–>validation–>xml–>Honour all XML schema locations,去掉这个选项的勾选即可。去掉后会使用第一个找到的版本文件就不会冲突了(推荐)。
这里写图片描述

以上就是关于spring配置文件的一些内容,有不正确的地方请指正,希望对大家有所帮助。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值