Spring AOP Summary

Source: Internet
Author: User

I. Introduction to AOP

AOP (aspect-orientedprogramming, aspect-oriented programming) can be said to complement and improve OOP (object-oriented programing, object-oriented programming). OOP introduces concepts such as encapsulation, inheritance, and polymorphism to create an object hierarchy that simulates a collection of public behavior. When we need to introduce public behavior to scattered objects, oop seems powerless. In other words, OOP allows you to define relationships from top to bottom, but it is not appropriate to define left-to-right relationships. such as logging capabilities. Log code is often spread horizontally across all object hierarchies, and has nothing to do with the core functionality of the objects it spreads to. This is true for other types of code, such as security, exception handling, and transparent persistence. This irrelevant code scattered around is called crosscutting (cross-cutting) code, and in OOP design, it leads to a lot of duplication of code, rather than the reuse of individual modules.

AOP, on the contrary, uses a technique called "crosscutting" that splits the encapsulated object interior and encapsulates public behavior that affects multiple classes into a reusable module, called "Aspect", which is the facet. The so-called "aspect", in a nutshell, is to encapsulate the logic or responsibility that is not related to the business, but for the common invocation of the business module, to reduce the duplication of code in the system, to reduce the coupling between modules, and to facilitate future operability and maintainability. AOP represents a horizontal relationship, if the "object" is a hollow cylinder, which encapsulates the properties and behavior of the object, then the aspect-oriented programming approach is like a razor that cuts through the hollow cylinders to get inside the message. The cut-off aspect is the so-called "facet". Then it hands the cut-off slices with a clever capture of the heavens, leaving no traces.

Using "crosscutting" techniques, AOP divides software systems into two parts: core concerns and crosscutting concerns. The main process of business process is the core concern, and the part that has little relation is the crosscutting concern. One feature of crosscutting concerns is that they often occur in many of the core concerns and are essentially similar everywhere. such as permission authentication, logging, transaction processing. The role of AOP is to separate the various concerns in the system, separating the core concerns from the crosscutting concerns. As Avanade's senior program architect Adam Magee says, the core idea of AOP is "separating the business logic in the application from the generic services that support it." ”

The implementation of AOP technology, mainly divided into two categories: first, the use of dynamic Agent technology, the use of interception of messages to decorate the message to replace the original object behavior, the second is the use of static weaving, the introduction of a specific syntax to create "aspects", so that the compiler can be woven into the relevant "aspects" The code.

Two. AOP Usage Scenarios

AOP is used to encapsulate crosscutting concerns, which can be used in the following scenarios:

    • Authentication Permissions
    • Caching Cache
    • Context Passing content delivery
    • Error handling Errors Handling
    • Lazy Loading lazily loaded
    • Debugging commissioning
    • Logging, tracing, profiling and monitoring record tracking optimization calibration
    • Performance Optimization Performance optimization
    • Persistence persistence
    • Resource Pooling resource Pool
    • Synchronization synchronization
    • Transactions transactions
Three. AOP-related concepts

Aspect (Aspect): A focus of modularity, this focus implementation may be additional crosscutting multiple objects. Transaction management is a good example of crosscutting concerns in the Java EE application. The aspect is implemented with spring's advisor or interceptor.

Connection point (Joinpoint): an explicit point during program execution, such as a call to a method or a particular exception being thrown.

Notification (Advice): The action performed by the AOP framework at a specific connection point. Various types of notifications include "around", "before", and "throws" notifications. The notification type is discussed below. Many of the AOP frameworks, including spring, are notification models with interceptors, maintaining a chain of interceptors around the connection point. Four Advice:beforeadvice, Afteradvice, Throwadvice and dynamicintroductionadvice are defined in spring.

Pointcut (Pointcut): Specifies a collection of connection points to which a notification will be raised. The AOP framework must allow developers to specify pointcuts: for example, using regular expressions. Spring defines the Pointcut interface for combining Methodmatcher and Classfilter, which can be clearly understood by name, and Methodmatcher is used to check whether the method of the target class can be applied to this notification. The classfilter is used to check whether the pointcut should be applied to the target class.

Introduce (Introduction): Add a method or field to the class being notified. Spring allows the introduction of new interfaces to any notified object. For example, you can simplify caching by using an introduction that enables any object to implement the IsModified interface. To use introduction in spring, you can implement notifications through Delegatingintroductioninterceptor. Configure the interfaces to be implemented by the advice and proxy classes through Defaultintroductionadvisor.

Target object: The object that contains the connection point. Also known as a notified or Proxied object. POJO (Plain ordinary Java object i.e. simple Java objects)

AOP Proxy: An AOP framework-created object that contains notifications. In spring, an AOP proxy can be either a JDK dynamic agent or a cglib proxy.

Weaving (Weaving): Assembling aspects to create a notified object. This can be done at compile time (for example, with the ASPECTJ compiler), or at run time. Spring, like other pure Java AOP frameworks, completes weaving at run time.

Four. Spring AOP components

The following class diagram lists the main AOP components in spring

Five. How to use spring AOP

You can use spring AOP in a configuration file or programmatically.

The configuration can be done through an XML file, in about four ways:

    1. Configure Proxyfactorybean, explicitly set advisors, advice, target, etc.
    2. Configuring Autoproxycreator, in this way, still uses the defined bean as before, but what is obtained from the container is actually a proxy object
    3. Configure with <aop:config>
    4. Configure with <aop:aspectj-autoproxy>, use ASPECTJ annotations to identify notifications and pointcuts

You can also use Spring AOP in a programmatic way using proxyfactory, and you can set the target object, advisor, and other related configurations by Proxyfactory, and finally get the proxy object through the GetProxy () method.

Reference: http://blog.csdn.net/moreevan/article/details/11977115

Spring AOP Summary

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.