java-什么是基于接口的代理?

我正在阅读有关在何处放置Transactional(接口与实现)的信息:

The Spring team’s recommendation is that you only annotate concrete classes with the @Transactional annotation, as opposed to annotating interfaces. You certainly can place the @Transactional annotation on an interface (or an interface method), but this will only work as you would expect it to if you are using interface-based proxies. The fact that annotations are not inherited means that if you are using class-based proxies then the transaction settings will not be recognised by the class-based proxying infrastructure and the object will not be wrapped in a transactional proxy (which would be decidedly bad). So please do take the Spring team’s advice and only annotate concrete classes (and the methods of concrete classes) with the @Transactional annotation.

所以问题是,到底什么是基于接口的代理,如何查看它是否被使用?是一些配置还是实例化/使用实例的方式?

解决方法:

如果Spring无法创建JDK代理(或者如果您通过将proxyTargetClass设置为true来强制Spring创建CGLIB代理),则该代码将不会在事务性上下文中执行,因为(如Spring docs所述):

The fact that annotations are not inherited means that if you are using class-based proxies then the transaction settings will not be recognised by the class-based proxying infrastructure and the object will not be wrapped in a transactional proxy

因此,我认为您的担忧是:

How can you be sure that Spring does not create a CGLIB proxy

您必须配置基于AOP的事务支持.如果您使用< tx:annotation-driven />那么proxy-target-class的默认值(false)可以满足要求,但可以通过设置以下内容来明确:< tx:annotation-driven proxy-target-class =“ false”< // tx:annotation-driven&gt ;.如果以其他方式配置事务支持(例如,使用Java config),则只需确保TransactionProxyFactoryBean.proxyTargetClass的值为false.

How can you be sure that your @Transactional annotations are respected

这很简单,只需使用@Transactional注释对具体的类进行注释,而不是对接口进行注释.这样可以避免代理问题.

总之,只要TransactionProxyFactoryBean.proxyTargetClass为false,Spring应该使用基于接口的代理,但是,如果您注释一个具体的类而不是一个实现,则代理问题不会影响事务支持.

上一篇:java-Spring数据如何清除事务方法中的永久实体?


下一篇:java-@Transactional,方法内部的方法