项目设置:Spring 3.0.5 / JPA 2 / Hibernate / @Transactional
我们使用多个不同的数据源(因此也使用不同的事务管理器),但具有通用的 Service 基类,因为许多功能被重用。
所以我们认为我们可以通过使用 自定义注释。我们在抽象基类中定义所有方法,并为每个事务管理器创建一个空的实现类。
现在的问题是:
在 AbstractFallbackTransactionAttributeSource
,这是正在完成的查找:
TransactionAttribute txAtt = findTransactionAttribute(specificMethod);
// ...
// Second try is the transaction attribute on the target class.
txAtt = findTransactionAttribute(specificMethod.getDeclaringClass());
首先检查方法,然后检查声明该方法的类(及其祖先)。但在我们的上下文中,自定义注释驻留在子类中,无法通过向上查找来找到该子类。
那么我们该如何解决这个问题呢?
[我还在 Spring 社区论坛中提出了这个问题]
Project setup: Spring 3.0.5 / JPA 2 / Hibernate / @Transactional
We work with several different Data Sources (and hence different transaction managers), but have common Service base classes, as a lot of the functionality is reused.
So we thought we'd solve this by using Custom Annotations. We define all methods in an abstract base class, and create one empty implementation class per Transaction Manager.
Now the problem is:
In AbstractFallbackTransactionAttributeSource
, this is the lookup that is being done:
TransactionAttribute txAtt = findTransactionAttribute(specificMethod);
// ...
// Second try is the transaction attribute on the target class.
txAtt = findTransactionAttribute(specificMethod.getDeclaringClass());
First the method is checked, then the class that declares the method (and its ancestors). But in our context, the custom annotation resides in a subclass that can't be found by going upwards.
So how can we solve this problem?
[I also asked this question in the Spring Community Forum]
发布评论
评论(2)
很棒的收获。我不知道一个好的解决方案,但作为一种解决方法,您可以像这样重写实现中的相关方法:
这将使
@Transactional
可见,但它很难看:-/Great catch. I don't know a good solution but as a workaround, you can override the methods in question in the implementation like so:
that will make the
@Transactional
visible but it's ugly :-/目前,我们通过提供
AnnotationTransactionAttributeSource
对目标进行查找
首先类,然后委托给
超类
BeanFactoryPostProcessor
它将我们的实现替换为内部创建的原始AnnotationTransactionAttributeSource
bean 定义。
For the time being, we solved this issue by providing
AnnotationTransactionAttributeSource
which does a lookup on the target
class first and then delegates to
the super class
BeanFactoryPostProcessor
that substitues our implementation for the originalAnnotationTransactionAttributeSource
bean definition that is created internally by<tx:annotation-driven>
.