这个就要从XML说了,Spring的配置管理能够利用XML方式进行配置,而XML里面就有命名空间这个概念。。实际上就和标签的意思有点像 你给一个命名空间之后,这个XML文件里面就能够用那个命名空间上下文里面的标签了。简化配置用,你能够去看看Spring AOP用命名空间和不用命名空间的配置有什么区别。spring
使用Spring 的命名空间p 装配属性spring-mvc
使用<property> 元素为Bean 的属性装配值和引用并不太复杂。尽管如此,Spring 的命名空间p 提供了另外一种Bean 属性的装配方式,该方式不须要配置如此多的尖括号。mvc
命名空间p 的schema URI 为http://www.springframework.org/schema/p。若是你想使用命名空间p,只须要在Spring 的XML 配置中增长以下一段声明:ui
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
经过此声明,咱们如今可使用p: 做为<bean> 元素全部属性的前缀来装配Bean 的属性。为了示范,咱们从新声明了kenny Bean 的配置:code
- <bean id="kenny" class="com.springinaction.springidol.Instrumentalist"
- p:song = "Jingle Bells"
- p:instrument-ref = "saxophone" />
p:song 属性的值被设置为“Jingle Bells”,将使用该值装配song 属性。一样,p:instrument-ref 属性的值被设置为“saxophone”,将使用一个ID 为saxophone 的Bean 引用来装配instrument 属性。-ref 后缀做为一个标识来告知Spring 应该装配一个引用而不是字面值。xml
选择<property> 仍是命名空间p 取决于你,它们是等价的。命名空间p 的最主要优势是更简洁。在固定宽度的纸张上编写样例时,选择命名空间相对更合适。所以,在本书中你可能看到我不时的使用命名空间p,特别是水平页面空间比较紧凑时。blog
Spring 的配置文件中,有一个配置文件头:get
<beans xmlns=”http://www.springframework.org/schema/beans”
xsi:schemaLocation=”
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>io
这代表,在当前配置文件中,使用的是beans命名空间,能够直接使用<bean id=”">。class
若是要在这个配置文件中使用mvc命名空间下的annotation-driven元素,要写为<mvc:annotation-driven/>,固然,还须要告诉xml解析器,mvc这个命名空间是在哪里定义的,以便解析器可以验证当前文件中mvc:开头的元素是否符合mvc命名空间的:
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:mvc=”http://www.springframework.org/schema/mvc”
xsi:schemaLocation=”
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd”>
这样解析器在解释mvc:命名空间的时候,会参考spring-mvc-3.0.xsd这个文件来检验<mvc:annotation-driven/>是否合格。