微笑精灵 2022-07-19 11:42 采纳率: 93.8%
浏览 1187
已结题

springboot的Transactional怎么关闭指定方法的事务?

原因:某个业务类上被挂了Transactional注解。
现在有个问题,我想让其中的某个方法关闭事务。
因为太影响性能了。
但是我又不好删除类上的注解(懂的都懂),所以有啥办法不?

  • 写回答

7条回答 默认 最新

  • kse_music 2022-07-20 15:58
    关注

    自定义一个排除注解就可以了

    
        @Target(ElementType.METHOD)
        @Retention(RetentionPolicy.RUNTIME)
        public @interface Exclude{
            
        }
        
        public class Bpf implements BeanDefinitionRegistryPostProcessor{
            @Override
            public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
                registry.removeBeanDefinition("transactionAttributeSource");
                AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(AnnotationTransactionAttributeSource.class).setRole(2).getBeanDefinition();
                AnnotationMethodMatcher methodMatcher = new AnnotationMethodMatcher(Exclude.class);
                beanDefinition.setInstanceSupplier(() -> new AnnotationTransactionAttributeSource(){
    
                    @Override
                    public TransactionAttribute getTransactionAttribute(Method method, Class<?> targetClass) {
                        if(methodMatcher.matches(method,targetClass==null?method.getDeclaringClass():targetClass)){
                            return null;
                        }
                        return super.getTransactionAttribute(method, targetClass);
                    }
    
                });
                registry.registerBeanDefinition("transactionAttributeSource", beanDefinition);
            }
    
            @Override
            public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
            }
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(6条)

报告相同问题?

问题事件

  • 系统已结题 7月28日
  • 已采纳回答 7月20日
  • 创建了问题 7月19日

悬赏问题

  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了