【spring】XML配置事务管理

<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        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">
   <!--包扫描-->
    <context:component-scan base-package="spring2"/>
    <!--引入数据源文件-->
    <context:property-placeholder location="classpath:config/jdbc.properties" />
    <!--配置c3p0-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
        <property name="driverClass" value="${jdbc.driverClass}" />
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}" />
        <property name="user" value="${jdbc.user}" />
        <property name="password" value="${jdbc.password}" />
        <property name="initialPoolSize" value="${jdbc.initPoolSize}" />
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
    </bean>
    <!--配置JDBC Template-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置Bean-->
    <bean id="bookShopDao" class="spring2.DAO.impl.BookShopDaoImpl" p:jdbcTemplate-ref="jdbcTemplate" />
    <bean id="bookShopService" class="spring2.service.impl.BookShopServiceImpl" p:bookShopDao-ref="bookShopDao" />
    <bean id="cashier" class="spring2.service.impl.CashierImpl" p:bookShopService-ref="bookShopService" />
    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref="dataSource"
    />
    <!--配置事务属性(传播行为、隔离级别、超时、只读,回滚异常)-->
    <!--
        name属性表示将属性标注在哪个方法上
        name属性值,可以使用通配符,如下表示一般get或find开头的方法都是为了查询数据
    -->
    <tx:advice id="txAdavice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="purchase" propagation="REQUIRES_NEW"/>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    <!--配置事务切入点,以及把事务切入点(需要关联的类的方法)和事务属性关联起来-->
    <aop:config>
        <aop:pointcut id="txPointCut" expression="execution(* spring2.service.*.*(..))"/>
        <aop:advisor advice-ref="txAdavice" pointcut-ref="txPointCut"/>
    </aop:config>
</beans>

posted @ 2020-03-23 11:26  叶荒  阅读(345)  评论(0)    收藏  举报