2015 24th Friday Spring's AOP

Source: Internet
Author: User
Tags jboss jboss application server

AOP (Aspect-oriented programming: Aspect oriented programing), like IOC, is the core of the spring container, and the functionality of declarative transactions is based on this. But the application of AOP is limited, it is generally only suitable for those with cross-cutting logic applications: performance monitoring, access control, transaction management, and logging.

Important terminology related to AOP:

connection point (Joinpoint)

A specific location where the program executes: Before the class begins to initialize, after the class is initialized, before a method call to the class, after the call, after the method throws an exception. A class or a piece of program code has some specific points that have boundary properties, which are called "Connection points." Spring supports only the connection points of methods, that is, only before method calls, after method calls, when methods throw exceptions, and before and after method calls, these programs perform point-weaving enhancements. We know that the hacker attack system needs to find a breakthrough, there is no breakthrough can not attack, to some extent, AOP is a hacker (because it wants to embed additional code logic into the current class), the connection point is an AOP to the target class to enter the wedge candidate points.

The connection point is determined by two information: the first is the program execution point represented by the method, and the second is the azimuth represented by the relative point. If the connection point before the Test.foo () method executes, the execution point is Test.foo () and the orientation is the position before the method executes. Spring uses pointcuts to position the execution point, while the bearing is defined in the enhanced type.

Tangent Point (Pointcut)
Each program class has multiple connection points, such as a class with two methods, both of which are connection points, that is, the connection point is an objective object in the program class. But in this large number of connection points, how to locate a connection point of interest? AOP locates specific points of contact through the tangent point. The concept of database queries to understand the relationship between pointcuts and connection points is more appropriate: connection points are equivalent to records in a database, and tangency is equivalent to a query condition. Pointcuts and connection points are not a one-to-many relationship, and a pointcut can match multiple connection points.

In spring, the pointcut is described by the Org.springframework.aop.Pointcut interface, which uses classes and methods as the query criteria for the connection point, and the spring AOP rules parsing engine is responsible for parsing the query conditions set by the pointcut and finding the corresponding connection points. In fact, the tangent point should be the execution point rather than the connection point, because the connection points are the specific program execution point that includes the azimuth information before and after the execution of the method, and the tangency is only positioned on a method, so if you want to locate the specific connection point, you also need to provide the azimuth information.

Enhanced (Advice)

Enhancement is a piece of program code that is woven into the target class connection point. Do you think AOP is becoming more and more like hackers: This is not the business class loaded Trojan? Readers can follow this idea to understand the enhancement, because it is more understandable. In spring, the enhancement, in addition to describing a piece of program code, has another information related to the connection point, which is the orientation of the execution point. Combining execution point orientation and pointcut information, we can find a specific connection point! Because the enhancement includes both a piece of execution logic for adding to the target connection point and azimuth information for locating the connection point, Spring provides the enhanced interface with the bearing name: Beforeadvice, Afterretuningadvice, Throwsadvice and so on. Beforeadvice represents the position before the method call, and Afterreturingadvice represents the location after the access is returned. So it's only by combining pointcuts and enhancing both that you can identify specific connection points and implement enhanced logic.

destination Object (target)

Enhanced logical weaving of target classes. Without AOP, the target business class needs to implement all of its own logic, as shown in Forumservice. With the help of AOP, Forumservice only implements the program logic for non-crosscutting logic, and these crosscutting logic, such as performance monitoring and transaction management, can be dynamically woven into specific connection points using AOP.

Introduction (Introduction)
An introduction is a special enhancement that adds properties and methods to a class. Thus, even if a business class originally did not implement an interface, through the introduction of AOP, we can dynamically add the implementation logic of the interface for the business class, so that the business class becomes the implementation class of this interface.

weave in (Weaving)
Weaving is the process of adding enhancements to a specific connection point to the target class, and AOP is like a loom that seamlessly weaves a target class, enhancement, or referral through an AOP loom. We cannot but say that the word "weaving" is too incisive. Depending on the implementation technology, AOP has three ways of weaving:
1) weaving in the compile period, which requires the use of a special Java compiler;
2) class loading period weaving, which requires the use of special class loaders;
3) Dynamic agent weaving, in the run time for the target class to add the way to enhance the generation of subclasses.
Spring uses dynamic proxy weaving, while the ASPECTJ is woven into the compile-time and class-loading stages.

agent (proxy)
Once a class is augmented by AOP, a result class is produced, which is a proxy class that incorporates both the original class and the enhanced logic. Depending on the proxy, the proxy class may be either a class with the same interface as the original class or a subclass of the original class, so we can invoke the proxy class in the same way as the original class.

Facets (Aspect)

Facets consist of tangency and enhancement (introduction), which includes both the definition of the crosscutting logic and the definition of the connection point, and Spring AOP is the framework responsible for implementing the facets, which weaves the crosscutting logic defined by the facets into the connection points specified by the facets.

The focus of AOP is on how to apply the enhancement to the target object's connection point, which consists of two jobs first: first, how to navigate to the connection point by tangency and enhancement, and second, how to write the section code in the enhancement.

Common AOP implementations

AspectJ

ASPECTJ is a language-level AOP implementation that was released in 2001 by the AOP Group of Xerox PARC, and is now updated to 1.6. ASPECTJ extends the Java language, defines the AOP syntax, and provides the ability to weave the crosscutting code at compile time, so it has a dedicated compiler to generate class files that conform to the Java Byte encoding specification. The home page is located in Http://www.eclipse.org/aspectj.

Aspectwerkz
Java-based, simple, dynamic, lightweight AOP framework, which was released in 2002 and supported by BEA systems. It supports a run-time or class-loading period in which to weave the crosscutting code, so it has a special class loader. Now, the ASPECTJ and Aspectwerkz projects have been consolidated to create a unified AOP platform that integrates the power and technology of both. The first release they collaborated with was ASPECTJ 5: Extending the AspectJ language to support code styles similar to ASPECTJ in annotation-based ways.

JBoss AOP
Published in 2004 as an extension of the JBoss Application Server framework, readers can learn more about JBoss AOP from this address: HTTP://WWW.JBOSS.ORG/PRODUCTS/AOP.

Spring AOP
Spring AOP uses a pure Java implementation, which does not require a specialized compilation process and does not require a special class loader, which weaves the enhanced code into the target class at run time by proxy. Instead of trying to provide the most complete AOP implementation, Spring focuses on providing an AOP implementation that integrates with the spring IOC container to solve common problems in enterprise-level development. In spring, we can seamlessly integrate spring AOP, IOC, and ASPECTJ.

Excerpt from: http://stamen.iteye.com/blog/1512388

2015 24th Friday Spring's AOP

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.