Review Spring AOP
Why aspect oriented programming (AOP)?
We know that Java is an object-oriented (OOP) language, but it has some disadvantages. For example, when we need to introduce a common behavior for multiple objects that do not have an inheritance relationship, such as logging, permission validation, transactions, etc., we can only reference the common behavior in each object. This is not maintainable, and there is a lot of duplicate code. AOP has made up for this shortcoming of OOP.
Some core knowledge of Spring AOP design, interview question?
1. What design pattern does Spring AOP use?
Answer: Proxy mode.
2. Can you briefly talk about your understanding of the agency model?
Proxy mode Balabala…… Remember some examples close to daily life for easy understanding, such as buying train tickets, shortcuts in Windows…
What is the difference between JDK proxy and Cglib proxy?
Instead of creating a proxy class, the JDK creates it dynamically for us at runtime. The JDK proxy is the balabala interface
If the target class does not have an interface, the proxy, Balabala, is generated using Cglib
Both JDK agents and Cglib agents are essentially manipulating bytecode, Balabala
4, let you implement a JDK implementation dynamic proxy? What’s your thinking?
Do as the cat does.
Proxy: Define a Proxy class of your own
InvocationHandler: Define your own InvocationHandler class
ClassLoad: custom class loaders (easy to load classes under our own specified path)
Here’s a quick review and some questions. Read with questions, and the results will be great.
Back to the main point of this article
What are the practical application scenarios of SpringAOP?
-
The Authentication authority
-
Caching cache
-
Context passing
-
Error handling Error handling
-
Lazy loading
-
Was Debugging Debugging
-
Logging, tracing, Profiling and Monitoring record tracking optimized calibration
-
Performance optimization
-
Persistence Persistence
-
Resource pooling Resource pools
-
Synchronization synchronous
-
The Transactions transaction
-
Logging log
Take logs as an example
Without AOP, we would add logging to each method when doing logging, such as
But most of the day processing code is the same, and for code reuse, we might separate the log processing into a new method. But then we still have to insert these methods manually.
But the two methods are strongly coupled, and if we no longer need this feature or want to change it to something else, we have to change it one by one.
Dynamic proxies allow processes to be executed at specified locations. This allows some horizontal functions to be separated into a separate module and then placed in a specific location
Insert these functions. This idea is known as faceted programming, or AOP.
In order to perform these horizontal functions at the specified location, you need to know where the specified location is.
For example, in the figure above, method-level AOP implementations refer to Method2 as a pointcut in a program execution chain, meaning that crosscutting functions are executed before or after Method2, and what is executed? This is all specified by advice. There are five types of advice:
-
Before is executed Before the target method call
-
After (post-notification) executes After the target method call
-
Execution After successful execution of the target method
-
After-throwing The target method throws an exception and executes it
-
Around is a combination of pre and post
Combining pointcuts and advice is a section, a section that specifies which method to execute when and where. Spring AOP defines this aspect like this:
@Aspect
@Component
public class HelloAspect {
@Before("execution(* com.test.service.impl.HelloServiceImpl.sayHello(..) )")
public void sayHello(){
System.out.println("Hello Java Programming Technology Paradise!"); }}Copy the code
Use the @aspect annotation to declare a particular class as an Aspect, so that methods under that class can be declared as horizontal function points and then inserted into the specified location. An execution expression is declared at this pointcut in the following format:
The first position specifies the return value of the method, the asterisk represents the return value of any type, and then the class and method name. The asterisk also represents any, which is any method in the class. In the previous example, the sayHello method was specified. And then the last parameter is the method entry parameter, because Java supports overloading, so this parameter can help you locate more accurately. Two points represent any parameter type. In this way, the execution expression tells the program where to execute the notification. And the methods that are modified by annotations like @before are the content of the notification, that is, what to do.
conclusion
When using Spring AOP, there are two things to note:
1. Declare the aspect class as a bean
2. The class of the method specified by the pointcut also needs to be injected by Spring to take effect
The last
Welcome to pay attention to my public number [programmer chasing wind], the article will be updated in it, sorting out the data will be placed in it.