Spring introduction accessing original introduced class

[亡魂溺海] 提交于 2019-12-11 05:06:00

问题


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.

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.

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

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.

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


回答1:


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

  • AopContext.currentProxy() - not so ugly but forces you to use expose-proxy="true"
  • directly inject A in B via Spring - not so bad is both are singletons

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.

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 ... :-(



来源:https://stackoverflow.com/questions/24992232/spring-introduction-accessing-original-introduced-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!