Spring Core framework-the origin and introduction of AOP

Source: Internet
Author: User

First, the origin of AOP technology

The birth of AOP technology was not too late, as early as 1990, researchers from Xerox Palo Alto research Lab (ie Parc) analyzed the limitations of object-oriented thinking. They have developed a new idea of programming that might help developers improve productivity by reducing code duplication modules. With the gradual deepening of research, AOP has gradually developed into a complete set of program design ideas, the application of AOP technology has emerged.

Second, what is AOP?

AOP (Aspect Orient programming), or aspect-oriented programming, is a complement to object-oriented programming and is specifically designed to address cross-focus issues in systems distributed across modules (different methods), often in Java EE applications through AOP To handle some system-level services with crosscutting properties, such as transaction management, security checks, caching, object pool management, and so on. The key to AOP implementation is the AOP proxy, which is automatically created by the AOP framework, which is divided into static agents and dynamic proxies, in which static proxies are compiled using the commands provided by the AOP framework to generate an AOP proxy class at compile time, which is also referred to as compilation , while dynamic agents generate AOP dynamic proxy classes in memory with the help of JDK dynamic proxies, CGLIB, and so on at runtime, and are also known as runtime enhancements. Static proxies are represented by AspectJ, while dynamic proxies are represented by Spring AOP.

Third, the role and value of AOP

In traditional OOP programming, with objects as the core, the entire software system consists of a series of interdependent objects that are abstracted into a single class and allow class inheritance to be used to manage the general-to-special relationship between classes and classes. With the increase of software scale and the gradual upgrading of application, there are some problems that OOP is difficult to solve.

We can analyze, abstract a series of objects with certain attributes and behavior, and through the collaboration between these objects to form a complete software function. Because objects can inherit, we can abstract attributes with the same functionality or attributes into a hierarchical class structure. With the continuous expansion of software specifications, specialization division of labor more and more series, as well as the increasing application of OOP, it also exposes some problems that OOP can not be solved very well.

Iv. Basic concepts of AOP

Aspect (Aspect) : The official abstraction is defined as "a focus of modularity, this concern may be crosscutting multiple objects", in this case, "tangent" is the specific behavior of the class Testaspect, for example, Aserviceimpl.bara () Call is one of the behaviors that the tangent testaspect is concerned about. "Slices" are configured in the ApplicationContext <aop:aspect>.

connection point (Joinpoint) : A behavior such as a call to Aserviceimpl.bara () or Bserviceimpl.barb (String _msg, int _type) to throw an exception during the execution of a program.

notification (Advice) : The action that "slice" produces for a "connection point", for example, the action of logging the methods of all classes under the Com.spring.service package in Testaspect is a Advice. Where a "slice" can contain multiple "Advice", such as Testaspect.

pointcut (Pointcut) : An assertion that matches a connection point, a notification in AOP, and a Pointcut expression association. For example, all the notifications in Testaspect are concerned with connection points that are execution by pointcut expressions (* com.spring.service.*.* (..)) to decide.

target Object : An object that is notified by one or more facets. For example, Aservcieimpl and Bserviceimpl, of course, when actually running, Spring AOP takes the proxy implementation, the actual AOP operation is TargetObject proxy object.

AOP Proxy: There are two kinds of proxy methods in spring AOP, JDK dynamic agent and cglib agent. By default, when the TargetObject implements an interface, the JDK dynamic agent is used, for example, Aserviceimpl; Conversely, cglib proxies, for example, Bserviceimpl. Forcing the Cglib agent to use the Proxy-target-class property of <aop:config> is set to true.

pre-notification (before advice) : A notification that is performed before a connection point (Joinpoint), but this notification does not prevent execution before the connection point. The ApplicationContext uses <aop:before> elements to declare in <aop:aspect>. For example, the Dobefore method in Testaspect

Post notification (after advice) : A notification that is executed when a connection point exits (whether it is a normal return or an unexpected exit). The ApplicationContext uses <aop:after> elements to declare in <aop:aspect>. For example, the Doafter method in Testaspect, so the Doafter method is still executed when Bserviceimpl.barb throws an exception in Aoptest.

post-return notification (after return advice): A notification that is executed after a connection point has completed normally does not include a case where an exception was thrown. The ApplicationContext uses <after-returning> elements to declare in <aop:aspect>.

Surround Notification (Around advice) : A notification that surrounds a connection point, similar to the Dofilter method of the filter in the servlet specification in the Web. You can customize the behavior before and after the call to the method, or you can choose not to execute. The ApplicationContext uses <aop:around> elements to declare in <aop:aspect>. For example, the Doaround method in Testaspect.

notification After an exception is thrown (after throwing advice) : The notification that is executed when the method throws an exception exits. The ApplicationContext uses <aop:after-throwing> elements to declare in <aop:aspect>. For example, the Dothrowing method in Testaspect.

Spring Core framework-the origin and introduction of 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.