AOP (Aspect Orient Programming), which is a supplement of object-oriented Programming, is mainly applied to deal with some crosscutting system-level services, such as log collection, transaction management, security check, caching, object pool management, etc.

The key of AOP implementation is the AOP proxy automatically created by AOP framework. AOP proxy can be divided into static proxy and dynamic proxy. Among them, static proxy refers to the use of the command provided by AOP framework to compile, so that the generation of AOP proxy class can be generated in the compilation stage, so it is also called compile-time enhancement; Dynamic proxies, on the other hand, generate AOP dynamic proxy classes “temporarily” in memory at run time with the help of JDK dynamic proxies, CGLIB, etc., and are therefore also known as runtime enhancement.

Section-oriented programming (AOP) is a programming paradigm that aims to improve modularity by allowing separation of crosscutting concerns. AOP provides facets to modularize cross-object concerns.

What AOP does is wrapper around the code we write, such as intercepting or enhancing methods before, after, or after an exception occurs during execution

The concept of Aop

Pointcut: is a (group) expression based on regular expressions, which is a bit convoluted, meaning that it is itself an expression, but it is based on regular syntax. Usually a pointcut selects some execution point in the program that we are interested in, or a collection of execution points in the program.

JoinPoint: A specific execution point in the collection selected by pointCut, which we call JoinPoint.

Advice: Operations and logic to be performed on the selected JoinPoint. About the 5 types, I will not say more, do not understand the students themselves make up the foundation.

Aspect: Is the modularity of our concerns. This concern may crosscut multiple objects and modules, and transaction management is a good example of a crosscutting concern. It is an abstract concept that, from a software perspective, refers to a domain or aspect in different modules of an application. Also pointcut and advice.

Weaving: The process of applying the aspect to the target object to create a new advised object.

What is AspectJ? What can be done?

AspectJ is an easy-to-use and powerful AOP framework

AspectJ stands for Eclipse AspectJ, and its official website is www.eclipse.org/aspectj/.

Quote from the official website:

  • A Seamless aspect-oriented extension to the Javatm Programming Language
  • Java Platform Compatible (Compatible with the Java platform and can be seamlessly extended)
  • Easy to learn and use

It can be used alone or integrated into other frameworks.

Using AspectJ alone requires a specialized compiler, AJC.

The compiler for Java is JavAC, the compiler for AspectJ is AJC, aj is an acronym, and C stands for compiler.

What are the differences between AspectJ and Spring AOP?

As Java developers, we are all familiar with the Spring framework. One of the main features of the Spring framework is AOP. When referring to AOP, AspectJ usually comes to mind.

Spring AOP

1, based on dynamic proxy implementation, the default if the interface is used, the dynamic proxy provided by JDK implementation, if the method is CGLIB implementation

2. Spring AOP needs to rely on the IOC container to manage, and can only operate on the Spring container, using pure Java code implementation

3. In terms of performance, Spring AOP is not as good as AspectJ because it is implemented based on dynamic proxies, which need to generate proxy instances at container startup and increase stack depth on method calls

AspectJ

  • AspectJ comes from the Eclipse Foundation

  • AspectJ is static weaving, implemented by modifying code, with the following opportunities for weaving:

1. Compile-time weaving: If class A adds an attribute using AspectJ and class B references it, the scenario needs to be compile-time weaving otherwise class B won’t Compile.

2. Post-compile weaving: that’s where the.class file is generated, or the weaving jar file is generated, and we use post-compile weaving.

Load-time weaving: Weaving occurs when classes are being loaded. There are several common ways to do this. 1. Custom class loaders do this, which is probably the easiest way to load classes before they are woven into the JVM, so you can define the behavior at load time. 2, the JVM startup specify AspectJ provide agent: – javaagent: XXX/XXX/aspectjweaver. Jar.

  • AspectJ can do what Spring AOP can’t, and it is a complete solution to AOP programming, which addresses the most common ASPECT of AOP in enterprise development (method weaving). Rather than becoming an AOP solution like AspectJ

  • Because AspectJ does the weaving before it actually runs, it generates classes with no additional runtime overhead

conclusion

The following table summarizes the key differences between Spring AOP and AspectJ:

Spring AOP AspectJ
Implemented in pure Java Extended implementation using the Java programming language
No separate compilation process is required The AspectJ compiler (AJC) is required unless LTW is set up
Only run-time weaving can be used Runtime weaving is not available. Support for compile time, compile time, and load time weaving
Not very functional – only method level weaving is supported More powerful – can weave fields, methods, constructors, static initializers, final classes/methods, and more…… .
It can only be implemented on beans managed by the Spring container This can be implemented on all domain objects
Only method execution pointcuts are supported Support for all pointcuts
Proxies are created by target objects, and facets are applied to them Aspects are woven directly into the code before the application is executed (at run time)
Much slower than AspectJ Better performance
Easy to learn and apply It’s more complicated than Spring AOP

If the content of this article is helpful to you, welcome to pay attention to the public account :YouXiangJAVA (ID:YouXiangJAVA), there are more technical dry goods, and carefully prepared a list of programmer books. Looking forward to your arrival!