类的内部调用和多线程调用解决事务、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; }); } } }
原文地址:https://www.cnblogs.com/huqi96/p/15410180.html