0044 spring框架的applicationContext.xml的命名空间
Spring框架中,创建bean,装配bean,事务控制等,可以用xml配置或者注解扫描的方法实现。如果用注解扫描,在xml配置中得加上
<context:component-scan base-package="XXX" />
<mvc:annotation-driven />
<tx:annotation-driven transaction-manager="XXX" />
但以上配置生效的前提,还要配置好相应的命名空间,比如:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd ">
。。。
</beans>
那这些命名空间又是什么,以tx为例:
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd "
『xmlns:tx="http://www.springframework.org/schema/tx"』声明了一个命名空间,tx就代表了『http://www.springframework.org/schema/tx』,后者看起来像个url,其实只是个字符串,之所以写成url的样子,只是为了确保唯一,就类似于包名要用域名的反写+项目名+模块名一样
『xsi:schemaLocation="http://www.springframework.org/schema/tx【空格】http://www.springframework.org/schema/tx/spring-tx-4.0.xsd "』指定了用于解析和校验xml文件的xsd文件的位置,那如何找到这个xsd文件呢
把tx.jar解压缩后,在META-INF目录下有一个spring.schemas文件,打开后如下所示:
http\://www.springframework.org/schema/tx/spring-tx-2.0.xsd=org/springframework/transaction/config/spring-tx-2.0.xsd
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/tx/spring-tx-3.0.xsd=org/springframework/transaction/config/spring-tx-3.0.xsd
http\://www.springframework.org/schema/tx/spring-tx-3.1.xsd=org/springframework/transaction/config/spring-tx-3.1.xsd
http\://www.springframework.org/schema/tx/spring-tx-3.2.xsd=org/springframework/transaction/config/spring-tx-3.2.xsd
http\://www.springframework.org/schema/tx/spring-tx-4.0.xsd=org/springframework/transaction/config/spring-tx-4.0.xsd
http\://www.springframework.org/schema/tx/spring-tx-4.1.xsd=org/springframework/transaction/config/spring-tx-4.1.xsd
http\://www.springframework.org/schema/tx/spring-tx-4.2.xsd=org/springframework/transaction/config/spring-tx-4.2.xsd
http\://www.springframework.org/schema/tx/spring-tx-4.3.xsd=org/springframework/transaction/config/spring-tx-4.3.xsd
http\://www.springframework.org/schema/tx/spring-tx.xsd=org/springframework/transaction/config/spring-tx-4.3.xsd
这里有xsd文件的位置的名称跟具体xsd文件所在目录的映射,到『org/springframework/transaction/config/』目录下就可以找到对应的xsd文件,打开后基本如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns="http://www.springframework.org/schema/tx" <!-- 与上面的xmlns:tx的值相同 -->
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tool="http://www.springframework.org/schema/tool"
targetNamespace="http://www.springframework.org/schema/tx"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans" schemaLocation="http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"/>
<xsd:import namespace="http://www.springframework.org/schema/tool" schemaLocation="http://www.springframework.org/schema/tool/spring-tool-4.0.xsd"/>
<xsd:annotation>
.................
</xsd:annotation>
<xsd:element name="advice"> <!-- 引入这个命名空间后,才能用<tx:advice><tx:annotation-driven>等 -->
.................
</xsd:element>
<xsd:element name="annotation-driven">
.................
</xsd:element>
<xsd:element name="jta-transaction-manager">
.................
</xsd:complexType>
</xsd:schema>
如何引入一个命名空间呢?
1. 声明:xmlns:tx="命名空间" 这里命名空间跟xsd文件的xmlns的值相同
2. 在xsi:schemaLocation指定xsd文件的位置:命名空间【空格】config目录下的.schemas的映射
0044 spring框架的applicationContext.xml的命名空间的更多相关文章
- Spring 框架的 applicationContext.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Spring 框架配置web.xml 整合web struts
package cn.itcast.e_web; import java.io.IOException; import javax.servlet.ServletContext; import jav ...
- 使用spring框架,用xml方式进行bean装配出现“The fully qualified name of the bean's class, except if it serves...”
使用spring框架,用xml方式进行bean装配出现“The fully qualified name of the bean's class, except if it serves...”. 原 ...
- Spring的配置文件ApplicationContext.xml配置头文件解析
Spring的配置文件ApplicationContext.xml配置头文件解析 原创 2016年12月16日 14:22:43 标签: spring配置文件 5446 spring中的applica ...
- Spring框架配置beans.xml扩展
Spring学习笔记(二) 续Spring 学习笔记(一)之后,对Spring框架XML的操作进行整理 1 什么是IOC(DI) IOC = inversion of control 控制反转 D ...
- Spring框架配置beans.xml
Spring学习笔记(一) 一.Spring 框架 Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 ...
- 【转载】Spring中的applicationContext.xml与SpringMVC的xxx-servlet.xml的区别
一直搞不明白两者的区别. 如果使用了SpringMVC,事实上,bean的配置完全可以在xxx-servlet.xml中进行配置.为什么需要applicationContext.xml?一定必须? 一 ...
- Spring中,applicationContext.xml 配置文件在web.xml中的配置详解
一.首先写一下代码结构. 二.再看web.xml中的配置情况. <?xml version="1.0" encoding="UTF-8"?> < ...
- 【Spring学习笔记-3.1】让bean获取spring容器上下文(applicationContext.xml)
*.hl_mark_KMSmartTagPinkImg{background-color:#ffaaff;}*.hl_mark_KMSmartTagBlueImg{background-color:# ...
随机推荐
- Endnote在latex中的应用的两种方法
从endnote中向latex文档批量插入参考文献的两种方法 一.若是latex模板中参考文献编写的命令是: \begin{thebibliography} \bibitem{lab1}LIU M L ...
- LESS,强大的CSS预处理语言
虽然写的css不多,但是我已经切身感觉到了书写css的恶心...抛开最令人烦的浏览器兼容问题不说,这个语言本身就有不少问题. 最简单的,比如多个地方是同一个颜色的,如果可以写在一个样式里还没什么,但是 ...
- 对REST的理解
现在标准服务基本都提供REST化的接口了.为了加强对REST的理解,看了这篇文章: http://kb.cnblogs.com/page/186516/ REST架构风格最重要的架构约束有6个: 客户 ...
- 关于Java的File类、字节流和字符流
一.File类: 在Windows下的路径分隔符(\)和在Linux下的路径分隔符(/)是不一样的,当直接使用绝对路径时,跨平台会报No Such file or diretory异常. File中还 ...
- Python 面试中 8 个必考问题(转载)
Python 面试中 8 个必考问题 1.下面这段代码的输出结果是什么?请解释. def extendList(val, list=[]): list.append(val) return list ...
- PHP5.3下加速器ZendGuardLoader安装 (LNMP/lnmpa)
PHP5.3下加速器ZendGuardLoader安装 (LNMP/lnmpa) 由于Zend新产品ZendGuardLoader的面世,Zend Optimizer已经不支持php5.3了,官方给出 ...
- DevExpress 编译成功的 dll
DevExpress 编译成功的 dll 附注册方法 其它的有些文件没有,如: DevExpress.EasyTest 这个程序集找不到 希望有的朋友可以分享一下, 安装程序集 通常,将程序集安装到G ...
- Junit测试Spring应用Dubbo测试框架之-Excel 工具类
package com.tree.autotest.demo; import com.alibaba.fastjson.JSON;import org.apache.poi.hssf.usermode ...
- .html(),.text()和.val()的差异
.html(),.text()和.val()的差异总结: .html(),.text(),.val()三种方法都是用来读取选定元素的内容:只不过.html()是用来读取元素的html内容(包括htm ...
- 防止继承和覆盖(PHP类)
可能出现需求:我们不希望继承的类覆盖abstract类中的某个方法. 解决方案:我们可以在某个方法前面加上final关键词,可以防止继承的类覆盖它并实现继承类自己的版本. 继承类仍然可以访问和调用这些 ...