Posted Jun 16, 2020 • 2 min read
AspectJ's motivation is to discover problems that cannot be handled well with OOP. For example, security is a problem that runs through all the modules of the system. Each module needs to apply a security mechanism to ensure the security of the entire system. The implementation of the security strategy here is actually a cross-cutting concern, using traditional programming to solve this problem Very troublesome and error-prone, this is when AOP comes into play.
In traditional object-oriented programming, each unit is a class, and similar to this aspect of security, they usually cannot be concentrated in one class because they span multiple classes, which results in code that cannot be reused. There is a lot of code redundancy.
The emergence of aspect-oriented programming can handle this kind of scenario. AspectJ came into being and defined AOP. It added Join Point and a few new structures to Java:pointcut, advice, Inter-type declaration and aspect. Pointcuts and notifications dynamically affect program flow, declarations between types are statically affecting the class hierarchy of the program, and cutpoints are the encapsulation of all these new structures.
Relationship with Spring AOP
The original intention of Spring AOP and AspectJ is the same, all for the unified processing of cross-cutting business, but unlike AspectJ, Spring AOP does not provide a complete AOP function(even if it is fully achievable), Spring AOP pays more attention to Spring IOC The combination of containers, combined with this advantage to solve the problem of cross-cutting business.
At the same time, Spring noticed that AspectJ relies on a special compiler(ajc compiler) in the implementation of AOP, which is due to application cost considerations. Spring chose to circumvent this and adopted the implementation principle of dynamic proxy technology to build the internal mechanism of Spring AOP(dynamic Weaving), which is fundamentally different from AspectJ(static weaving). Annotation style of @Aspect form was proposed after AspectJ 1.5. Spring also followed this approach very quickly, so after Spring 2.0, the same annotations as AspectJ were used to reduce the cost of understanding for developers.
JoinPoint(JoinPoint):specific entry point
Aspect:A collection of advices and pointcuts:cutpoints:one or more positions, or a certain type of position, notifications:enhancement of the original function(method)
Weaving:The way the aspect affects the target code. Spring's built-in AOP uses dynamic proxy(jdk dynamic proxy and cglib dynamic proxy) to weave, and AspectJ static proxy weaving
Introduction:Enhancement of the class and juxtaposition with the advice(Advice)
Target:a class of objects to be woven
Proxy:A proxy object that can be generated by weaving in the target object; and calling the method of the target object through the proxy object to achieve the purpose of weaving
Configure @AspectJ in Spring to enable AOP