zoukankan      html  css  js  c++  java
  • 类的内部调用和多线程调用解决事务、aop失效的问题

    1.获取指定类的bean对象

    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.core.env.Environment;
    import org.springframework.stereotype.Component;
    
    /**
     * @author huqi 2021/10/15
     */
    @Component
    public class SpringUtil implements ApplicationContextAware {
    
        private static ApplicationContext applicationContext = null;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            SpringUtil.applicationContext = applicationContext;
        }
    
        public static <T> T getBean(Class<T> cla) {
            return applicationContext.getBean(cla);
        }
    
        public static <T> T getBean(String name, Class<T> cal) {
            return applicationContext.getBean(name, cal);
        }
    
        public static Object getBean(String name){
            return applicationContext.getBean(name);
        }
    
        public static String getProperty(String key) {
            return applicationContext.getBean(Environment.class).getProperty(key);
        }
    }

    2.通过指定类的bean对象调用内部方法

    import com.example.bean.Student;
    import com.example.bean.TaskDownload;
    import com.example.dao.StudentMapper;
    import com.example.dao.TaskDownloadMapper;
    import com.example.service.TestService;
    import com.example.util.SpringUtil;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    /**
     * @author huqi 2021/10/15
     */
    @SuppressWarnings("all")
    @Service
    public class TestServiceImpl implements TestService {
    
        @Autowired
        private StudentMapper studentMapper;
    
        @Autowired
        private TaskDownloadMapper taskDownloadMapper;
    
        public void test(){
    
            getService().addStudent();
        }
        @Transactional
        public void addStudent(){
            Student student = new Student();
            student.setName("test");
            student.setAge(12);
            studentMapper.insert(student);
            getService().addTask();
        }
        @Transactional
        public void addTask(){
            TaskDownload download = new TaskDownload();
            download.setFileName("test");
            download.setStatus(1);
            download.setTaskId("33");
            taskDownloadMapper.insert(download);
            int i = 1/0;
        }
    
        //解决事务失效
        private TestServiceImpl getService(){
            return SpringUtil.getBean(this.getClass());
        }
    }

     3.多线程事务失效解决

        
      // 入口
      public void test(){ new Thread(()->{ getService().test2(); }).start(); } @Transactional public void test2(){ addStudent(); addTask(); } public void addStudent(){ Student student = new Student(); student.setName("test"); student.setAge(12); studentMapper.insert(student); } public void addTask(){ for(int i = 0;i<10;i++){ TaskDownload download = new TaskDownload(); download.setFileName("test"); download.setStatus(1); download.setTaskId("33"); taskDownloadMapper.insert(download); if(i==3){ new Thread(()->{ int k=1/0; }); } } }
  • 相关阅读:
    es6 export 和export default的区别
    Vue 中 export及export default的区别
    深入学习jQuery选择器系列第一篇——基础选择器和层级选择器
    深入理解javascript选择器API系列第三篇——HTML5新增的3种selector方法
    深入理解javascript选择器API系列第二篇——getElementsByClassName
    深入理解javascript选择器API系列第一篇——4种元素选择器
    理解jQuery对象$.html
    深入理解DOM节点操作
    深入理解DOM节点关系
    深入理解DOM节点类型第一篇——12种DOM节点类型概述
  • 原文地址:https://www.cnblogs.com/huqi96/p/15410180.html
Copyright © 2011-2022 走看看