一、配置文件说明
Spring容器通过ApplicationContext接口定义,ClassPathXmlApplicationContext是其实现类。Spring配置文件是一个或多个标准的XML文档,applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
</beans>
配置文件的最外层一个<bean>标签包裹,作为根节点 。
①xmlns 默认命名空间
②xmlns:xsi:"http://www.w3.org/2001/XMLSchema-instance"
显示 schema 中用到的元素和数据类型来自命名空间"http://www.w3.org/2001/XMLSchema-instance" ,且规定了命名空间的元素和数据类型应该使用前缀 xsi:
③xmlns:p p命名空间,简化配置。
通常配置文件都是基于setter的依赖注入,也就是使用<property>标签定义属性。
而p命名使用属性,<bean id="" class="" p:属性名="普通值" p:属性名-ref="引用值">。
④xmlns:context 应用上下文,加载配置文件。
⑤xmlns:jee jee标签用来处理javaee标准相关的问题,例如查询一个jndi对象以及定义一个ejb的引用等。
⑥xmlns:aop spring配置文件里面需要使用到aop的标签,声明前缀为aop的命名空间,后面的URL用于标示命名空间的地址不会被解析器用于查找信息。其惟一的作用是赋予命名空间一个惟一的名称。
⑦xmlns:tx spring事务配置,即配置文件中tx标签。
⑧xmlns:jaxrs JAX-RS即Java API for RESTful Web Services,支持REST。
⑨xmlns:jaxws xmlns:cxf webService应用。
(标红没有具体使用过,不太理解,后续深入理解)。
⑩xsi:schemaLocation
之前已经指定了xmlns:xsi命名空间的位置,这里定义了XML Namespace和对应的XSD(Xml Schema Definition)文档的位置的关系,也就是上述的各种标签。值一般是成对的URL,两个URI之间以空白符分隔(空格和换行均可)。第一个URI是定义的XML Namespace的值,第二个URI给出Schema文档的位置。