将自定义 AnnotationTransactionAttributeSource 与 tx:annotation-driven 结合使用

发布于 2024-12-18 18:35:01 字数 1104 浏览 6 评论 0原文

我需要使用自定义 AnnotationTransactionAttributeSource 来拦截事务属性。现在,我使用 TransactionInterceptor 执行此操作,并将其注入 TransactionAttributeSourceAdvisor 中。代理是使用 DefaultAdvisorAutoProxyCreator 创建的,如下所示。

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>

<bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
    <property name="transactionInterceptor" ref="txInterceptor"/>
</bean>

<bean id="txInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager" ref="txManager"/>
    <property name="transactionAttributeSource"> 
       <bean class="org.myProject.transaction.CustomAnnotationTransactionAttributeSource"/>
    </property>
</bean>

这里,CustomAnnotationTransactionAttributeSource 扩展了 AnnotationTransactionAttributeSource。有什么方法可以强制 Tx:annotation-driven 使用我的 CustomAnnotationTransactionAttributeSource 以便我可以避免所有这些配置? 。我在一篇文章中读到,这可以通过使用 BeanPostProcessors 来完成,但不确定如何在这种情况下使用它。

I need to use a Custom AnnotationTransactionAttributeSource in order to intercept transaction attributes. Right now, I do this using the TransactionInterceptor and injecting this in TransactionAttributeSourceAdvisor .The proxies are created using DefaultAdvisorAutoProxyCreator as given below.

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>

<bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
    <property name="transactionInterceptor" ref="txInterceptor"/>
</bean>

<bean id="txInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager" ref="txManager"/>
    <property name="transactionAttributeSource"> 
       <bean class="org.myProject.transaction.CustomAnnotationTransactionAttributeSource"/>
    </property>
</bean>

Here, CustomAnnotationTransactionAttributeSource extends AnnotationTransactionAttributeSource. Is there any way I can force Tx:annotation-driven to use my CustomAnnotationTransactionAttributeSource so that I could avoid all these configurations? . I read in one of the posts that this could be done by using BeanPostProcessors but not sure how to use it for this case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇纵 2024-12-25 18:35:01

没有做任何魔法,它只是注册与手动操作几乎相同的 bean 定义(请参阅 AnnotationDrivenBeanDefinitionParser)。

因此,您可以替换其他 bean 中对 AnnotationTransactionAttributeSource 的引用,或者替换其定义中的类名属性。后者看起来更简单(尽管相对于 Spring 代码的更改而言更脆弱),并且可以通过以下 BeanFactoryPostProcessor 来完成:

public class AnnotationTransactionAttributeSourceReplacer implements BeanFactoryPostProcessor {
    public void postProcessBeanFactory(ConfigurableListableBeanFactory factory)
            throws BeansException {

        String[] names = factory.getBeanNamesForType(AnnotationTransactionAttributeSource.class);

        for (String name: names) {
            BeanDefinition bd = factory.getBeanDefinition(name);
            bd.setBeanClassName("org.myProject.transaction.CustomAnnotationTransactionAttributeSource");
        }            
    }       
}

<tx:annotation-driven> doesn't do anything magic, it just registers almost the same bean definitions as you do manually (see AnnotationDrivenBeanDefinitionParser).

So, you can either replace references to AnnotationTransactionAttributeSource from other beans, or replace class name property in its definition. The latter looks simplier (though more fragile with respect to changes in Spring code) and can be done by the following BeanFactoryPostProcessor:

public class AnnotationTransactionAttributeSourceReplacer implements BeanFactoryPostProcessor {
    public void postProcessBeanFactory(ConfigurableListableBeanFactory factory)
            throws BeansException {

        String[] names = factory.getBeanNamesForType(AnnotationTransactionAttributeSource.class);

        for (String name: names) {
            BeanDefinition bd = factory.getBeanDefinition(name);
            bd.setBeanClassName("org.myProject.transaction.CustomAnnotationTransactionAttributeSource");
        }            
    }       
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文