Proxy @RabbitListener
and Generics
如果你的服务打算被代理(例如,在 @Transactional
的情况下),当接口具有泛型参数时,你应该记住一些注意事项。考虑以下示例:
If your service is intended to be proxied (for example, in the case of @Transactional
), you should keep in mind some considerations when
the interface has generic parameters.
Consider the following example:
interface TxService<P> {
String handle(P payload, String header);
}
static class TxServiceImpl implements TxService<Foo> {
@Override
@RabbitListener(...)
public String handle(Thing thing, String rk) {
...
}
}
使用通用接口和特定实现,你被迫切换到 CGLIB 目标类代理,因为接口`handle`方法的实际实现是桥接方法。在事务管理的情况下,通过使用注解选项`@EnableTransactionManagement(proxyTargetClass = true)`来配置 CGLIB 的使用。在此情况下,所有注解必须在实现类的目标方法上声明,如下例所示:
With a generic interface and a particular implementation, you are forced to switch to the CGLIB target class proxy because the actual implementation of the interface
handle
method is a bridge method.
In the case of transaction management, the use of CGLIB is configured by using
an annotation option: @EnableTransactionManagement(proxyTargetClass = true)
.
And in this case, all annotations have to be declared on the target method in the implementation, as the following example shows:
static class TxServiceImpl implements TxService<Foo> {
@Override
@Transactional
@RabbitListener(...)
public String handle(@Payload Foo foo, @Header("amqp_receivedRoutingKey") String rk) {
...
}
}