广告
返回顶部
首页 > 资讯 > 后端开发 > Python >AOP之事务管理<aop:advisor>的两种配置方式
  • 867
分享到

AOP之事务管理<aop:advisor>的两种配置方式

2024-04-02 19:04:59 867人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录aop事务管理<aop:advisor>两种配置方式方式一方式二hibernate事务配置Aop aop:advisor模式AOP事务管理<aop:adviso

AOP事务管理<aop:advisor>两种配置方式

方式一

@transactionManagerbean.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:component-scan base-package="com.wanho.java150"/>
     <context:property-placeholder location="classpath*:jdbc.properties"/>
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
         <property name="driverClassName" value="${jdbc.driverClassName}"/>
         <property name="url" value="${jdbc.url}"/>
         <property name="username" value="${jdbc.username}"/>
         <property name="passWord" value="${jdbc.password}"/>
    </bean>
     <!--spring 提供 jdbc 帮助类-->
     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
         <property name="dataSource" ref="dataSource"/>
     </bean>
    
     <!--基于@Transactional-->
     <!--事务管理器-->
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource" ref="dataSource"/>
     </bean>
     <tx:annotation-driven />    
</beans>

ServiceImpl

在service实现类的最外层或者具体方法上添加@Transactional注解


@Service
//@Transactional
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDAO customerDAO ;
    @Autowired
    private LoginLogDAO loginLogDAO ;
    @Transactional
    @Override
    public Customer login(String account, String pswd) {
        return null;
    }
}

方式二

bean.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:component-scan base-package="com.wanho.java150"/>
     <context:property-placeholder location="classpath*:jdbc.properties"/>
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
         <property name="driverClassName" value="${jdbc.driverClassName}"/>
         <property name="url" value="${jdbc.url}"/>
         <property name="username" value="${jdbc.username}"/>
         <property name="password" value="${jdbc.password}"/>
    </bean>
     <!--spring 提供 jdbc 帮助类-->
     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
         <property name="dataSource" ref="dataSource"/>
     </bean>
    
     <!--jdbc 事务管理配置基于xml-->
      <!--事务管理器-->
     <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource" ref="dataSource"/>
     </bean>
     <!--事务隔离级别-->
     <tx:advice id="txAdvice" transaction-manager="txManager">
         <tx:attributes>
             <!--还可以添加回滚、只读等标签配置-->
             <tx:method name="*" />
         </tx:attributes>
     </tx:advice>
     <!--业务层 使用 事务切面-->
     <aop:config>
         <aop:pointcut id="servicePointcut" expression="execution(* com.wanho.java150.service.impl.CustomerServiceImpl.*(..))"/>
         <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut"/>
     </aop:config>
</beans>

hibernate事务配置Aop aop:advisor模式


<!-- 使用HibernateTransactionManager管理hibernate事务 -->
    <bean id="txManager"
    class="org.springframework.ORM.hibernate3.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
        
    <!-- 创建事务规则 -->
    <!-- 表示我们要控制事务的地方,如果方法名开头是add、update和delete,那么使用REQUIRED事务传播方式。那么其他的方法使用REQUIRED事务传播方式,并且是只读 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
           <tx:method name="add*" propagation="REQUIRED"
          rollback-for="Exception" />
          <tx:method name="delete*" propagation="REQUIRED"
          rollback-for="Exception" />
          <tx:method name="update*" propagation="REQUIRED"
          rollback-for="Exception" />
          <tx:method name="*" propagation="REQUIRED" read-only="true" />
       </tx:attributes>
    </tx:advice>
    <!-- 告知事务的切入点 -->
    <aop:config>
    <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.tiema..service..*.*(..))" />
    </aop:config>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: AOP之事务管理<aop:advisor>的两种配置方式

本文链接: https://www.lsjlt.com/news/158184.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作