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 AOP technology in Spring framework?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces what the AOP technology is in the Spring framework, it has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to know about it.

AOP is not an original technology in spring, because AOP can be understood as a programming idea, such as OOP (object-oriented programming). It is only recently that it has been used in software development as a technology. So what exactly is AOP? The full name of AOP is Aspect Oriented Programing. Looking at the translation, we can basically guess the name of AOP, which is what we often hear about facet programming. Let's use the following example to show what problems AOP can solve in my approach.

We know in daily development that if there is the same code in multiple classes, then we should consider extracting a common abstract class or interface, extracting the same code from multiple classes to the abstract class for implementation, and putting different logic into the corresponding subclasses to deal with, which is not only convenient for us to develop and maintain, but also follows the idea of software refactoring. So does this really eliminate duplicate code in subclasses? Let's look at the following example:

We look at the above code UserService class there is only simple code, we use an output statement instead of specific business logic, but in our daily development, we often need to add things to the corresponding logic to solve the consistency problem, like the above code. At this time, the disadvantages of the above code will be shown, that is, there will be a large number of transaction management code in the business code, and there is no way to solve these codes by extracting abstract classes. At this point, if we want to eliminate the transaction code into the business logic, we have to consider using AOP to solve the problem. So how to use AOP in spring to solve the above problems? Don't worry, let's throw a brick to attract jade in the following article we will look at the specific solutions, let's take a look at the knowledge of other AOP methods in spring.

Some basic terms are defined in spring's AOP, and we will describe the specific names and functions of these terms below.

Connection point

A specific location where the program executes, that is, before, after the method call of the class, after the method throws an exception, and so on. In spring, the join point only supports the join point of the method, that is, before, after the method call of the class, after the method throws an exception, and so on.

Tangent point

The pointcut also locates a specific connection. A simpler analogy is that join points are equivalent to database records, while pointcuts are equivalent to query conditions. Therefore, in spring, there is not an one-to-one correspondence between pointcuts and join points, and one pointcut can match multiple join points.

Enhancement

An enhancement is the code that is added to the connection point of the target class, that is, the code that adds things in our example above. Because of this, all the enhanced interfaces that spring provides for us are with azimuth names, such as BeforeAdvice, AfterReturningAdvice, and so on.

Target object

That is, to add a target class that enhances the logic.

Introduction

A special enhancement that adds properties and methods to a class. Even if the class does not implement an interface, the implementation logic of the interface can be dynamically added to the class through introduction.

Weave into

Weaving is the process of adding enhancements to the specific join points of the target class. There are three types of weaving in AOP. They are:

Compile-time weaving

Class loading period weaving

Dynamic proxy weaving that adds enhanced subclass generation methods to the target class at run time.

Spring uses dynamic weaving, while AspectJ uses compiler weaving and class loading weaving.

Agent

When the target class is enhanced, a new proxy class that includes enhanced logic is generated. We use this proxy class to complete our crosscutting logic.

Cut plane

The section consists of tangent points and enhancements, which includes both the logic of crosscutting and the definition of join points.

Thank you for reading this article carefully. I hope the article "what is the AOP Technology in the Spring Framework" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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