@Transactional proxyTargetClass 和@Inherited

@Transactional proxyTargetClass and @Inherited

关于@Transactional,我有 2 个问题无法找到答案:

1-接口上的@Transnational
根据答案:

If you have your app set to proxy-target-class='true' (in your application context, then your @Transactional information wont be picked up if you annotate the Interface.

我将 application.properties 配置为:

spring.aop.proxy-target-class=true

并且界面上的注释仍然有效,我也尝试了以下方法(proxyTargetClass = true on EnableTransacationManagement):

@SpringBootApplication
@EnableAsync
@EnableTransactionManagement(proxyTargetClass = true)
public class Application extends SpringBootServletInitializer {
}

@Transnational 仍然在我只在接口上配置的地方工作,所以我在这里缺少什么?

2-@Transnational 有@Inherited,@inherited 根据 java 文档:

Note that this meta-annotation type has no effect if the annotated type is used to annotate anything other than a class. Note also that this meta-annotation only causes annotations to be inherited from superclasses; annotations on implemented interfaces have no effect.

我有以下用@Transnational 注释的接口,它仍在处理扩展它的接口:

@Repository
@Transactional
public interface UserRepository extends JpaRepository<User, Long> {


}

following/below 接口有修改查询,如果没有 @Transactional 就无法工作,我已经用 @Transacational 注释了上面的接口并且它正在被拾取(如果我在父方法上配置它甚至可以工作的 class ) :

public interface TestRepository extends UserRepository {

@Query("UPDATE User U set U.name='hello' where U.id=1")
@Modifying
public void test();

}

如果 @Inherited 对接口没有影响,那么事务如何工作?


更新: 问题 1

我什至在接口方法上尝试了 @Async 并且它在没有注释实现的情况下工作 class ,AOP 正在使用 CGLIB 代理(仅在注释接口方法时)这与大多数答案矛盾,不确定是否有什么不对的。

您 link 的答案是 Spring 3.0,但目前我们的版本是 Spring 5.1。这大约提前了 7 个版本,参考指南的那部分不再存在。

无论您使用哪种代理模式都没有关系 Spring 当在实现上找不到时,总是会尝试在实现接口上找到 @Transactional 注释。这是 Spring 行为并在 Spring 中专门实现,这偏离了默认的注释继承规则。 (通常在执行 getAnnotation 时这是行不通的,所以 Spring 实现了这个)。另请参阅 Spring 参考指南的 this section

除此之外,您还使用了 Spring Data JPA,它还专门为其创建的动态存储库添加了元数据和注释处理。