最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Replacing Spring Boot TransactionInterceptor - Stack Overflow

programmeradmin2浏览0评论

I am trying to upgrade a legacy spring-hibernate (no jpa) project to spring-jpa.

The project has some monkeying around datasource, transactionManager generation etc.., basically we are generating all of them (multiple datasources, multiple transactionManagers) programmatically via parsing the properties on our own.

Generating all these and having a map of different datasources and transactionManagers are ok. But I am unable to replace the TransactionInterceptor bean in context, so that I can utilize my pre-generated map to use different managers. When I try to generate a bean with name TransactionInterceptor, spring does not like it and context failed to come up.

So my question is if it's possible to replace the TransactionInterceptor or something else (like advisors etc..) to intercept and use proper tm.

spring boot : 3.3.3 spring-tx : 6.1.12

I am trying to upgrade a legacy spring-hibernate (no jpa) project to spring-jpa.

The project has some monkeying around datasource, transactionManager generation etc.., basically we are generating all of them (multiple datasources, multiple transactionManagers) programmatically via parsing the properties on our own.

Generating all these and having a map of different datasources and transactionManagers are ok. But I am unable to replace the TransactionInterceptor bean in context, so that I can utilize my pre-generated map to use different managers. When I try to generate a bean with name TransactionInterceptor, spring does not like it and context failed to come up.

So my question is if it's possible to replace the TransactionInterceptor or something else (like advisors etc..) to intercept and use proper tm.

spring boot : 3.3.3 spring-tx : 6.1.12

Share Improve this question asked yesterday Olgun KayaOlgun Kaya 2,5794 gold badges33 silver badges53 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Please see Programmatic Transaction Management.

I’m not sure if this is the best approach for your case, but you could replace the TransactionInterceptor with a custom Advisor bean.

Tested on my computer.

import org.springframework.aop.Advisor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionInterceptor;

@Configuration
public class TransactionInterceptorConfig {

    @Bean
    public Advisor customTransactionAdvisor(TransactionManager transactionManager) {
        var transactionInterceptor = new TransactionInterceptor(); // replace with your interceptor
        transactionInterceptor.setTransactionManager(transactionManager);

        // pointcut for classes/methods annotated with @Transactional
        var annotationMatchingPointcut =
                new AnnotationMatchingPointcut(Transactional.class, Transactional.class);

        return new DefaultPointcutAdvisor(annotationMatchingPointcut, transactionInterceptor);
    }
}
发布评论

评论列表(0)

  1. 暂无评论