关于application.xml 头部文件,看了N多次,也copy了N多次,但心里从来没有感觉清晰了解过。
今天来彻底将他弄清楚先。
先来看看正常的spring application.xml例子
<?xml version="1.0" encoding="UTF-8"?>
<beans
1xmlns="http://www.springframework.org/schema/beans"
2xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3xmlns:p="http://www.springframework.org/schema/p"
4xmlns:aop="http://www.springframework.org/schema/aop"
5xmlns:context="http://www.springframework.org/schema/context"
6xsi:schemaLocation="
7http://www.springframework.org/schema/beans
8http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
9http://www.springframework.org/schema/aop
10http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
11http://www.springframework.org/schema/context
12http://www.springframework.org/schema/context/spring-context-4.3.xsd">
13<aop:aspectj-autoproxy proxy-target-class="true"/>
14<context:component-scan base-package="com.test"></context:component-scan>
15<description>springxml简介</description>
16<bean id="hellospring" class="test.HelloSpring">
17<property name="msg" value="Hello Spring!"/>
18</bean>
</beans>
要透彻了解这里内容,我们可能先得了解 xmlns xmlns:xsi xmlns:aop xsi:schemaLocation 这几个字符的意义。
先简单快速描述一下这几个字符含义,没看懂没关系,后面会详细解说:
xmlns : xml name space xml命名空间
xmlns:aop 简称为aop的命名空间
xmlns:xsi xml schema instance xml 模板样例
xsi:schemaLocation 为xsi的属性 xml 模板样例的位置
下面我们来整行理解:
一、看第4行 4xmlns:aop="http://www.springframework.org/schema/aop"
这行意义是定义了一个命名空间,
其中aop 为命名空间的简称
http://www.springframework.org/schema/aop 为命名空间的标识符
为什么要有命名空间?为什么要有命名空间简称?为什么要有命名空间唯一标识符?
命名空间实际上可以理解为一个区域,而这个区域里面定义了很多标签,可以形象理解为文件夹,一个文件夹下面有很多个文件(标签)。而文件夹需要有一个名字,这就是命名空间需要有一个唯一标识符("http://www.springframework.org/schema/aop"),而这个标识符太长了,所以我们需要给它一个简称”aop”。这个简称很有用,比如我们第13行
13<aop:aspectj-autoproxy proxy-target-class="true"/> 表示意思就是引用了命名空间aop简写,我们总不能每次引用该命名空间时候都这么写:
<http://www.springframework.org/schema/aop:aspectj-autoproxy proxy-target-class="true"/>
那太麻烦了。
上面提到命名空间是定义了一组标签的域,那么他的标签放在在哪里呢?
比如你自己杜撰一个标签
<aop:qtmd proxy-target-class=”true”> 那么eclipse马上回报错
Open quote is expected for attribute "proxy-target-class" associated with an element type "aop:qtmd".
为什么,那是因为aop 命名空间下不存在 qtmd 这个标签和属性,那eclipse怎么知道呢,
这里首先要看第二行:
2xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
这行意思是 xml schema instance xml 模板样例 ,意思此根标签<beans> 下面的所有标签,必须遵循xml模板样例的标准,那这个模板样例标准放在哪里呢?
这个看第6 到 12行
6xsi:schemaLocation="
7http://www.springframework.org/schema/beans
8http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
...
首先xsi:schemaLocation 表示意思为xml schema instance location ,意思是xml模板样例位置,
后面跟着7 、8 行 表示的是命名空间标识符为http://www.springframework.org/schema/beans
的模板位置为http://www.springframework.org/schema/beans/spring-beans-4.3.xsd后面意思同样,这里我们看到 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 是一个连接,我们程序真的会去访问互联网获取吗?为什么我电脑不联网也没有报错呢,所以很明显,程序其实并没有去访问外网,而是在这里:
Spring 每个jar包下面有META-INF目录,其中spring.schemas 文件中定义了xml模板位置
这时候我们就真正找到了模板,
所以我们找到,简称为aop ,唯一标识符为 http://www.springframework.org/schema/aop
的命名空间的模板文件在spring-aop.jar 下面的目录
org/springframework/aop/config/spring-aop-4.3.xsd ,打开该文件可以看到定义了很多标签元素以及属性,而然这里并没有找到一个叫”qtmd”的标签元素,所以你自己杜撰一条
<aop:qtmd proxy-target-class=”true”> 肯定就报错啦 !
至此,我们已经搞明白了下面几个标签意义:
2xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4xmlns:aop="http://www.springframework.org/schema/aop"
6xsi:schemaLocation="
7http://www.springframework.org/schema/beans
8http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
13<aop:aspectj-autoproxy proxy-target-class="true"/>
明白了为什么自己杜撰一个标签<aop:qtmd proxy-target-class=”true”>会报错。
那么理解其他的就很easy了
二、接下来我们看第1行
1xmlns="http://www.springframework.org/schema/beans"
这个表示意思为,默认命名空间,看第15行,
15<description>springxml简介</description>
这个标签并没有跟前面一样需要加一个aop,<aop: 这种
没有命名空间简称,那么他遵循的命名空间唯一标识符为
"http://www.springframework.org/schema/beans" 同样,他的模板文件位置为
8http://www.springframework.org/schema/beans/spring-beans-4.3.xsd ,根据上面方法
在spring-bean jar包下面对应目录可以找到其模板文件。
同样如果你随意写一个标签
<nihao name = “zhangshan”> 这种也会报错,因为spring-beans-4.3.xsd这个文件模板中不存在”nihao”这个标签元素。
三、4xmlns:aop="http://www.springframework.org/schema/aop"
对于这个命名空间,唯一标识符 "http://www.springframework.org/schema/aop"
是可以随意定义的吗?
当然不是,这个需要找到对应的xsd文件,org/springframework/aop/config/spring-aop-4.3.xsd
打开它可以看到:
其中这行
targetNamespace="http://www.springframework.org/schema/aop"
表示xsd对外的命名空间唯一标识符,即在xml中定义时候,需要使用这个来作为他的唯一标识符。
四、剩下最后一行我们没解释了
3xmlns:p="http://www.springframework.org/schema/p"
这行意义是缩小xml体量。
比如没有添加该命名空间时你需要这么写:
<bean id="student" class="com.test.Student">
<property name="name" value="zhangshan">
<property name="age" value="18">
</bean>
添加后可以这么写
<bean id="student" class="com.test.Student" p:name="zhangshan" p:age="18"/>