spring-事务的添加(企业标准配置)

<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager"></tx:advice>
这两个transactionManager互相引用

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    <!--1.配置包扫描 -->
    <context:component-scan base-package="com.jt"></context:component-scan>
    <!--2.配置数据源,它将被mybatis引用 -->
    <!--2.1导入pro配置文件 -->
    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <!-- private Resource[] locations; -->
            <list>
                <value>classpath:/property/jdbc.properties</value>
            </list>
        </property>
    </bean>
    <!--2.2配置数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    <!--3.配置事务控制 -->
    <tx:annotation-driven />
    <!--3.1定义事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--3.2定义事务策略
    propagation="REQUIRED" 执行该操作必须添加事务
    propagation="SUPPORTS" 事务支持的,原来的操作有事务,则添加事务
    原有的操作没事务,不添加事务
    propagation="NEVER"从不添加事务
    propagation="REQUIRES_NEW" 不管之前有没有事务,都会创建一个新的事务
    read-only="true" 该操作只读
    *除此之外的方法只读
    -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!--3.3定义事务切面  
    Content Model : (pointcut*, advisor*, aspect*)
                        连接点,          通知,        自定义切面
        expression 切入点表达式
        within(包名.类名) 按类匹配-控制粒度-粗粒度
        execution(返回值类型 包名.类名.方法名(参数列表))
        execution(返回值类型 包名.类名.方法名(int))
        execution(返回值类型 包名.类名.方法名(String))
        execution(返回值类型 包名.类名.方法名(int,String))
    -->
                        
    <aop:config>
        <aop:pointcut expression="execution(* com.example.manage.service..*.*(..))" id="pc"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
    </aop:config>
</beans>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容