Spring-AOP切面编程和事务回滚

Spring-AOP切面编程和事务回滚,第1张

Spring-AOP切面编程和事务回滚

Spring-AOP切面编程和事务回滚
  • 项目Github下载地址
  • 一、AOP的重要配置
    • 定义切点
    • 测试代码
    • AOP配置文件
  • 二、JDBC事务回滚
    • 事务管理类
    • dao层 *** 作数据库
    • Spring事务配置
    • 业务代码及测试方法


项目Github下载地址

点击跳转–>Github下载地址

一、AOP的重要配置



    
    
    

    
    
        
            
            
            
            

        
    



定义切点
package com.mabo.aop;



public class DiyPointcut {

    public void before(){
        System.out.println("---------方法执行前---------");
    }

    public void after(){
        System.out.println("---------方法执行后---------");
    }

}

测试代码
package com.mabo.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserService {


    public void add() {
        System.out.println("增加用户");
    }

    public void delete() {
        System.out.println("删除用户");
    }

    public void update() {
        System.out.println("更新用户");
    }

    public void search() {
        System.out.println("查询用户");
    }

    public static void main(String[] args) {
        ApplicationContext context =new ClassPathXmlApplicationContext("spring-aop.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.search();
    }
}
AOP配置文件



    
    
    

    
    
        
            
            
            
            

        
    



二、JDBC事务回滚 事务管理类
package com.mabo.transaction;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;



public class TransactionManager {


    private DataSourceTransactionManager manager;

    public void setManager(DataSourceTransactionManager manager) {
        this.manager = manager;
    }
    
    public TransactionStatus begin(){
        //获取事务状态
        TransactionStatus transaction = manager.getTransaction(new DefaultTransactionAttribute());
        return transaction;
    }

    
    public void commit(TransactionStatus transaction){
        //提交到数据库
        manager.commit(transaction);
    }

    
    public void rollback(TransactionStatus transaction){
        manager.rollback(transaction);
    }
}

dao层 *** 作数据库
package com.mabo.transaction;

import org.springframework.jdbc.core.JdbcTemplate;

public class UsersDao {

    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void addUser(String name, Integer age) {
        String sql="INSERT INTO users(name, age) VALUES(?, ?);";
        jdbcTemplate.update(sql, name,age);
        System.out.println("添加数据"+name+"成功!.......");
    }
}

Spring事务配置




    
        
        
        
        
    
    
    
        
    
    
    
        
    
    



    
        
    


    
        
        
    


    
        
    

    
    
        
    

    
    
        
            
        
    

    
    
        
    


业务代码及测试方法
package com.mabo.transaction;


import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.TransactionStatus;

public class UsersService {

    private UsersDao usersDao;
    private TransactionManager manager;

    public void setManager(TransactionManager manager) {
        this.manager = manager;
    }

    public void setUsersDao(UsersDao usersDao) {
        this.usersDao = usersDao;
    }

    public void add(){
        TransactionStatus begin=null;
        try {
            //开启事务
            begin = manager.begin();
            usersDao.addUser("吴大宝01",18);
            int i=1/0;//故意制造异常
            usersDao.addUser("吴大宝02",20);
            System.out.println("添加用户成功!");
            //提交事务
            manager.commit(begin);
        } catch (Exception e) {
            //发生异常回滚事务
            manager.rollback(begin);
            System.out.println("发生异常回滚事务!");
        } finally {

        }
    }


    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-transation.xml");
        UsersService usersService = (UsersService) applicationContext.getBean("usersService");
        usersService.add();
    }

}

# 项目Maven依赖

```xml


    4.0.0

    org.example
    AOP
    1.0-SNAPSHOT

    
        8
        8
    
    
        
            mysql
            mysql-connector-java
            5.1.49
        
        
            org.springframework
            spring-core
            5.3.7
        
        
            org.apache.poi
            poi
            3.17
        
        
            org.apache.poi
            poi-ooxml
            3.17
        
        
            org.apache.poi
            poi
            4.0.1
        
        
            org.apache.httpcomponents
            httpcore
            4.4.13
        
        
            org.apache.httpcomponents
            httpclient
            4.5.13
        

        
            com.aliyun
            dysmsapi20170525
            2.0.5
        
        
            org.springframework
            spring-aop
            5.3.7
        
        
            org.springframework
            spring-context
            5.3.7
        
        
            org.aspectj
            aspectjweaver
            1.9.5
        
        
            org.springframework
            spring-jdbc
            5.3.4
        
        
            com.mchange
            c3p0
            0.9.5.2
        
    




欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4659884.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-07
下一篇 2022-11-06

发表评论

登录后才能评论

评论列表(0条)

保存