Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the design idea and principle of Spring AOP?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly analyzes the relevant knowledge of what the Spring AOP design idea and principle is, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor to have a look, and follow the editor to learn more about "what are the design ideas and principles of Spring AOP".

Preface

Spring provides support for AOP (Aspect Oriented Programming), so what is AOP? This article will interpret the concept of AOP from another perspective to help you better understand and use Spring AOP.

1. The characteristics of Java program running in JVM

When we write a main () method in a class Foo, and then execute java Foo, your Java program journey begins, as follows:

So what did JVM do for you during this execution?

When you execute java Foo, JVM creates a main thread main that starts executing your code with the above main () method as an entry. Each thread maintains its own Stack in memory, recording the execution of the whole program. Each element in the stack is called Stack Frame, which represents a method call and records the information of the method call; in fact, when we call a method in the code, it corresponds to the stack frame in memory.

At a particular point in time, the stack within a Main thread looks like this:

From the perspective of the thread stack, we can see that the basic unit of JVM processing Java programs is method calls. In fact, the most basic unit of instructions (that is, atomic operations) executed by JVM is machine bytecode of the nature of assembly language. The reason why we are talking about the basic execution unit of the Java program when calling a method is from a more macro point of view.

How do I get the contents of the virtual machine thread stack (that is, the method call procedure)?

Imagine how to get the contents of a method call in the JVM thread stack? I'm sure all Java programmer know the answer. Java Programmer sees it almost every day-when our code throws an exception without catching it, or when an Error error occurs at run time, we get a very annoying Log message like this:

Of course, we can detect the contents of the JVM thread stack in other ways than when the code throws an exception. You can create a fake Exception instance through the Thread.dumpStack () method, and then output the contents of the current thread stack recorded by this Exception instance to the standard error stream. For example, I executed the Thread.dumpStack () method somewhere in the code and output the following result:

2. Java program execution flow [understand the concepts of AOP, Join Point and point cut]

If you consider the execution of Java programs from the perspective of virtual machine thread stack, you will find that the process of running a real program is the process of method calls. We arrange the method calls in a string according to the order in which the methods are executed, which constitutes the Java program flow.

If we arrange the method calls in the above thread stack according to the execution flow, we will have a diagram similar to the following:

Based on the time series, we can line up the method calls. Each method call can be treated as a node in the Java execution flow. This node is called Join Point, or join point, in AOP terminology. The process of running a Java program is a process in which several connection points are connected and executed in sequence.

In our normal object-oriented thinking, we consider how to implement our business logic through method calls according to the time series. So what is AOP (that is, aspect-oriented programming)?

Usually object-oriented programs, the code is developed vertically according to the time series, and they all have one thing in common: they are all deployed as the basic execution unit of the method call. Treat the method call as a connection point, then the program execution flow strung together by the connection point is the execution process of the whole program.

AOP (Aspect Oriented Programming) considers the whole program from another point of view. AOP takes every method call, that is, the connection point, as the entry point for programming. From the point of view of the logic of execution, it is equivalent to the horizontal cut of the program executed according to the timeline before. It is equivalent to cutting the previous program horizontally into several faces, that is, Aspect. Each face is called a section.

So, as far as I understand it, AOP is essentially a programming idea for method calls.

Since AOP is programmed for aspects, which aspects (that is, join points Joint Point) you need to choose as your programming objects?

Because the aspect is essentially every method call, the process of selecting the section is actually the process of selecting the method. Then, the selected facet (Aspect) is called a Point Cut in AOP terminology. Pointcuts are actually the process of selecting the Join point you are interested in from all the join points.

The Spring AOP framework uses methods to match expressions to express the pointcut (Point Cut). As for what the detailed expression syntax is not the focus of this article, please refer to the corresponding documentation of Spring.

Since AOP is programming for method calls (join points), and now that you have selected the link point you are interested in-Point Cut, what kind of programming can AOP do for it? What can AOP do?

Before we understand this, we need to know a very important question: since AOP is programming method calls, how does AOP capture method calls? To figure this out, you have to understand the agent pattern in the design pattern. Let's first take a look at what the execution flow of a Java program that introduces the proxy pattern looks like.

3. Java program execution flow with proxy pattern (mechanism of AOP implementation)

We assume that in our Java code, proxy objects are created for instance objects through the proxy pattern, and access to these instance objects must be through proxies, then the Java program execution flow that adds proxy objects becomes a little more complicated.

Let's take a look at the schematic diagram of the execution flow of the Java program after adding the proxy object:

As can be seen from the above figure, whenever you want to call the method of an instance object, it will pass through the corresponding proxy object of the instance object, that is, the control of execution will be handed over to the proxy object first.

About the agent mode

Agent pattern is a frequently used and important design pattern in Java code. The proxy pattern can provide some additional functions for some objects in addition to implementing their own functions, as shown in the following figure:

The Java program execution flow with proxy mode is added so that all method calls pass through the proxy object. As far as the Spring AOP framework is concerned, it is responsible for controlling the proxy objects inside the container. When we call any of the non-final public methods of an instance object, the entire Spring framework knows.

The SpringAOP framework at this time plays the role of God to some extent: it knows anything you do within the framework, and your non-final public method calls to each instance object can be detected by the framework!

Now that the Spring proxy layer is aware of every method call you make to the instance object, Spring has a chance to insert Spring's own business code in the process of the proxy.

4. How Spring AOP works

It has been introduced earlier that AOP programming should first choose the join point it is interested in-that is, the Point cut, so what can AOP do with the pointcut? Let's first refine a connection point in proxy mode, and you will see the process shown in the following diagram:

In order to make it easier for us to understand the AOP of Spring, I simplify the functions of the agent role here to make it easy for you to understand. (note: the proxy role of the real Spring AOP can only be much more complex than this. It is only simplified here, which is easy for everyone to understand. Please do not prejudge.) the agent role of the agent mode needs to be considered at least three stages:

1. What should I do before calling the method of the real object?

two。 What do you need to do if an exception is thrown during the call to the method of the real object?

3. After calling the method of the real object, the result is returned. What do you need to do?

AOP's programming of this method call is to insert its own business code for these three phases.

Now let's assume that the class of the current RealSubject role is org.luanlouis.springlearning.aop.FooService, and the method signature for the current join point is: public void foo (). Then the three stages of the above proxy object will have the following processing logic:

1. Before calling the method of the real object

Proxy tells Spring AOP, "I'm going to call public void foo () of class org.luanlouis.springlearning.aop.FooService. Do you have any suggestions before calling it?"

Spring AOP then uses the class name and method signature provided by proxy to try to match whether it is within the pointcut of interest. If it is within the pointcut of interest, Spring AOP will return the MethodBeforeAdvice processing suggestion and tell proxy what to do.

two。 What do you need to do if an exception is thrown during the call to the method of the real object?

Proxy told Spring AOP, "I threw an exception during my call to public void foo () of class org.luanlouis.springlearning.aop.FooService. Do you have any suggestions for handling it?"

Based on the type and method signature provided by proxy, Spring AOP determines that it is within the pointcut it is interested in, and then returns the corresponding processing suggestion ThrowsAdvice, telling proxy what action to take during this period.

3. After calling the method of the real object, the result is returned. What do you need to do?

Proxy told Spring AOP, "I finished calling the public void foo () of the class org.luanlouis.springlearning.aop.FooService and returned the result. What advice do you have now?"

Based on the type name and method signature provided by proxy, Spring AOP determines that it is within the pointcut that it is interested in, then returns the AfterReturingAdivce processing suggestion, which proxy gets, and then executes.

The above diagram has made it clear what Spring AOP should do: give corresponding processing suggestions according to the specific period of execution of specific methods of a particular class provided by proxy. To do this, Spring AOP should implement:

1. Determine what kind of methods you are interested in? -that is, to determine the Point Cut of the AOP, which can be done through the pointcut (Point Cut) expression

two。 What suggestions are given for a specific period of execution of the methods of the corresponding class? -this requires Spring AOP to provide corresponding advice, that is, we often talk about Advice.

About "what is the design idea and principle of Spring AOP" is introduced here, more related content can search previous articles, hope to help you answer questions, please support the website!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report