什么是Spring的命名空间及使用Spring 的命名空间p 装配属性
这个就要从XML说了,Spring的配置管理可以利用XML方式进行配置,而XML里面就有命名空间这个概念。。实际上就和标签的意思有点像 你给一个命名空间以后,这个XML文件里面就可以用那个命名空间上下文里面的标签了。简化配置用,你可以去看看Spring AOP用命名空间和不用命名空间的配置有什么区别。
使用Spring 的命名空间p 装配属性
使用<property> 元素为Bean 的属性装配值和引用并不太复杂。尽管如此,Spring 的命名空间p 提供了另一种Bean 属性的装配方式,该方式不需要配置如此多的尖括号。
命名空间p 的schema URI 为http://www.springframework.org/schema/p。如果你想使用命名空间p,只需要在Spring 的XML 配置中增加如下一段声明:
- <?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 的配置:
- <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 应该装配一个引用而不是字面值。
选择<property> 还是命名空间p 取决于你,它们是等价的。命名空间p 的最主要优点是更简洁。在固定宽度的纸张上编写样例时,选择命名空间相对更合适。因此,在本书中你可能看到我不时的使用命名空间p,特别是水平页面空间比较紧凑时。
Spring 的配置文件中,有一个配置文件头:
<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”>
这表明,在当前配置文件中,使用的是beans命名空间,可以直接使用<bean id=”">。
如果要在这个配置文件中使用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/>是否合格。
什么是Spring的命名空间及使用Spring 的命名空间p 装配属性的更多相关文章
- 使用Spring的命名空间p装配属性-摘自《Spring实战(第3版)》
使用<property>元素为Bean的属性装配值和引用并不太复杂.尽管如此,Spring的命名空间p提供了另一种Bean属性的装配方式,该方式不需要配置如此多的尖括号. 命名空间p的sc ...
- Spring入门(3)-Spring命名空间与Bean作用域
Spring入门(3)-Spring命名空间与Bean作用域 这篇文章主要介绍Spring的命名空间和Bean作用域 0. 目录 Spring命名空间 Bean作用域 1. Spring命名空间 在前 ...
- Spring入门(3-1)Spring的标签命名空间
1.标签命名空间声明: 2.标签命名空间使用 标签默认的命名空间是 security:,可以不用带 security:,直接写标签,如: <http <authentication-ma ...
- Spring:源码解读Spring IOC原理
Spring IOC设计原理解析:本文乃学习整理参考而来 一. 什么是Ioc/DI? 二. Spring IOC体系结构 (1) BeanFactory (2) BeanDefinition 三. I ...
- Spring学习笔记—最小化Spring XML配置
自动装配(autowiring)有助于减少甚至消除配置<property>元素和<constructor-arg>元素,让Spring自动识别如何装配Bean的依赖关系. 自动 ...
- Spring学习(三)——Spring中的依赖注入的方式
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...
- Spring Boot (五)Spring Data JPA 操作 MySQL 8
一.Spring Data JPA 介绍 JPA(Java Persistence API)Java持久化API,是 Java 持久化的标准规范,Hibernate是持久化规范的技术实现,而Sprin ...
- Spring(八):Spring配置Bean(一)BeanFactory&ApplicationContext概述、依赖注入的方式、注入属性值细节
在Spring的IOC容器里配置Bean 配置Bean形式:基于xml文件方式.基于注解的方式 在xml文件中通过bean节点配置bean: <?xml version="1.0&qu ...
- Spring学习(五)——Spring注解(一)
---恢复内容开始--- 概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射 ...
随机推荐
- VS调试Ajax
VS调试Ajax: 1.ashx在后台处理程序中设定断点 2.触发AJAX 3.F12打开浏览器调试,搜索找到ajax调用的JS,设置断点,在浏览器中单步调试,会自动进入后台处理程序,然后就可以调试后 ...
- bootstrap菜单完美解决---原创
由于bootstrap的各方优点,偶的“点金项目细化分包管理平台”决定采用它.但在使用中遇到了一些问题,比如菜单的问题,这个菜单是用的一个JQuery的一个效果,点击后,所点击的链接处的class要加 ...
- gen already exists but is not a source folder ZT
解决方法:1. 右键点击工程,选择 "Properties"2. 选择左边的 "Java Build Path" 3. 打开 "Source" ...
- K Best(最大化平均数)_二分搜索
Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...
- 用SSH指令批量修改文件夹 文件权限和拥有者
在linux系统下或登录ssh可以批量修改文件权限 wwwroot目录下的所有目录的权限递归设置为755 cd wwwrootfind -type d -exec chmod 755 {} \;或者是 ...
- 第二个Sprint冲刺第一天
讨论地点:宿舍 讨论成员:邵家文.李新.朱浩龙.陈俊金 任务:第二次sprint前期把选择题的计时功能完成. 燃尽图: 开发感悟: 大三第一学期就快结束了,大家都为未来找工作而烦恼.班里的高手都去学校 ...
- markdown简明语法
# markdown简明语法 标签(空格分隔): markdown 本语法只涵盖了常用的内容 [toc] 标题 标题 标题 语法为: 根据需求 可以指定 不同大小的标题 # 顶级 ## 次级 ### ...
- 用rem来做响应式开发
强烈推荐这篇文章:<web app 变革之rem> px转rem工具:<px转rem工具> 由于最近在做公司移动项目的重构,因为要实现响应式的开发,所以大量使用到了rem的单位 ...
- angularjs 不同的controller之间值的传递
Sharing data between controllers in AngularJS I wrote this article to show how it can possible to pa ...
- What are the 10 algorithms one must know in order to solve most algorithm challenges/puzzles?
QUESTION : What are the 10 algorithms one must know in order to solve most algorithm challenges/puzz ...