Introduction to Spring AOP--concepts and points of attention

Source: Internet
Author: User

AopWhat is it?

AOP is functionally a function of doing some business logic before and after you can allow you to dynamically add some operations (such as logging, or to determine whether there are permissions, etc.), the addition of these operations is completely decoupled from the original business logic, so that the original business logic is completely transparent.

In other words, this operation is completely separate from the business logic, which may require the crosscutting of multiple modules in the project, and is itself a separate module that runs through the entire project. We can enable or disable this feature as needed. The typical application of AOP is transaction management and logging.


The concepts in AOP

The following terms are not defined by Spring. Because The terminology in AOP is not so graphic, It makes these concepts even more confusing if Spring defines a set of its own.

A) tangent (Aspect): A crosscutting focus of modularity. In an enterprise-class JAVA application, transaction management is a typical example of crosscutting concerns. In Spring AOP , facets can be implemented (based on the @AspectJ) with regular classes (based on Shcema) or with regular classes and annotations .

b) Notification (Advice): An action performed by a slice at a particular connection point is called a notification. Different types of notifications include pre-notification, surround notification, post-notification, and more. Many AOP frameworks, including Spring, simulate notifications as an interceptor and maintain an interceptor chain around the connection point.

c) connection point (Joinpoint): A point in the process of running a program, such as executing a method or handling an exception. In Spring Aop , a connection point always represents the execution of a method.

d) The target object: That is, the object that needs to be notified, so also called the notified object (advisedobject). Because Spring AOP is implemented with a runtime proxy, this object is always a proxy object.

E)  aop proxy (AOP proxy): aop framework created for implementation aop Span style= "font-family: Song Body" > function. In spring frame, aop Proxy can be based on jdk Dynamic agent, also can be based on cglib Agent.

f) Weaving (Weaving): Create a target object with facets and other application types or objects. It can be done at compile time ( such as using the AspectJ compiler ), the load phase, or the runtime. Like other pure Java AOP frameworks,Spring AOP implements weaving at run time.


Notification type

A) Pre-notification (before advice): Executed before the connection point, but cannot block the execution of the connection point (unless an exception is thrown)

b) Back-to-back notification (after returning advice): Executes after the connection point has completed normal execution, such as a method returning normally without throwing an exception

c) Post-exception notification (after throwing advice): Executes after the notified method throws an exception

d) post notification (after final advice): As long as the connection point is executed, it must be executed, whether it is throwing an exception or returning normally

e) surround notification (Around advice): This is the most powerful notification, it can implement both a pre-notification and a post-notification, and it can also control whether ① performs a connection point ② return value ③ throw exception

Surround notifications are the most common kind of notification. Spring AOP like AspectJ provides a full range of notification types, so the official documentation suggests using the "weakest" notification to achieve our needs, why? You know, killing chickens with sledgehammer, and complex things prone to error. For example, if you only need to update the cache with the return value of the method, the surround notification will satisfy this requirement, but using a post-return notification will be better than using a surround notification. Using the most appropriate notification type will make your programming model simple and avoid some potential errors. For example, you don't need to call proceedingjoinpoint 's proceed () method, so you don't make an error calling it.

The concept of connection points, like Pointcuts, is at the heart of AOP. They differentiate AOP from older technologies that only provide interceptors. The pointcut can point the notification to the target without having to be confined to The structure of the OOP. For example, surround notifications can provide transaction management for methods that span multiple objects, such as all business methods at the service layer.


places to be aware of

1, if there is around, and around not proceed the target method, then after will not be triggered

2, if there is around, and proceed the target method throws an exception, then the proceed after the statement will not be executed, but directly to after or after the returing

3, if there is around, and around normal execution, then the return value of the around method, that is, the return value can be obtained in after-returning

4, if there is no around, then after-returning can obtain the return value is the target method after the execution of the return value

5, around does not give the return value, also does not hinder the target method's invocation, does not like the net to cause the target method to stop the call and returns NULL.

6, a transaction is bound to a thread, and if a new thread is opened in a method, it is naturally independent of the previous transaction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.