[Spring Interview Questions] AOP related interview questions: Concept? scenes to be used? how to use? core?

What is AOP

         AOP is aspect-oriented and aspect-oriented programming. It is a technology that achieves unified maintenance of program functions through pre-compilation and run-time dynamic agents. The common behavior of multiple objects is encapsulated into a module called an aspect , and a certain method is the point of interest .

        In layman's terms: when we do repeated operations in some codes, in order to achieve code reusability, we handle these tasks in a unified manner, and then we have to naturally embed them (using custom annotations) into the specified methods. Location.

        AOP can be used to isolate various parts of the business logic, thereby reducing the coupling between the various parts of the business logic, improving the reusability of the program, and improving the efficiency of development.

As shown in the figure below:         Abstract the horizontal plane into an aspect object, we put the log code into the aspect object, and then program this aspect object. In essence, it is still object-oriented programming, but the idea is aspect-oriented programming. It can be enhanced through aop, reducing duplicate code and reducing coupling. Based on proxy implementation.

5 key terms

1) Cross-cutting concerns , the same type of non-core business extracted from each method

2) Aspect , a class that encapsulates cross-cutting concerns. Each concern is embodied as a notification method; the @Aspect annotation is usually used to define aspects.

3) Advice , each specific work that the aspect must complete. For example, our log aspect needs to record the time before and after the interface is called. It needs to record the time before and after calling the interface, and then take the difference. There are five notification methods:

  • @Before: The notification method will be executed before the target method is called

  • @After: The notification method will be executed after the target method is called

  • @AfterReturning: The notification method will be executed after the target method returns

  • @AfterThrowing: The notification method will be executed after the target method throws an exception

  • @Around: Wrap the entire target method and execute the notification method before and after being called.

4) JoinPoint , which notifies the application of the timing. For example, when an interface method is called, it is the join point of the log aspect.

5) Pointcut , the scope in which the notification function is applied. For example, the application scope of the log aspect in this article is the interface of all controllers. Pointcut expressions are usually defined using the @Pointcut annotation.

Have you used AOP in your project? AOP usage scenarios?

In my project, I have used log operations.

When a user makes a request, we need to record these operations. For example, we need to record who is the user doing the operation, what is the request method, access address, module name, login IP, operation time, etc. This AOP is used. For details on how to operate, see how to use AOP below.

Common usage scenarios

  1. Record operation log

  2. Caching

  3. Built-in transactions in Spring

How to use AOP?

For example, log operation: The core of log operation is to use the surround notification + pointcut expression in aop (find the method to record the log), obtain the parameters of the request method through the parameters of the surround notification, and save them to the database.

The log is used to record the request method (can be obtained by parsing method), URL, user name (can be obtained by parsing Session), operation time, etc.

        1. Import AOP into the project, that is, import it into the xml file

        2. Add an annotation to the aspect class @Aspectto indicate that it is an aspect class. For example, for log operations, you need to write some business in your aspect class: obtain the operation time, the user name of the operator (can be obtained using Session or token), the accessed URL, the request method, etc., and then record these to the database.

        3. Customize an annotation to mark the public code you want to do.  

                Some codes need to be logged, and some do not, so use this annotation to indicate which ones need to be logged.

AOP core: dynamic proxy

        Adding the business logic in the program to the target class will involve dynamic proxy. The core technology of AOP is dynamic proxy , which embodies the core of dynamic proxy (the dao layer of mybatis actually uses jdk dynamic proxy, only the interface) needs to pass CGlib Generate an implementation class as a template class for use by the agent. Spring introduces the template class through the agent and generates the agent class object. Invoke calls the Method method of the object and passes in the target class object and parameters to complete the dynamic agent.

What is the difference between Spring AOP and AspectJ AOP?

  • springAOP is aspect-oriented AOP programming supported by spring.

  • AspectJ is an aspect-oriented framework that extends the Java language. AspectJ defines AOP syntax, and it has a specialized compiler to generate Class files that comply with Java byte encoding specifications.

springAOP is not a complete AOP solution.

AspectJ is the most original AOP technology and is used to provide comprehensive AOP solutions.

Spring AOP is a run-time enhancement , while Aspect is a compile-time enhancement . Spring AOP is based on Proxying, while AspectJ is based on Bytecode Manipulation. Spring AOP has integrated Aspectl, which should be regarded as the most complete AOP framework in the Java ecosystem. Aspectl is more powerful than Spring AOP, but Spring AOP is relatively simpler. If we have fewer aspects, there is not much performance difference between the two. However, when there are too many aspects, it is best to choose Aspectl, which is much faster than Spring A0P. Calling methods at the same level cannot be cut through

Guess you like

Origin blog.csdn.net/KangYouWei6/article/details/132716068