Spring 介绍访问原始介绍类 [英] Spring introduction accessing original introduced class

查看:24
本文介绍了Spring 介绍访问原始介绍类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Spring 框架 AOP 扩展一些类 AImpl(实现接口 A),以便能够实现新的接口 B.

I am trying to use Spring framework AOP to extend some class AImpl (which implements interface A) to be able to implement also new interface B.

我正在使用 @DeclareParents 并实现 Aspect.不幸的是,我需要访问 A 的一些方法才能在其中实现业务逻辑.

I am using @DeclareParents and implementing Aspect. Unfortunatelly I need to access some methods of A to be able to implement business logic within it.

有没有人知道比将 A 作为参数添加到接口 B 方法更好的解决方案?

Does anyone knows better solution than adding A as a parameter to interface B method ?

有没有人成功地使用 Spring AOP 引入以我需要的方式扩展类?我能够找到的所有示例都仅以静态方式"扩展类 A,而无需从 A 调用方法.

Does anybody used successfully Spring AOP introductions to extend class in such a way I need to ? All the samples I have been able to find extends class A only in "static way" without need to call methods from A.

到目前为止,我只发现了 AopContext.currentProxy() 方法,我认为它非常丑陋.

So far I have found only AopContext.currentProxy() method which I consider as very ugly hack.

推荐答案

恐怕你只有两种方法可以从 B 访问 A

I'm afraid you have only 2 ways to get access to A from B

  • AopContext.currentProxy() - 不是那么难看,而是强迫你使用 expose-proxy="true"
  • 通过 Spring 将 A 直接注入 B - 还不错,两者都是单例

我尝试在接口 B 的方法上使用环绕建议,希望访问将实现两个接口的原始代理.但是 Spring 将其替换为另一个仅实现 B 以具有正确目标的代理.

I tried to use an around advice on a method of interface B hoping to access the original proxy that would have implemented both interfaces. But Spring replaces it by another proxy that only implements B to have a correct target.

实际上还有第三种方式,但恕我直言,这种方式真的很丑:让接口 B 的所有方法都明确地采用一个参数实现 A ... bof ... :-(

There is effectively a third way but IMHO this one is really ugly : have all methods of interface B explictely take a parameter implementing A ... bof ... :-(

这篇关于Spring 介绍访问原始介绍类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆