在application-context.xml文件中配置分为 2.5 和3.0的
xmlns:tx="http://www.springframework.org/schema/tx" 其中:tx中tx是"http://www.springframework.org/schema/tx"这个命名空间的简称,"http://www.springframework.org/schema/tx"是命名空间的全称必须在xsi命名空间为基本指定对应的schema文件
<?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文件-->xmlns:p="http://www.springframework.org/schema/p"<!-- -->
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
</beans>
在为每个命名空间指定了对应的Schema文档的时候,定义的语法为
xsi:schemaLocation="全称命名空间1 空格 全称命名空间1对应的Schema文件空格 全称命名空间2 空格 全称命名空间2对应的Schema文件 空格 全称命名空间3 空格 全称命名空间3对应的Schema文件 。。。。。。。"
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
</beans>
若配置文件中引用http://www.springframework.org/schema/p命令空间,则将会使用SimplePropertyNamespaceHandle来处理这个Bean的定义,可以在Spring2.0中的Bean中以更简单的方式配置设值方法注入,如下所示:
< ?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
< bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="org.gjt.mm.mysql.Driver" p:url="jdbc:mysql://127.0.0.1:3306/easyjf-bbs" p:username="root" p:password="mysql" />
< /beans>
在上面的配置中,使用p:url则可以直接注入BasicDataSource的url属性值,可以使用p:url-ref属性来引用另外一个Bean。
如,Spring2.0以前的一个DAO配置:
< bean id="userDao" class="com.easyjf.bbs.dbo.springjdbc.UserDaoSpringJdbc">
< property name="dataSource">< ref bean="dataSource"/>< /property>
< /bean>
使用简短属性方式,则改成如下:
< bean id="userDao" class="com.easyjf.bbs.dbo.springjdbc.UserDaoSpringJdbc" p:dataSource-ref="dataSource" />