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:# ...
随机推荐
- 在JAVA中,如何计算两个日期的月份差
package com.forezp.util; import org.joda.time.DateTime; import org.joda.time.Months; import org.joda ...
- Jenkins的slave异常:Exception in thread "main" java.lang.ClassNotFoundException: hudson.remoting.Launcher
当任务分配到slave上执行时,报如下错误: Parsing POMs Established TCP socket on 38257 maven33-agent.jar already up to ...
- JavaScript Dictionary
Excellent. The 4guysfromrolla example is very helpful, thanks. I've pasted a complete javascript.j ...
- JVM调优思路
一.jvm内存调优 (Gc 和 Full gc) hotspot -Xms40m 最小堆内存 -Xmx512m 最大值内存 -verboose:gc -XX:PrintGCDetails -XX: ...
- js清空子元素,创建新的子元素
清空子元素 $('#region').empty(); 添加子元素 var regions = document.getElementById('region'); regions.appendChi ...
- Maven nexus 安装nexus私服出现的两个问题
1. 在win10中安装nexus时提示:wrapper | OpenSCManager failed - 拒绝访问. (0x5) 主要是没有权限.需要以管理员的身份运行 如果你是直接点击 start ...
- 转: 加快Android编译速度
转: http://timeszoro.xyz/2015/11/25/%E5%8A%A0%E5%BF%ABandroid%E7%BC%96%E8%AF%91%E9%80%9F%E5%BA%A6/ 加快 ...
- 云计算之路-试用Azure:上不了高速的跑车,无法跨Cloud Service的DNS服务器
从阿里云的踩坑大师,到Azure的抹黑大师,我们似乎成了云计算负面用户的典型,可是我们还是忍不住想表达自己真实的使用感受.如果有错误的地方,欢迎大家批评! 在Azure上建好虚拟网(Vitual Ne ...
- meta文件是什么东西
meta是用来在HTML文档中模拟HTTP协议的响应头报文.meta 标签用于网页的<head>与</head>中,meta 标签的用处很多.meta 的属性有两种:name和 ...
- PHP投票实现24小时间隔投票
l 设置cookie,浏览器都有禁用或者清除cookie的功能 l 设置session,关闭浏览器就没了 所以,我们只能尽量防止重复投票现象 session_start(); //获取ip地址 i ...