Deeply understand the AOP mechanism and principle of spring

preface

In software development, the functions scattered in many applications are called crosscutting concerns. Generally speaking, these crosscutting concerns are conceptually separated from the business logic of the application. Separating these crosscutting concerns from business logic is the problem to be solved by AOP. AOP can help us modularize crosscutting concerns. In other words, crosscutting concerns can be described as affecting the extra functions of the application. These crosscutting points are modularized into special classes, which are called facets.

Definition of terms

Notification: there is work that must be completed in the aspect. In AOP, the work of the aspect is called notification. The notification defines what the aspect is and when to use it. In addition to describing the work to be completed by the aspect, the notification also solves the problem of when to execute the work. Should it be before a method? After? Called before and after? Or is it called only when the method throws an exception?

Connection point: a connection point is a point that can be inserted into the section during the execution of the application.

Pointcut: it is defined on the basis of connection points. For example, a class consists of more than a dozen methods. Notifications can be inserted before and after each method is called, but you only want to select several methods to insert notifications. Therefore, you define a pointcut to select the methods of notifications you want to insert.

Aspect: aspect is the combination of notification and pointcut.

Weaving in: weaving in is the process of applying the slice to the target object and creating a new proxy object. The slice is woven into the target object at the specified connection point. In the life cycle of the target object, there are many points that can be woven: compilation period, class loading period and running period. Compiler weaving requires a special compiler, class loader weaving requires a special class loader, and spring's AOP weaves notifications at run time.

AOP support for spring

Spring provides four kinds of AOP support, namely: Classic spring AOP mode based on agent; Pure POJO section@ AspectJ annotation driven facets@ Injected AspectJ facet. The notifications created by spring are written in standard Java classes, and the pointcuts used to define notifications are usually written using annotations or XML in spring configuration files.

Spring only supports method level join points.

In spring AOP, pointcuts are defined using AspectJ's pointcut expression language. The most important thing about AspectJ pointcuts in spring AOP is that spring only supports a subset of AspectJ pointcut indicators:

1. Arg() restricts the connection point matching parameter to the execution method of the specified type; 2. @ args() restricts the execution method marked by the specified annotation for the connection point matching parameters; 3. Execution() is used to match the execution method of the connection point; 4. This() restricts the bean reference of the connection point matching AOP proxy to the class of the specified type 5 Target restricts the connection point to match the class whose target object is the specified type 6@ Target () restricts the connection point to match specific execution objects, and the corresponding classes of these objects should have annotations of the specified type 7 Within() restricts the join point from matching the specified type 8@ Within() restricts the join point to match the type marked by a particular annotation 9@ Annotation qualifies matching join points with specified annotations

Spring annotation creation aspect

Target object:

Tangent object:

In the above class, the pointcut expression execution (* * concert. Performance. Perform (..)) For multiple occurrences, we can also avoid writing a long pointcut table every time through the @ pointcut annotation, but it is as follows:

Next, you need to configure the section in the configuration file as follows:

Indicates that the facet agent is started

Surround notification:

Processing parameters in notifications

Declare facets in XML

XML configuration elements provided by spring AOP:

1. < AOP: advisor > define AOP notification; 2. < AOP: after > post notification; 3. < AOP: after returning > return notification 4< AOP: around > surround notification 5< AOP: aspect > define a section 6< AOP: AspectJ AutoProxy > enable facet annotation driven 7< AOP: before > advance notice 8< AOP: config > top level AOP configuration elements; 9. < AOP: pointcut >: define a tangent point

Define tangent point:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>