But what is the exact reason all these 3 interfaces are implemented?
If the proxy server has not implemented all these interfaces, the bean cannot be connected to another beans that uses this interface (you will get a ClassCastException). For example, auto-increment all beans of this interface in a bean. In addition, things like getBeanNamesForType will not work if the proxy server does not implement the interface.
So, it seems to me that there are 2 types of proxies (correct me if I say wrong statements)
Yes this is correct. See ScopedProxyMode . By default, Spring will not create a proxy. It only creates a proxy if it needs to wrap a bean to add extra behavior (AOP). Note that there is also a special case of a CGLIB-based proxy server that uses Objenesis to work with subclass objects that do not have a default constructor.
CGLIB Proxy: in which it seems to me that the proxy extends the implementation of the wrapped object, adding additional function logic to it
When you use a proxy based on CGLIB, the constructor for your bean is called twice: once when a dynamically generated subclass is created (to create a proxy) and a second time when the actual bean is created (target).
I think that in AOP, additional logic is represented by the implementation of the interceptor method (is this true?)
The proxy server, in fact, simply encourages the use of a chain of recommendations. This tip is not implemented in the proxy itself. For example, @Transactional advice is located in TransactionAspectSupport . Take a look at the source of JdkDynamicAopProxy .
and standard logic is represented by the implementation of the method defined in the interfaces.
Assuming you are programming against interfaces and using the correct JDK proxies.
But, if the previous reasoning is true, my doubts are: why do I need to define this interface and make the object wrapped by the object implement these interfaces? (I cannot understand if the proxy itself implements these interfaces).
If you want to use proxy based interface, you need to use interfaces. Just make sure that all your beans implementation interfaces, all your recommended methods are defined by these interfaces and that when one bean depends on another bean, this dependency is set using the interface. Spring will take care of creating a proxy server and make sure that it implements all interfaces.
In your diagram, you have "Spring AOP Proxy (this)". You must be very careful using this when using any type of proxy.
- Calls within the same class will not be applied because these calls will not go through the proxy.
- If in one of your beans you pass
this to external code, you pass the goal of the AOP advice. If any other code uses this link, calls will not apply to AOP recommendations (again, you bypass the proxy).