[Spring practice series] (16) Aspect-oriented programming (AOP) Overview

Source: Internet
Author: User

[Spring practice series] (16) Aspect-oriented programming (AOP) Overview
1. Introduction in software, some actions are common to most applications. Logs, security, and transaction management are indeed important, but are they all actively involved by application objects? Would it be better to let the Application Object focus only on the issues in the business field that it is targeting, and other issues should be handled by other application objects? In software development, features distributed in multiple places in applications are calledCross-concern. In general, these cross-cutting concerns are conceptually separated from the application's business logic (But it is often directly embedded into the application's business logic.). Separating these cross-cutting concerns from the business logic is for cross-section programming. Shows a typical application divided into modules. The core functions of each module provide services for specific business areas, but these modules require similar auxiliary functions, such as security and transaction management. Inheritance and delegation are the most common object-oriented technology for reusing general functions. However, if the same base class is used in the entire application, inheritance often leads to a fragile Object System, and the use of delegation may require the delegated object to perform complex calls. This section provides another option to replace inheritance and delegation. When using Aspect-Oriented Programming, we can define general functions in one place, but we can define the way in which this function is applied in a declaration, you do not need to modify the affected classes. 2. It is not intuitive to use several AOP concepts to describe the functions of AOP. Here, let's take a look.
2.1 In AOP, the work of a aspect is called a notification. The notification defines what the aspect is and when to use it. In addition to describing the work to be completed, the notification also solves the problem of when to execute the work. Before the application and a method are called? After?

Type Description
Before Call the notification before the method is called.
After Call the notification after the method is completed, regardless of whether the method is successfully executed.
After-returning Call the notification after the method is successfully executed.
After-throwing Call the notification after the method throws an exception.
Around The notification wraps the notification method and executes the custom behavior before and after the notification method is called.
2.2 The connection point is a point that can insert a cut surface during application execution. This point can be used when a method is called, an exception is thrown, or even a field is modified. The aspect code can use these points to insert them into the normal process of the application, and add a new action 2.3 cut point. A cut point does not need to notify all the connection points of the application. The Slice helps narrow the scope of the connection points notified by the slice. The Section 2.4 is a combination of the notification and the cut point. The notification and cut-point define all the content about the cut-side -- what it is, and when and where to complete its functions. 2.5 introduction allows us to add new methods or attributes to existing classes. For example, we can create an Auditable notification class that records the status of the object during the last modification. We only need one method, setLastModified (Date), and one instance variable to save this state. Then, the new method and instance variables can be introduced into the existing class. So that they can have new behaviors and States without having to modify these existing classes. 2.6 weaving is the process of applying a cut surface to the target object to create a new proxy object. The section is woven into the target object at the specified connection point. In the lifecycle of the target object, multiple points can be woven. 3. spring does not support all AOP frameworks in the same way. Although there are some differences, creating a cut point to define the joining Point of the cut plane is the basic function of the AOP framework. The main AOP framework is as follows:
  • AspecJ
  • JBoss AOP
  • Spring AOP
Here we mainly look at Spring's support for AOP. Spring provides four distinctive types of AOP support:
  • Agent-based classic AOP
  • @ AspectJ annotation-driven aspect
  • POJO-only Plane Surface
  • Injection AspectJ aspect (applicable to various Spring versions)
3.1 notifications created by Spring written in Java are written in standard Java classes. In this way, we can use the same integrated development environment (IDE) as general Java development. In addition, the switch point of the notification application is usually written in XML in the Spring configuration file. AspectJ is opposite. Although AspectJ currently supports annotation-based aspect, AspectJ was initially implemented in Java extension mode. This method has both advantages and disadvantages. Through the unique AOP language, we can obtain more powerful and fine-grained control and a richer set of AOP tools, but we need to learn additional new tools and syntaxes. 3.2 Spring notifies the object at runtime to wrap the section in the proxy class, and Spring will weave the section into the Bean managed by Spring at runtime. The proxy class encapsulates the target class, intercepts calls to the notified method, and forwards the call to the real target Bean. When a method call is intercepted, the proxy executes the aspect logic before calling the target Bean method. Spring creates a proxy object only when it knows that the application needs to be proxy Bean. If ApplicationContext is used, Spring creates the proxy object when ApplicationContext loads all beans from BeanFactory. Because proxy objects are created only during Spring runtime, we do not need to weave special compilers into the aspect of Spring AOP. 3.3 Spring only supports method connection points. Because Spring is based on dynamic proxy, Spring only supports method connection points. This is different from some other AOP architectures, such as AspectJ and JBoss. In addition to method cut points, they also provide field and constructor access points. Spring does not support field connection points and cannot create more fine-grained notifications. For example, it intercepts modification of object fields, and Spring does not support constructor connection points. We cannot apply notifications when Bean is created. Source: Spring practice

Related Article

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.