Spring AOP notifications, Connection points, pointcuts, facets.

Source: Internet
Author: User

1: Knowledge background

The software system can be regarded as a set of concerns, in which the direct business focus is the direct focus. Providing services for direct-cut concerns is a crosscutting concern.

2: The basic principle of face-tangent

What is tangent-oriented programming

Crosscutting concerns: Features that affect multiple applications (security, transactions, logs)

Section:

Crosscutting concerns are modeled as special classes, which are called facets

Advantages:

Each focus is now concentrated in one place, rather than scattered across multiple codes.
The service modules are more concise and the service modules focus on the core code only.

AOP terminology

    • Notice:
      • Definition: Facets also need to be done. In AOP terminology, the work of a slice is called a notification.
      • Job Description: Notifications define what facets are and when to use them. In addition to describing the work to be done on the slice, the notification resolves when the work is performed.
      • 5 types of notifications that can be applied to Spring facets:
  1. before--calls the notification before the method call
  2. after--invokes the notification after the method completes, regardless of whether the method executes successfully or not
  3. after-returning--in the method Call notification
  4. after-throwing--after execution succeeds
  5. around--notifies the method that the notification was wrapped, performs custom behavior before the notified method call, and after the call
    • connection point:
      • definition: A connection point is a point in which an application can insert a slice during execution. The
      • connection point can be any point where a method is called, when an exception is thrown, when a field is modified, or when a
      • cut-off code can take advantage of these points to insert into the formal process of the application
      • can apply notifications during program execution.
    • pointcuts:
      • Definition: If the notification defines "what" and "when". Then the pointcut defines "where". The tangent point matches one or more connection points that the notification is to weave into.
      • typically uses explicit classes or methods to specify these pointcuts.
      • Role: Define where notifications are applied (at which connection points)
    • facets:
      • definition: Facets are a collection of notifications and pointcuts, common definitions of notifications and pointcuts The full functionality of the slice-what it is and when and where it is done.
    • introduced:
      • introduce allows us to add a method or property to an existing class
    • Weaving:
      • Weaving is a proxy object procedure that is applied to the target object to create a slice. The
      • Slice is woven into the target object at the specified connection point, and there are multiple points in the life cycle of the target object that can be woven into the
  1. Compile-time-facets are woven into the target class during compilation, which requires a special compiler. AspectJ's weaving compiler is the way in which the facets are woven in this manner.
  2. Class load period--facets are loaded into the class
  3. The JVM, which requires a special class loader, can enhance the byte code of the target class before the target class is introduced into the application. AspectJ5 's LTW support this weaving method
  4. Run time-the slice is woven at some point during the application run. In general, the AOP container creates proxy objects dynamically for the target object when it is woven into the plane. Spring AOP is the way in which the facets are woven in this manner.
3:spring Support for AOP
    • Not all AOP frameworks are the same, and they may have strong or weak points on the connection point model.
      • Some allow notifications to be applied to the field modifier level
      • Some support methods call connection points
    • 4 different features of the AOP support provided by Spring
  1. Agent-based classic AOP;
  2. @AspectJ annotation-driven facets;
  3. Pure POJO plane;
  4. Injection type AspectJ plane; Spring
    • Notifies an object during run time
      • By weaving the parcel facets into the proxy class, spring weaves the facets into spring-managed beans during run time.
        The proxy class encapsulates the target class, intercepts the notified method call, and forwards the call to the true target Bean

        When a method call is intercepted, the agent executes the tangent logic before invoking the target Bean method.
        Spring creates a proxy object when the Bean that needs to be proxied is actually applied. If you use ApplicationContext, when applicationcontext loads all beans from beanfactory, Spring creates a proxy object because spring creates a proxy object at run time. So we don't need a special compiler to weave into the facets of Spring AOP.
    • Spring support method to create a connection point
      • Because spring is based on dynamic proxies, spring only supports method connection points.
      • Spring missing support for field connection points does not allow us to finer-grained notifications, such as blocking modification of object fields
      • Spring lacks support for constructor connection points, and I send notifications when the Bean is created.
4: Select a connection point using tangent points
    • The tangency point is used for accurate positioning to determine where to apply the slice notification.
    • Spring Defining pointcuts
      • In Spring AOP, you need to use AspectJ's pointcut expression to define a pointcut.
ASPECTJ indicator
arg () The specified parameter of the restricted connection point is the execution method of the specified type
@args () restrict connection point matching parameters by the execution method that is labeled by the specified annotation
execution () to match the execution method of the connection point
this () restricting connection points matching the Bean reference for the AOP agent to a class of the specified type
target () restricts connection points to match specific execution objects that correspond to classes with the specified type annotations
within () restricting connection points matching the specified type
@within () restricts connection points to match the type indicated by the specified annotation (when using Spring AOP, the method is defined in the class labeled by the specified annotation)
@annotation limit matches a connection point with the specified comment

1. Create your own pointcut

-Execution () indicator select the Play () method of instrument.
The method expression begins with the * number, identifying the type of method return value that we do not care about.
* After we have specified the permission to set the class name and method name.
For the parameter list of the method, use the (.. Identifies the pointcut to select any play () method, regardless of the entry parameter.
-Suppose we need to match the pointcut to match COM only. Springinaction.springidol package. You can use within ()

Note && is the execution () and within () that are linked together to form an and relationship. Similarly, you can also use | | or relationship,! Non-relational
-Create a Spring bean () indicator
Spring 2.5 introduces a new bean () indicator that allows us to use the Bean ID in a pointcut expression to identify the bean
The Bean () uses the bean ID or bean name as a parameter to restrict the pointcut to match only specific beans.
As below, we want to implement instrument's play () method when the notification is applied, but the ID of the qualified Bean is Eddie

You can also use non-operations as a bean app notification other than a bean with a specified ID

In this scenario, the slices are notified to be woven into all the beans with the ID not Eddie

5: Declaring slices in XML

Spring's AOP configuration elements simplify POJO-based facet declarations

AOP configuration Elements Description
Aop:advisor Defining an AOP Notifier
Aop:after Define AOP post notification (regardless of whether the method being notified is successful)
Aop:after-returing Defining AOP after-returing Notifications
Aop:after-throwing Defining AOP after-throwing Notifications
Aop:around Defining AOP Surround Notifications
Aop:aspect Defining facets
Aop:aspectj-autoproxy Start @AspectJ annotation-driven facets
Aop:before Defining AOP Pre-notifications
Aop:config Top-level AOP configuration elements, most AOP: * elements must be contained within an element
Aop:declare-parents Introduces an additional interface to the notified object and implements it transparently
Aop:pointcut Defining pointcuts

Spring AOP notifications, Connection points, pointcuts, facets.

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.