Using Custom AnnotationTransactionAttributeSource with tx:annotation-driven

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.

Answers


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

Need Your Help

Cannot select text of TextBox control with mouse

c# .net winforms .net-2.0

I want to embed a Form within another Form. Here is what I have tried:

Recurring tasks in a Ruby On Rails application: Cron or other?

ruby-on-rails ruby passenger task

I am currently writing an application that pulls new information from RSS sources and has to update those RSS sources in a certain frequency. Currently I am pulling only when the user requests a fe...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.