mercredi 15 avril 2015

Why in Spring AOP the object are wrapped into a JDK proxy that implements interfaces?

I am studying Spring and I have the followig


Consider the following bean definition:



<bean id="clientService" class="com.myapp.service.ClientServiceImpl" />


Now consider the case on which it is declared a pointcut* targetting all methods inside the **clientService bean.


Consider also that the ClientServiceImpl class implements 3 interfaces


Now I know that using AOP the clientService bean is proxied and that this proxy implements all the 3 interfaces.


But what is the exact reason for which all these 3 interface are implemented?


So it seems to me that exist 2 kinds of proxies (correct me if I am saying wrong assertions):




  1. JDK Proxy: used by default from Spring (is it true?) in wicht I have an interface that define the method of the object that I want to proxify. So the concrete implementation of this interface is wrapped by the proxy. So when I call a method on my object I am calling it on its proxy. The call is recognized by a method interceptor that eventually perform the aspect and then is performed the invoked method.




  2. CGLIB Proxy: in wich, it seems to me that, the proxy extend the implementation of the wrapped object adding to it the extra logic features




Something like this:


enter image description here


So it seems to me that Spring use the first kind of proxy that is based on the implementation of interfaces (is it right?):


enter image description here


I think that in AOP the extra logic is represented by the implementation of the method interceptor (is it true?) and the standard logic is represented by the implementation of the method defined into the interfaces.


But, if the previous reasoning are correct, my doubts is: why I need to define these interface and do that the object wrapped by the object implement these interfaces? (I can't understand if the proxy itself implement these interfaces).


Why? How exactly works?


Tnx


Aucun commentaire:

Enregistrer un commentaire