This is the 10th article in the Good Interview series. By the end of this article, you’ll be able to understand the use and principles of Aop in Spring and be able to easily answer any AOP-related interview questions.

In the actual research and development, Spring is the framework we often use, after all, they are too popular, so Spring related knowledge is also a point of interview, today we will talk about Aop. Specially in the weekend tweet, because the article is still more relaxed and witty to read, of course, more important is the weekend I also charge learning, I hope the pursuit of friends also try not to let go of the weekend time, appropriate charge, in order to walk on the peak of life, marry Bai Fumei. (o≖ item one for one ≖)

Next, go straight to the text.

Why AOP

We all know that Java is a language of object-oriented programming (ALSO known as OOP), and OBJECT-ORIENTED programming is an excellent design, but no language is perfect. With OOP, when it comes to introducing common parts for some objects, OOP introduces a lot of repetitive code (which we can call crosscutting code). That’s where Aop comes in. Yes, Aop was designed to make up for OOP’s shortcomings. Aop encapsulates this crosscutting code into a reusable module, thereby reducing the coupling between modules, which also facilitates later maintenance.

What is Aop

Those who have learned Spring know that the core functions of Spring are Ioc and Aop. Ioc’s main role is to decouple between application objects, while Aop can realize the decoupling of crosscutting code (such as permissions, logging, etc.) and their bound objects. For example, the simple and easy to understand small chestnut, where users call many interfaces, Authority certification, we all need to do to determine whether a user has the authority to invoke the interface, if each interface to yourself to do similar processing, may be a little sb is not x, so Aop can come in handy, the processing code into slices, define your slice, connection points and notice, brush brush ojbk run.

To understand Aop, you need to understand terms such as PointCut, Advice, and JoinPoint. Then try to describe it in vernacular Chinese.

PointCut “point of tangency” in fact, the concept of the tangent point is easy to understand, you want to cut something must first know where to cut to the right, the point of tangency format is as follows: execution (* com. Nuofankj. Springdemo. Aop. Service. (..) As you can see, the format uses normal expressions to define classes in that scope, and those interfaces are treated as pointcuts.

Advice A lot of people in the Advice industry define this as notification, but I’ve always felt a little forced. Advice is the definition of when Aop is called. It does have a sense of Advice, but when it is called is really just the following:

  • Before is called Before the method is called
  • After is called After the method completes
  • After-returning is called After successful execution of the method
  • After-throwing is called After the method has thrown an exception
  • Around is called before and after the notified method call

JoinPoint [JoinPoint] JoinPoint [JoinPoint] JoinPoint [JoinPoint] JoinPoint [JoinPoint] JoinPoint [JoinPoint] Yes, it’s a method object that corresponds to the business, because it’s possible to use specific data in specific methods in crosscutting code, and join points do that.

Give a practical application scenario of Aop

Let’s start with two intra-business interfaces, one for chatting and one for buying

execution(* com.nuofankj.springdemo.aop.Service.(..) )

Advice is

Before

JoinPoint Yes

MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod();

The code is easy to understand, in fact, the ChatService and BuyService inside the userId to do permission check logic out to make slices.

So how do you get the specific parameters within the specific business method? Here a new annotation is defined

Send Buddha to the west, wrong, wanke code wanke complete set, and then give the main class running

The result is as follows

Analysis of THE principle of Aop

As for the principle analysis, because everyone does not like to read too long articles, so I plan to split into two articles, the next article will be on the principle and design ideas of Aop analysis, interested friends can pay attention to me a wave.