Reputation: 30
I'm trying to understand what Spring AOP is and how it works. The questions I can't answer are the following:
Upvotes: 0
Views: 478
Reputation: 5589
One of the reasons why Spring AOP is not used compared to AspectJ is that Spring AOP cannot add an aspect to anything that is not created by the Spring factory. What does this mean? Isn't everything created from the Spring Factory?
In short, that Spring builds proxies for the beans in the Container, and relyies on these proxies to implement AOP.
Regarding the comparison with AspectJ:
From the Spring.io reference docs:
" ...
Thus, for example, the Spring Framework’s AOP functionality is normally used in conjunction with the Spring IoC container. Aspects are configured by using normal bean definition syntax (although this allows powerful “auto-proxying” capabilities). This is a crucial difference from other AOP implementations. You cannot do some things easily or efficiently with Spring AOP, such as advise very fine-grained objects (typically, domain objects). AspectJ is the best choice in such cases. However, our experience is that Spring AOP provides an excellent solution to most problems in enterprise Java applications that are amenable to AOP.
Spring AOP never strives to compete with AspectJ to provide a comprehensive AOP solution. We believe that both proxy-based frameworks such as Spring AOP and full-blown frameworks such as AspectJ are valuable and that they are complementary, rather than in competition. Spring seamlessly integrates Spring AOP and IoC with AspectJ, to enable all uses of AOP within a consistent Spring-based application architecture. This integration does not affect the Spring AOP API or the AOP Alliance API. Spring AOP remains backward-compatible. See the following chapter for a discussion of the Spring AOP APIs."
What is meant by "Spring AOP is proxy-based"? How does Spring Core use the Spring AOP module "behind the scenes"?
Proxy based means that, beans are wrapped in another object (the proxy) which itercepts the calls to the obejct and can act upon that interception before calling the real method on the wrapped oject.
There are two ways of implementing this, one is using java Dynamic Proxys (Reflection), the other is using CGLIB, a library that adds the proxy capability at bytecode level.
Spring.io reference docs AOP
An article about Proxys
Upvotes: 1
Reputation: 3304
You should read this for comparison https://stackoverflow.com/questions/1606559/spring-aop-vs-aspectj&ved=2ahUKEwim6cD24-DoAhVMzaQKHd4SDfMQFjAMegQICRAB&usg=AOvVaw1Sps_B0sPQPKRD5N9UtOpA&cshid=1586622128032
Upvotes: 0