学spring之xmlns配置之命名空间

在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文件


 <!--2.5版本的配置-->
<?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文件   。。。。。。。"

------------------------------------3.0----------------------------
 
<?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" />  


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值