The first problem I encountered in my work was solved with Spring AOP. Spring AOP is rarely actually used, but it always solves important problems. Spring AOP solves an important problem in an easy way.
The advent of Spring AOP
In my work, I often encounter the task of maintaining the old WEB project. There are many problems in the old project. One of them is that there is no log system. My job was to add logs to the old project. Adding logging should be easy, add a Log4j dependency and fill out the configuration. Create log objects in each of the Controller and Server classes and add log prints at key locations.
The problem is that the project has been written, and there are thousands of ways to log it one by one. It’s exhausting, and it’s repetitive, and it’s painful. Then I thought of using Spring AOP to add logging, and it was easy enough to solve the problem.
What is Spring AOP
Let’s get down to business by first introducing what Spring AOP is. The official introduction is aspect oriented programming. First, it’s a programming idea, a complement to object-oriented programming (OOP). The idea of object-oriented programming is that everything is an object. The process of working with an object is called a method. Aspect oriented programming refers to treating a method as a whole and changing the execution of the method without changing the original code of the method.
The analogy is a tomato and a plum. You cut the tomato, you put a plum inside, and then you close the tomato, it’s the same tomato, it’s the same tomato, but it’s got the plum flavor inside. On the physical level, the tomato has been changed to include a plum. But when it comes to code, we can change the logic of the method without changing the source of the method.
Spring AOP principle
How does this magical effect work? This is thanks to the proxy pattern among the 23 design patterns. To understand the mechanics of aspect oriented programming, you must first understand the proxy pattern.
The proxy pattern is easy to understand, just by analogy. Let’s say you’re Eddie Peng (Eddie Peng for all readers), but you only focus on acting, and you only get paid for acting. Now you are so famous that many companies seek you as a spokesman. But you don’t have time to negotiate. That’s when you need an agent. An agent will help you get endorsements, manage your fans, and get you shows. You still have the same job, but you get paid more.
In this process, “Eddie Peng” represents a native object. The agent who fully represents Peng is called the agent. When I need Eddie Peng to perform, I go to an agent. This process is called the agency pattern.
Flow chart:
There are two common implementations of the proxy pattern: dynamic proxy and static proxy
Let’s start with static proxies: The advantage of static proxies is that they are easy to understand and easy to observe. Disadvantages are difficult to maintain, rigid. Not suitable for dynamic project requirements. Simple version of static proxy:
This is static proxy, where both the target object and the proxy object need to implement their own superclass methods. Once a superclass is added or modified, it needs to be changed in all subclasses, making maintenance difficult. So static proxies are rarely used in a project.
Say dynamic proxy again: the advantage of dynamic proxy is: good use, good maintenance. Disadvantages: hard to understand, hard to observe. Now go to code: Simple version dynamic proxy: interface part unchanged, “Eddie Peng” part unchanged
Notice the proxy object next
The so-called dynamic proxy is to use reflection to create proxy objects. The benefit of using reflection to create proxy objects is high versatility. This avoids the painful behavior of changing the upper-layer interface and changing the code for the proxy object.
Get started with Spring AOP
Now that you understand what Spring AOP does, let’s show you how Spring AOP can be used in a real project. Spring AOP already implements automatic creation of dynamic proxies; we just need to learn to invoke them correctly.
Graphic display:
Step 1: Import Spring AOP dependencies
PS: Update the dependency after adding a new dependency
Step 2: Annotate @Aspect with an Aspect
Aspect: Defines that the current class is an aspect class and needs to generate a different proxy object for each class that satisfies the aspect conditions.
Step 3: Define the required notifications.
Notification: Notification is when the code defining the proxy object is to take effect.
The following types of notifications are commonly used. You can select them as required
Step 4: Fill in the required section expression
Aspect expression: defines which conditions are met to generate its proxy class.
Collate common pointcut expressions:
Execution Result:
The advantages and disadvantages of Spring AOP
Spring AOP has the obvious advantage of adding new logical code without changing the original code. On the downside, the logic is incoherent and the reading experience is poor. After all, these are specialized tools designed to solve specific problems.
We should be mindful of Spring AOP when we need to batch add logic to methods.
Java inside a lot of concepts, knowledge is also a lot of. A lot of concepts and knowledge point is designed to solve specific problems and was born, we can not remember all the knowledge, but at the time of need to use a knowledge point, need to be able to think of what knowledge to solve the problem, continue to encounter problems, solve the problem in the process of our understanding of this nature will be more profound, but don’t stop, Think how and why. This is the path to being an architect.
Spring AOP summary
To review the key points of this chapter:
AOP is a programming idea — aspect oriented programming
AOP implementation principle – proxy pattern. The proxy mode is divided into dynamic proxy and static proxy.
Spring AOP: 1. Import dependencies. 2. Use aspect comments. 3. Use notification comments. 4. Declare expressions using pointcuts.
You can see it here. Handsome little brother, beautiful as a flower little sister, remember to like after reading.