将自定义 AnnotationTransactionAttributeSource 与 tx:annotation-driven 结合使用
我需要使用自定义 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有做任何魔法,它只是注册与手动操作几乎相同的 bean 定义(请参阅AnnotationDrivenBeanDefinitionParser
)。因此,您可以替换其他 bean 中对 AnnotationTransactionAttributeSource 的引用,或者替换其定义中的类名属性。后者看起来更简单(尽管相对于 Spring 代码的更改而言更脆弱),并且可以通过以下 BeanFactoryPostProcessor 来完成:
<tx:annotation-driven>
doesn't do anything magic, it just registers almost the same bean definitions as you do manually (seeAnnotationDrivenBeanDefinitionParser
).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 followingBeanFactoryPostProcessor
: