自定义事务注解

自定义事务注解


大纲:

1.如何自定义注解
2.如何封装一个事务注解
步骤:1.spring.xml 开启注解
	2.定义注解接口
	3.写事务类
	4.写切面类
4.写切面类的步骤
1.先通过反射拿到代理对象的方法代理对象的信息
2.通过方法获取到这个方法是否加入了注解
    (JpcTc jpcTc = objMethod.getDeclaredAnnotation(JpcTc.class);)
3.如果有则开启事务
4.如果没有则不执行事务

1.写spring.xml

<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">
       <context:component-scan base-package="com.cn"></context:component-scan>
       <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- 开启事物注解 -->
       <!-- 1. 数据源对象: C3P0连接池 -->
       <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
              <property name="driverClass"  value="com.mysql.jdbc.Driver"></property>
              <property name="jdbcUrl"  value="jdbc:mysql://127.0.0.1:3306/test"></property>
              <property name="user" value="root"></property>
              <property name="password" value="root"></property>
       </bean>
       <!-- 2. JdbcTemplate工具类实例 -->
       <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
              <property name="dataSource" ref="dataSource"></property>
       </bean>
       <!-- 3.配置事务 -->
       <bean id="dataSourceTransactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
              <property name="dataSource" ref="dataSource"></property>
       </bean>
       <tx:annotation-driven transaction-manager="dataSourceTransactionManager" />
</beans>

2.事务工具类

package com.cn.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
importorg.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
@Component
@Scope("prototype") // 每个事务都是新的实例 目的解决线程安全问题 多例子  这个注解很重要!!!!  有点像给他分配 一个 threadLoad 防止多线程并发
public class Transaction {
       private TransactionStatus transactionStatus;
       @Autowired
       private DataSourceTransactionManager dataSourceTransactionManager;
       
       public TransactionStatus  begin(){
              TransactionStatus status =  dataSourceTransactionManager.getTransaction(new DefaultTransactionAttribute());
              return status;
       }
       
       //提交事务
       public void commit(TransactionStatus status){
              dataSourceTransactionManager.commit(status);
       }
       //回滚事务
       public void rollback(){
              dataSourceTransactionManager.rollback(transactionStatus);
       }
       
}

User类

package com.cn.service;
//user 服务层
public interface UserService {
       public void add();
       public void del();
}

User实现类

package com.cn.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.cn.dao.UserDao;
import com.cn.inteface.JpcTc;
import com.cn.service.UserService;
//user 服务层
@Service
public class UserServiceImpl implements UserService {
       @Autowired
       private UserDao userDao;
       
       
       @JpcTc
       public void add() {
              // 调用接口的时候 接口失败 需要回滚,但是日志记录不需要回滚。
              userDao.add("test001", 20);
               int i = 1 / 0;
              System.out.println("################");
              userDao.add("test002", 21);
       }
       // 方法执行完毕之后,才会提交事务
       public void del() {
              System.out.println("del");
       }
}

注解类

package com.cn.inteface;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value = ElementType.METHOD) // 这个就是注意 他只能在方法上
@Retention(RetentionPolicy.RUNTIME)    // 必须要加这个注解 这个注解才能反射
public @interface JpcTc {
//这就定义了一个注解
}

dao层

package com.cn.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void add(String name, Integer age) {
        String sql = "INSERT INTO t_users(NAME, age) VALUES(?,?);";
        int updateResult = jdbcTemplate.update(sql, name, age);
        System.out.println("updateResult:" + updateResult);
    }

}

aop切面

package com.cn.aop;

import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;

import com.cn.inteface.JpcTc;
import com.cn.util.Transaction;

@Aspect
@Component
public class AopConfig {

    @Autowired
    private Transaction transaction ;
    
    @Pointcut("execution(* com.cn.service.*.*.*(..))")
    public void print(){}
    
    @AfterThrowing("execution(* com.cn.service.*.*.*(..))")
    public void afterThrowing() {
        // 获取当前事务进行回滚
        // TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        System.out.println("抛异常,事务要回滚了");
        transaction.rollback();
    }

    
    @Around("print()")
    public void auout(ProceedingJoinPoint pjp) throws Throwable{
        /*
         * 1.先通过反射 获取到 方法  方法上是否存在注解
         * 2.如果有的话则开启事务
         * 3.执行代码
         * 4.判断是否有开启 事务, 如果有 就结束事务
         */
        String methodName = pjp.getSignature().getName();
        // 获取目标对象
        Class<?> classTarget = pjp.getTarget().getClass();
        // 获取目标对象类型
        Class<?>[] par = ((MethodSignature) pjp.getSignature()).getParameterTypes();
        // 获取目标对象方法
        Method objMethod = classTarget.getMethod(methodName, par);
        //这个方法就能 获取到  是否存在这个注解
        JpcTc jpcTc = objMethod.getDeclaredAnnotation(JpcTc.class);
        
        
        //###2.如果存在 则说明存在注解
        TransactionStatus status  = null;
        if(jpcTc != null){
            System.out.println("###########开启事务了!!!!!");
            status = transaction.begin();
        }
        //###3.执行被代理对象
        pjp.proceed();
        //###4.判断
        if(status != null){
            System.out.println("##########提交事务了!!!!!");
transaction.commit(status);
        }
        
        
        
        
        
        
        
        
        
    }
}

main方法测试

package com.cn;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cn.service.UserService;
public class Test001 {
       public static void main(String[] args) {
              ClassPathXmlApplicationContext applicationContext = new  ClassPathXmlApplicationContext("spring.xml");
              UserService userService = (UserService)  applicationContext.getBean("userServiceImpl");
              userService.add();
       }
}

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值