Spring框架名声在外的特点, IoC(Inverse of Control) 控制反转和面向切面AOP(Aspect Oriented Program),使用方便,扩展性极强,现在已经发展了很多受欢迎的分支项目。这里我主要对Spring MVC框架xml配置文件进行理解。能够读懂xml配置文件的含义,以便在应用中更好的应对配置文件引起的异常。来来, 盆友们看过来。
- 配置文件示例:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.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-4.3.xsd">
<context:component-scan base-package="com.calm.login.dao"/>
......
<tx:advice id="txAdvice" transaction-manager="transactionManager">
......
</tx>
</bean>
- 上面是常见的Spring配置文件, 在这里完成bean的配置, 这些bean将由Spring容器管理。具体bean的注入配置不做赘述, 这里需要关注的可以分为下面的两部分:
- 命名空间声明
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.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-4.3.xsd"
- xmlns
xmlns 是xml 命名空间(xml name space), 用来标识唯一的文件,防止重复或者指向不清晰,命名空间的声明语法是,xmlns : 别名 = namespace的统一资源标识符(URI):
xmlns:namespace-aliases="namespaceURI"
例如上述命名空间声明中:
<!-- 没有别名,是默认的命名空间-->
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<!-- 别名是context-->
xmlns:context="http://www.springframework.org/schema/context"
所以别名只作为该命名空间的标识,是可以自定义, 例如:
xmlns:ttt="http://www.springframework.org/schema/context"
相应的当使用该命名空间时也要进行对应的更改:
<!--包扫描-->
<ttt:component-scan base-package="com.calm.login.dao"/>
-
xmlns:xsi
大家注意到下面的两条有点不同
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.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd"
xsi也是一个别名,只是这个别名约定俗成,语意性强,大家容易看懂。是(xml schema instance)的缩写,指定schema资源文件里定义的元素所准守的标准和规范。所以xsi需要明确通过属性schemaLocation来指定这些xml协议定义的具体文件。
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"
xsi:schemaLocation指定协议具体的路径,因为一个命名空间下还会存在多个版本的xsd(Xml Schema Definition 协议定义)文件的需要进行具体的指定如:

这是存在于本地的xsd定义文件,会对配置文件进行验证。还可以复制命名空间到浏览器中查看对应的不同版本的协议定义文件。
