Using ASPECTJ constructs the Aspect library

Source: Internet
Author: User
Tags abstract aop definition inheritance jboss

1. Aspect Library

As you all know, the core of the various AOP tools is the declaration of Pointcuts (Pointcut) and notifications (advice). The pointcut describes where the execution of the main program meets the execution of the aspect, which is the location of the crosscutting; The notification describes what action should be taken when a matching pointcut is encountered during program execution. Suppose you have developed an aspect and feel it works for other projects, you can generalize this aspect and isolate it into your own stand-alone project to form a library, the Aspect library. The Aspect library provides the internal execution logic and infrastructure of a feature that connects the aspect library to a specific project by instantiating the pointcut. For example, it provides a library of application performance monitoring that implements all the performance monitoring-related methods and notifications, and when an application uses the library, it simply defines the library's pointcut as applying a specific connection point without caring for the specific implementation of the performance monitoring feature. This is the basic concept of the aspect library.

Aspect libraries are an embodiment of the extensibility of AOP tools, and currently commonly used AOP tools such as JBoss AOP, Spring AOP, AspectJ, and so on, have support for the aspect library, but because of their different approaches to AOP, the definition and usage of the aspect library are not the same. In JBoss AOP and spring AOP, the implementation of notifications is defined by the common Java syntax, and the binding of pointcuts to notifications is implemented through explicit XML documents or annotations. So applying a aspect library to an application can easily be implemented by defining a new notification binding in XML or through annotations. And the JBoss and spring systems themselves have provided a good aspect library for users to use.

ASPECTJ is an extension of the Java language syntax and semantics, it provides its own set of processing keywords, which determine the use of ASPECTJ construction aspects of the library method has a certain specificity. So the aspect library of ASPECTJ constructs has not been as pervasive as JBoss AOP and Spring AOP, and ASPECTJ has not provided any aspect libraries. Thankfully, ASPECTJ has been providing support for annotations from the AspectJ5 version, and its editing tool AJDT has gradually added support to the Aspect library, making it easier to use the ASPECTJ construction aspect library. This article describes how to use ASPECTJ constructs the aspect library.

2. ASPECTJ technical support to the aspect library

The implementation of the Aspect library is the instantiation way of the pointcut. ASPECTJ support for Pointcut definition methods leads to two completely different aspects of the library implementation method-the use of abstract aspects (abstraction aspect) and methods of using annotations. This chapter provides a detailed description of both approaches.

ASPECTJ is an extension of the Java language. Aspect is marked with the Aspect keyword in the AspectJ language, which resembles a Java class, can be defined as abstract, and there is an inheritance relationship. The pointcut is labeled with the keyword pointcut in the AspectJ language, it has a complete set of syntax to describe the pointcut, or it can be defined as abstract, that is, there is no actual definition of the pointcut. Abstract pointcut can only be defined in abstract terms. As shown in Listing 1, abstract aspect a defines an abstract pointcut named Publiccall. Abstract aspects, like abstract classes, can be inherited, and the aspect of inheriting abstraction must overload the abstract pointcut, that is, giving the abstract pointcut the actual definition. As shown in Listing 2, Aspect B inherits the abstract aspect A, overloading the pointcut named Publiccall, giving it a specific definition.

Listing 1 Abstract aspects

public abstract aspect A {
    abstract pointcut publicCall(int i);
  }

Listing 2 inherits the child aspect of the abstraction

public aspect B extends A {
    pointcut publicCall(int i): call(public Foo.m(int))  && args(i);
}

ASPECTJ's support for inheritance and abstraction is the foundation of our construction aspect library. Abstract aspects include abstract pointcuts and specific notifications that are conforming to the characteristics of the aspect library and can be constructed using an abstract aspect to construct the Aspect library file. The child aspect of the inheritance abstraction must specify the Pointcut, which can be used as the implementation of the Aspect library in the specific application program. To deepen our understanding, we will use the abstract aspect technology in the following chapters to create a simple aspect library of record traces and extend it to apply to specific projects.

The annotations that are supported from ASPECTJ 5 are the technical basis for another construction aspect library. Java5 introduces the annotation type, which expresses the metadata information of the members of the program in the form of annotations, using the symbol @ mark. There are a number of Java program members that can be Java5 in annotations, AspectJ 5 can support annotations including cosmetic aspects, methods, properties, constructors, and notifications, as well as annotations for parameters that decorate methods and notifications, but do not support annotations on Pointcut and DECLARE statements. To support annotation types, AspectJ 5 extends the pointcut syntax to match the existence or absence of annotation types. For example, the pointcut named Onewaymethod in Listing 3 can match all method calls decorated with annotations @oneway.

Listing 3 contains the annotations of the Pointcut

public aspect C {
pointcut onewayMethod: call(@Oneway * *(..));
}

The AspectJ 5 support for annotations simplifies the implementation of the library, and it is easy to think that when implementing an aspect library to an application, annotations can be used to indicate the location of the specific pointcut. When constructing the Aspect library file, you only need to define the pointcut that is relevant to the annotation and specify the specific notification content on the pointcut. We will also introduce the process of using annotations to make simple aspect libraries in the following sections.

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.