Spring.xml事物配置

来源:互联网 发布:广西广电网络宽带价钱 编辑:程序博客网 时间:2024/06/15 21:42
<?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: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.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">


    <!-- 定义dbcp数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <!-- 指定JDBC驱动类 -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver">
        </property>
        <!-- 提供连接数据库的URL地址 -->
        <property name="url"
            value="jdbc:mysql://localhost:3306/db?useUnicode=true&amp;characterEncoding=utf-8"></property>
        <!-- 提供连接数据库的用户名和密码 -->
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    <!--************************************************************************** -->
    <!-- 定义SessionFactory Bean -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <!-- 为LocalSessionFactoryBean注入定义好的数据源 -->
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <!-- 添加Hibernate配置参数 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
        <!-- 引入com/ssh/entity/包下所有实体类对应的映射XML -->
        <property name="mappingDirectoryLocations">
            <list>
                <value>classpath:util/</value>
            </list>
        </property>
    </bean>

    <!-- 切面配置 -->
    <aop:config>
        <!-- 定义切入点 -->
        <aop:pointcut id="serviceMethod" expression="execution(* dao.UserDoaImpl.*.*(..))" />
        <!-- 将事务通知与切入点组合 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
    </aop:config>

    <!-- 定义事务管理器 -->

    <bean id="txManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <!-- 配置声明式事务 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="select*" read-only="true" />
            <tx:method name="search*" read-only="true" />
            <tx:method name="query*" read-only="true" />
            <!--name="insert*" 方法名以insert开头 -->
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="do*" propagation="REQUIRED" />
            <tx:method name="*" propagation="REQUIRED" read-only="false" />
        </tx:attributes>
    </tx:advice>
</beans>