| 关键词: nbsp 空间 Schema springframework Spring 标签 bean 元素 context 一个 |
一、Spring头信息Spring配置文件的头部信息一般是固定不变的,但每一个标签都有自己的含义,xml命名空间格式如下:<?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:context="http://www.springframework.org/schema/context" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p" 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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd"> <!— xml配置内容 --></beans> 1、XML Schema命名空间作用:1)、避免命名冲突,像Java中的package一样2)、将不同作用的标签分门别类(像context命名空间针对组件的标签)2、代码解释:1)、xmlns="http://www.springframework.org/schema/beans"声明xml文件默认的命名空间,表示未使用其他命名空间的所有标签的默认命名空间。2)、xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"声明XML Schema 实例,声明后就可以使用 schemaLocation 属性了3)、xmlns:context="http://www.springframework.org/schema/context" 4)、xmlns:cache="http://www.springframework.org/schema/cache"5)、xmlns:p="http://www.springframework.org/schema/p" 6)、 xmlns:task="http://www.springframework.org/schema/task"7)、xmlns:aop="http://www.springframework.org/schema/mvc"声明前缀为mvc的命名空间,后面的URL用于标示命名空间的地址不会被解析器用于查找信息。其惟一的作用是赋予命名空间一个惟一的名称。当命名空间被定义在元素的开始标签中时,所有带有相同前缀的子元素都会与同一个命名空间相关联。8)、xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd这个从命名可以看出个大概,指定Schema的位置这个属性必须结合命名空间使用。这个属性有两个值,第一个值表示需要使用的命名空间。第二个值表示供命名空间使用的 XML schema 的位置 所以我们需要什么样的标签的时候,就引入什么样的命名空间和Schema 定义就可以了。 二、Spring配置文件结构beans标签中可以包含4个标签: <alias>为一个定义过的bean起一个别名 <bean>向Spring容器中定义bean元素 <description>用来描述Spring context或每个bean元素,虽然他会被Spring容器所忽略,但<description>元素可以通过工具生成属于你的Spring context文档 <import>导入其他Spring context的定义 下面具体介绍bean标签:1、标签bean中可以包含如下元素: <constructor-arg>向bean的构造函数注入值或引用,即构造函数注入 <description>同beans中作用相同,用来描述Spring context或每个bean元素,虽然他会被Spring容器所忽略,但<description>元素可以通过工具生成属于你的Spring context文档 <lookup-method>使用方法来代替getter注入,指定一个方法,他会在运行被复写从而返回一个指定的bean,即getter注入 <meta>允许为你的bean进行meta配置,仅在一些特殊场合下有用 <property>为bean的特定属性注入一个值或者引用,这就是我们常说的setter注入 <replaced-method>用一个新的实现来代替bean的某个方法2、标签bean中可以包含如下属性: |
|
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系
[邮箱地址] 删除
|