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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 引入外部配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" />

    <bean id="AccountDao" class="com.itcast.dao.impl.AccountDaoImpl">
        <property name="JdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

    <bean id="AccountService" class="com.itcast.service.impl.AccountServiceImpl">
        <property name="AccountDao" ref="AccountDao"></property>
    </bean>

    <!-- spring自带连接池 -->
    <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName" value="${jdbc.driverClass}"></property> 
        <property name="url" value="${jdbc.url}"></property> <property name="username" 
        value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> 
        </bean> -->

    <!-- DBCP连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost/test01"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

    <!-- c3p0连接池 -->
    <!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property 
        name="jdbcUrl" value="jdbc:mysql://localhost/test01"></property> <property 
        name="user" value="root"></property> <property name="password" value="root"></property> 
        </bean> -->

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
<!-- *****************************************以下是XML配置事务管理器********************************************* -->
    <!-- 配置事务管理器 -->
    <!-- <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean> -->

    <!-- 配置事务增强 -->
<!--     <tx:advice id="txAdvice" transaction-manager="transactionManager">-->        
<!-- 
        propagation="REQUIRED" 传播行为 
        isolation="DEFAULT"   隔离级别
        isolation="DEFAULT"    过期时间
        -->
        <!-- <tx:attributes> 
            <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" timeout="-1"/>
        </tx:attributes>
    </tx:advice> -->

    <!-- AOP配置 -->
    <!-- <aop:config>
        <aop:pointcut
            expression="execution( * com.itcast.service.impl.AccountServiceImpl.*(..))"
            id="pointcut1" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1" />
    </aop:config> -->
    
    <!-- *****************************************以上是XML配置事务管理器********************************************* -->
    
    
    
    <!-- *****************************************以下是注解配置事务管理器********************************************* -->
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 开启事务管理注解 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
        <!-- *****************************************以下是注解配置事务管理器********************************************* -->
    
    
</beans>

猜你喜欢

转载自blog.csdn.net/weixin_42210904/article/details/86684302
今日推荐