1. 程式人生 > >xml方式實現spring的宣告式事務管理及對jdbc操作的支援

xml方式實現spring的宣告式事務管理及對jdbc操作的支援

<?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"
    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">
        <!-- c3p0連線池的一些配置-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="user" value="root"></property>
            <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/day16"></property>
            <property name="password" value="root"></property>
            <property name="maxPoolSize" value="10"></property>
            <property name="maxStatements" value="20"></property>
        </bean>
        
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <bean id="deptDao" class="com.spring.tranzcation.DeptDao">
            <property name="jdbcTemplate" ref="jdbcTemplate"></property>
        </bean>
        <bean id="deptService" class="com.spring.tranzcation.DeptService">
            <property name="deptDao" ref="deptDao"></property>
        </bean>
        <!-- 配置事物管理 -->
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>        
        <!-- 配置事物增強 -->
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="get*" read-only="true"/>
                <tx:method name="*" read-only="false"/>
            </tx:attributes>
        </tx:advice>
        <!-- AOP配置攔截那些方法,切入點表示式和事物配置增強相配合 -->
        <aop:config>
            <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.spring.tranzcation.DeptService.*(..))"/>
        </aop:config>    
</beans>