spring配置文件头部xmlns配置精髓

<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	   xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	   xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
	   xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
		http://www.springframework.org/schema/task 
		http://www.springframework.org/schema/task/spring-task-3.1.xsd">

  使用spring已经好几年了,可是每次遇到要本身配置spring项目时就头疼,经过网络各类复制别人的配置文件,而后一不当心就报错了,因此今天想探探究竟。java

  如上是一个spring-context配置文件spring

xmlns部分:spring-mvc

1.网络

xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  这个是每一个配置文件必须的部分,也就是spring的根本。mvc

  声明xml文件默认的命名空间,表示未使用其余命名空间的全部标签的默认命名空间。spa

  声明XML Schema 实例,声明后就可使用 schemaLocation 属性了。code

2.xml

xmlns:aop="http://www.springframework.org/schema/aop"

  这个就是spring配置文件里面须要使用到aop的标签,声明前缀为aop的命名空间,后面的URL用于标示命名空间的地址不会被解析器用于查找信息。其唯一的做用是赋予命名空间一个唯一的名称。当命名空间被定义在元素的开始标签中时,全部带有相同前缀的子元素都会与同一个命名空间相关联。而后其余好比context(针对组件标签)、MVC(针对mvc标签)、tx(针对事务标签)都同样的意思。blog

 

xsi:schemaLaction部分:事务

1.

http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

是为上面配置的命名空间指定xsd规范文件,这样你在进行下面具体配置的时候就会根据这些xsd规范文件给出相应的提示,好比说每一个标签是怎么写的,都有些什么属性是均可以智能提示的,以防配置中出错而不太容易排查,在启动服务的时候也会根据xsd规范对配置进行校验。可是这里须要为你上面xmlns里面配置的mvc、aop、tx等都配置上xsd规范文件。

相关文章
相关标签/搜索