初学者在学习Spring的时候,往往会对Spring配置文件头部的命名空间感到“抓狂”,密密麻麻写了N行,却未必知道其使用原理,本文对Spring配置文件的命名空间进行讲解,希望对大家具有价值。
一、申明名称空间
名称空间声明的一般形式为:
第一部分是一个关键字xmlns:,
第二部分是名称空间的前缀,
第三部分是一个等号,
第四部分是双引号,
将第五部分的名称空间标识URI包括起来。
需要注意的是,名称空间的前缀不能为xml,因为在XML中这个字符串是保留作特殊用途的。例:
xmlns:tns="http://www.whtest.com/" //其中tns为前缀。
还可以隐式声明名称空间,即省略掉冒号和名称空间前缀。例:
xmlns="http://www.whtest.com/" //注意在一个文档中只能有一个隐式声明的命名空间
二、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" <!--xsi标准命名空间,用于指定义自定义命名空间的schema文件,声明后就可以使用 schemaLocation 属性了--> xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--此处可以不配置,可以用来引用schema在本地的副本--> </beans>
为每个命名空间指定了对应的Schema文档的时候,定义的语法为:
“全称命名空间1 空格 全称命名空间1对应的Schema文件空格”
其他命名空间与
同一级别下配置即可xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
util
util标签用来配置集合、常量等的
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation中需要定义
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
使用:分别使用<util:list>、<util:map>、<util:set>、<util:properties>等标签,用来
取代ListFactoryBean、MapFactoryBean、SetFactoryBean、PropertiesFactoryBean。
用途一:直接使用<util:properties id="appProps" location="classpath:META-INF/app.properties" />指定了配置文件
以前需要使用
<!-- creates a java.util.Properties instance with values loaded from the supplied location -->
<bean id="jdbcConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:com/foo/jdbc-production.properties"/>
</bean>
spring中id就是这个类的一个别名
现在只需使用
<!-- creates a java.util.Properties instance with values loaded from the supplied location -->
<util:properties id="jdbcConfiguration" location="classpath:com/foo/jdbc-production.properties"/>
jee
jee标签用来处理javaee标准相关的问题,例如查询一个jndi对象以及定义一个ejb的引用等
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation中需要定义
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
lang
lang用来将那些已经被定义在一些动态语言(例如Jruby和Groovy)中的对象作为beans中的对象存放到spring容器中
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation中需要定义
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
jms
xmlns:jms="http://www.springframework.org/schema/jms"
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd
tx (transaction)
使用时建议配合aop
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
aop
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
context
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
jdbc
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
cache
xmlns:jdbc="http://www.springframework.org/schema/cache"
http://www.springframework.org/schema/cache http://www.springframework.org/schema/jdbc/spring-cache.xsd