问题
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