当前位置: 首页 > news >正文

简析:spring配置文档中beans的xmlns和xsi:schemaLocation

xmlns 属性,可以写在xml或者html的开始标签中,比如:

<beans xmlns=""></beans>
或者
<table xmlns=""></table >

它的名称是xml namespace,也就命名空间。

它的作用:
就拿spring来举例,如果你的spring需要配置spring的aop功能和事务tx功能,那么在spring配置文件的beans标签内部你肯定需要用到spring-aop、spring-tx、spring-context中的标签元素。
spring-aop、spring-tx、spring-context中包含哪些标签元素,这是在xsd文件中设定好的,同时也是为了校验配置文件的标签是否正确,比如http://www.springframework.org/schema/context/spring-context.xsd文件定义如下:
在这里插入图片描述
可以看到,其中就包含了component-scan标签元素,加入没有命名空间,那么配置内容就是如下:

<beans><component-scan base-package="com.home" />
</beans>

问题来了,如果存在其他比如它test.xsd文件中也存在component-scan标签元素,这样是不是就冲突了呢?
比如:

<beans><!-- spring-context.xsd --><component-scan base-package="com.home" /><!-- test.xsd --><component-scan test="test" />
</beans>

所以,如果引入其他xsd定义中的标签,则需要分配命名空间。

如何使用:
先来一个spring配置的例子:

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

这里spring需要用到aop和事务tx的功能,他们的标签元素分别定义在spring-aop.xsd和spring-tx.xsd中。
1、xmlns指定引入的命名空间,比如上面引入了context、aop、tx。而xmlns="http://www.springframework.org/schema/beans"表示默认的命名空间是beans。
2、xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” 是默认写法,必须加上,后面才能用到xsi:schemaLocation。
3、xsi:schemaLocation属性指定命令空间的xsd文件路径,它的格式是

xsi:schemaLocation="键 值 键 值 键 值 "

键 值是成对出现的,键是比如 xmlns:aop="http://www.springframework.org/schema/aop"中的http://www.springframework.org/schema/aop,值是spring-aop.xsd的文件路径。
4、配置好xmlns:context和指定xsi:schemaLocation之后,就可以在<beans>标签内部通过命名空间context来使用其标签元素,比如:

<!-- 开启注解 -->
<context:annotation-config />

其他命名空间也是一样的使用方法。

5、介绍下p空间,如下:

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

xmlns:p不需要在xsi:schemaLocation中指定xsd文件路径,它是灵活配置属性名的意思。
比如:

<beans><bean id="test" p:password="123" p:clazz-ref="springTest" />
</beans>

说明:
配置 xmlns:p命名空间后,可以在标签内通过“p:属性名称=值”来灵活配置属性,属性名称需要对应到具体的属性名。如果属性名称中以“-ref”结尾,默认代表这个属性的值是一个类的映射。

http://www.xdnf.cn/news/811477.html

相关文章:

  • github工具_Github Statistics 一个基于 React 的 GitHub 数据统计工具
  • 期货入门必备知识,了解最全的期货知识
  • Lua内存泄露检测原理
  • 新一代 AVS3 视频编码标准
  • Bootstrap中定制LESS-颜色及导航条
  • 如何显著提升小目标检测精度?深度解读Stitcher:简洁实用、高效涨点
  • windows系统下帧率获取工具--fraps
  • Verilog case/casez/casex的区别
  • 手机号验证最新正则表达式
  • 基于单片机智能温控器控制系统设计
  • FreeEIM(飞鸽传书)应用虚拟化系统
  • 51单片机最小系统原理图、PCB及组成原理详解
  • 【简单html静态网页代码】 保护环境网页设计模板 简单学生网页设计 静态HTML CSS网站制作成品
  • 三级网络
  • linux内存的active,Linux性能优化和监控系列(三)——分析Memory使用状况
  • ES15新特性
  • Local System/Network Service/Local Service权限详解
  • js 中文转为首字母拼音
  • javaeye的第一篇blog
  • C++中的多线程
  • CGI与FastCGI
  • ansys fluent udf manual 下载_【FLUENT】Fluent 2020R1中文版(启动方法)
  • C语言基础知识(适合初学者)
  • 10 常见网站安全攻击手段及防御方法
  • 花指令简析
  • PHP包含文件函数include、include_once、require、require_once区别总结
  • 【学习笔记】AD中PCB泪滴设计
  • ms08-067漏洞复现
  • 半监督之mixmatch
  • SHFileOperation复制文件夹、文件用法