将自定义AnnotationTransactionAttributeSource与tx:annotation驱动一起使用 [英] Using Custom AnnotationTransactionAttributeSource with tx:annotation-driven

查看:206
本文介绍了将自定义AnnotationTransactionAttributeSource与tx:annotation驱动一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Custom AnnotationTransactionAttributeSource来拦截交易属性.现在,我使用TransactionInterceptor进行此操作,并将其注入TransactionAttributeSourceAdvisor中.代理使用DefaultAdvisorAutoProxyCreator创建,如下所示.

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>

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

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.

推荐答案

<tx:annotation-driven>并没有做任何魔术,它只是注册了几乎与您手动进行的相同的bean定义(请参见

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

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

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");
        }            
    }       
}

这篇关于将自定义AnnotationTransactionAttributeSource与tx:annotation驱动一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆