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

I am learning Spring and I have followig

Consider the following bean definition:

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

Now consider the case when a pointcut * is declared , intended for all methods inside ** clientService bean.

Consider also that the ClientServiceImpl class implements 3 interfaces

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

But what is the exact reason all these 3 interfaces are implemented?

So, it seems to me that there are 2 types of proxies (correct me if I say wrong statements):

  • JDK Proxy : used by default from Spring (is that true?) In wicht I have an interface that defines the method of the object I want to maximize. Thus, a specific implementation of this interface ends with a proxy server. Therefore, when I call a method on my object, I call it on my proxy. The call is recognized by the method interceptor, which ultimately executes the aspect, and then the called method is executed.

  • CGLIB Proxy:, in which it seems to me that the proxy extends the implementation of the wrapped object, adding additional logical functions to it

Something like that:

enter image description here

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

enter image description here

I think that in AOP, additional logic is represented by an implementation of the interceptor method method (is this true?), And standard logic is represented by an implementation of the method defined in the interfaces.

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 server really implements these interfaces).

Why? How does it work?

Tnx

+6
source share
1 answer

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).
+3
source

Source: https://habr.com/ru/post/985178/


All Articles