Spring xml配置文件头解析
2017-12-20 14:19
176 查看
最简洁xml文件头
详解
命名空间
头解析
模式内容
Spring的xml配置虽然比较繁琐,不如推荐使用的注解方式简介,但是能读懂xml配置文件还是很重要的,尤其是对于一些老系统的维护,几乎不可避免要面对xml配置文件的问题。现在我们就从默认的xml文件头说起。
首先,需要了解的是xml本身的一些知识,这些知识和spring无关。
xml的命名空间用
其中,url of name是完整命名空间,而prefix是命名空间的别名。
最后一个定义比较长:
详解
命名空间
头解析
模式内容
Spring的xml配置虽然比较繁琐,不如推荐使用的注解方式简介,但是能读懂xml配置文件还是很重要的,尤其是对于一些老系统的维护,几乎不可避免要面对xml配置文件的问题。现在我们就从默认的xml文件头说起。
最简洁xml文件头
以下可谓是最简洁的spring文件头的内容,然而看起来依旧一大坨,令人眼花缭乱。其实细细分析,就会觉得东西其实并不多。<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="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"> <context:component-scan base-package="cn.edu.buaa" /> </beans>
首先,需要了解的是xml本身的一些知识,这些知识和spring无关。
详解
命名空间
命名空间这个概念,和c++,java等都是一样的,无非是想解决重名的东西的具体归属问题,就好像你有Student.class,我也有Student.class,具体用的是哪个Student.class,只要带上前面的包名(相当于命名空间),就可以知道到底是谁的Student。xml的命名空间用
xmlns定义,一读便知是
XML NameSpace的字母缩写。
语法:xmlns:[prefix]="[url of name]"
其中,url of name是完整命名空间,而prefix是命名空间的别名。
头解析
xmlns="http://www.springframework.org/schema/beans":声明xml文件默认的命名空间,其后凡是没有使用命名空间的标签,全是来自这个命名空间;
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance":引入XMLSchema-instance命名空间,并简称为xsi,显然也是取其首字母组成的;
xmlns:context="http://www.springframework.org/schema/context":同上,引入一个context命名空间,简称为context;
最后一个定义比较长:
xsi:schemaLocation="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"[/code] 首先xsi:schemaLocation代表刚刚引入的XMLSchema-instance(简称xsi)中的schemaLocation属性,它的作用是将多个<命名空间,模式位置>对对应起来,其中命名空间和模式位置之间使用空白符分隔。所以最后一个定义的意思就是,命名空间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/beans网站下看看,有spring-beans-2.0.xsd、spring-beans-2.5.xsd、spring-beans-3.0.xsd、spring-beans-3.1.xsd、spring-beans-4.3.xsd、spring-beans.xsd等多种模式,而这里使用的是spring-beans.xsd这一模式,spring-context.xsd同理。<context:component-scan base-package="cn.edu.buaa" />:具体参见下一小节。模式内容
具体使用的spring-beans.xsd和spring-context.xsd有什么用呢?
可以去http://www.springframework.org/schema/beans/spring-beans.xsd查看spring-beans.xsd的内容,其中定义了很多属性,比如<xsd:element name="beans">,这意味着xml中可以使用<beans>标签。至此,xml文件中终于可以使用spring的东西了!
spring的所有东西几乎都是可以从这些定义文件里面找出来的。比如beans的<xsd:documentation>属性向我们展示了beans的文档说明:The default 'lazy-init' value; see the documentation for the 'lazy-init' attribute of the '<bean>' element.。beans下还通过<xsd:attribute name="default-lazy-init" default="false" type="xsd:boolean">定义了一个default-lazy-init属性,类型是boolean,默认值是false,它的文档解释为The default 'lazy-init' value; see the documentation for the 'lazy-init' attribute of the '<bean>' element.,告诉我们想了解详情,具体参见bean下的lazy-init。继续挖掘,找到<xsd:attribute name="lazy-init" default="default" type="defaultable-boolean">,终于通过其doc:Indicates whether or not this bean is to be lazily initialized. If false, it will be instantiated on startup by bean factories that perform eager initialization of singletons. The default is "false". Note: This attribute will not be inherited by child bean definitions. Hence, it needs to be specified per concrete bean definition.,知道了作用就是是否将这个bean设置为迟加载,如果不是,就在启动的时候通过bean工厂初始化为一个单例,且这个属性的设置不会被子bean继承,因此每个子bean需要设置自己所需要的值。
至此,<context:component-scan base-package="cn.edu.buaa" />的意义也不难理解了。这是一个context命名空间下的component-scan的属性。而之前的xsi:schemaLocation告诉我们可以在http://www.springframework.org/schema/context/spring-context.xsd里找到它的定义。
所以说,这些模式的文件内容其实相当于成为了一个非常有用的wiki。
相关文章推荐
- Spring xml配置文件头解析
- Spring源码分析3 — spring XML配置文件的解析流程
- Spring xml配置文件头解析
- Spring xml配置文件头解析
- Web.xml文件 - 工程初始化配置文件解析
- ibatis源码学习2_初始化和配置文件解析
- XML形式的配置文件解析框架以及自动代码生成(一)
- Redis配置文件解析
- 配置文件中“键=值”(key=val)字符串C语言解析
- Android读取init.rc配置文件parse_config函数解析
- 【转】Spring Boot干货系列:(二)配置文件解析
- Python实现加载及解析properties配置文件的方法
- C语言配置文件解析库——iniparser
- sphinx 配置文件全解析
- monit 源码之配置文件解析
- python 解析配置文件
- WINCE的内存配置-config.bib文件的解析
- 在python中实现自定义配置文件解析及应用技巧[yaml and ConfigParser] 推荐
- 配置文件中提供解析带单位数字功能的工具类ByteNumber
- web配置文件中<authentication> 元素解析