spring中事务的配置

来源:互联网 发布:快打软件 编辑:程序博客网 时间:2024/04/29 08:13

1.首先在头部(beans)加上头部信息

<beansxmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:context="http://www.springframework.org/schema/context"    xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="    http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-3.1.xsd    http://www.springframework.org/schema/tx     http://www.springframework.org/schema/tx/spring-tx-3.1.xsd    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"></beans>

2.配置事务管理器(transactionManager)

<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    <propertyname="dataSource"ref="dataSource"/></bean>

3.配置事务advice策略(tx:advice)

<tx:adviceid="txAdvice"transaction-manager="transactionManager">    <tx:attributes>       <tx:methodname="get*"read-only="true"/>       <tx:methodname="find*"read-only="true"/>       <tx:methodname="query*"read-only="true"/>       <tx:methodname="is*"read-only="true"/>       <tx:methodname="*"propagation="REQUIRED"/>(给所有的方法加上事务)    </tx:attributes></tx:advice>

4.配置切面管理(aop:config)

<aop:configproxy-target-class="true"><aop:advisorpointcut="execution(* com.kinglo.promotion.service..*Service.*(..))" advice-ref="txAdvice"/></aop:config>

5.Execution的理解

    a.步骤d中的execution意思是:

   匹配com.kinglo.promotion.service包及其子包中的所有接口名为*Service里所有0个或任意参数的方法

 

    b.第一个*号表示任何权限修饰符(public,private,protected,默认不写).

   eg:匹配所有共有方法:execution(public * com.kinglo.promotion.service..*Service.*(..)) 

    c.第二个*号表示匹配任何字符串

    d.第三个*号表示任何方法

    e.第四个(..)表示0个或任意个参数