Spring xml配置文件头解析

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"

首先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。

猜你喜欢

转载自blog.csdn.net/puppylpg/article/details/78291176