首页 > 代码库 > spring---------配置文件的命名空间
spring---------配置文件的命名空间
两种格式的配置文件:
DTD和Schema区别:http://www.cnblogs.com/zhaozhan/archive/2010/01/04/1639194.html
1 <?xml version="1.0" encoding="UTF-8"?>2 <beans xmlns="http://www.springframework.org/schema/beans" <!--默认命名空间:表示未使用其他命名空间的所有标签的默认命名空间-->3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <!--xsi标准命名空间,用于指定义自定义命名空间的schema文件,声明后就可以使用 schemaLocation 属性了-->4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--此处可以不配置,可以用来引用schema在本地的副本-->5 6 7 8 </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="http://www.mamicode.com/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.xsdhttp://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
完整的一个配置文件头如下
<?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:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.1.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"></beans>
spring---------配置文件的命名空间