欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring事务完成之后异步执行其他操作

程序员文章站 2022-04-30 21:21:55
...

1.service接口

package com.rfg.springbootmybaits.service;

import com.rfg.springbootmybaits.dto.PersonAddParam;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: RuanFuGui
 * @Date: Created in 2020/5/17 20:28
 * @Description: TODO
 * @Version: 1.0
 */
public interface PersonService {

    void insertPerson(PersonAddParam param);

}

2.service实现类

package com.rfg.springbootmybaits.service.impl;

import cn.hutool.core.thread.ThreadUtil;
import com.rfg.springbootmybaits.dto.PersonAddParam;
import com.rfg.springbootmybaits.service.PersonService;
import com.rfg.springbootmybaits.dao.PersonDAO;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import javax.annotation.PostConstruct;

/**
 * @Author: RuanFuGui
 * @Date: Created in 2020/5/17 20:28
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonDAO personDAO;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertPerson(PersonAddParam param) {
        personDAO.insertPerson(param);
        deleteAfterCommit(param.getCode());
    }

    private void deleteAfterCommit(String code) {
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
                @Override
                public void afterCommit() {
                    ThreadUtil.execute(() -> {
                        personDAO.deletePerson(code);
                    });
                }
            });

    }
}

3.启动类

package com.rfg.springbootmybaits;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages={"com.rfg.springbootmybaits"})
@MapperScan("com.rfg.springbootmybaits.dao")
public class SpringBootMybaitsApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootMybaitsApplication.class, args);
    }

}

4.测试类

package com.rfg.springbootmybaits;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootMybaitsApplication.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class SpringBootMybaitsApplicationTests {

    @Test
    void contextLoads() {
    }

}

package com.rfg.springbootmybaits;

import com.rfg.springbootmybaits.dto.PersonAddParam;
import com.rfg.springbootmybaits.service.PersonService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * @Author: RuanFuGui
 * @Date: Created in 2020/5/17 21:21
 * @Description: TODO
 * @Version: 1.0
 */
public class TestMain extends SpringBootMybaitsApplicationTests{

    @Autowired
    private PersonService personService;

     @Test
     public void test(){
        PersonAddParam param = new PersonAddParam();
        param.setCode("rfg1");
        param.setName("rfg");
         param.setGender(1);
         param.setAge(25);
        personService.insertPerson(param);
     }

}

相关标签: springboot集成