What is AOP?

Source: Internet
Author: User

What is AOP?

Aspect-oriented programming (AOP), which is similar to OOP object oriented programming. AOP is a continuation of OOP and short for aspect oriented programming, which means Aspect-Oriented Programming. What is the aspect?

It can also be considered as a concern. There are many concerns in a system, such as logging and authorization. The object-oriented trend is to make the system coupling smaller and smaller, as is the design pattern. However, these concerns are the most common functions, which may run through the system and cause many problems during system development, especially maintenance. The same code is repeated everywhere.
For example, record logs:
Public interface mybizinterface {

Public void mybizmethod ();
}

Public class mybizobject implements mybizinterface {

Private logger = logger. getlogger (getclass (). getname ());

Public void mybizmethod (){

Try {

Logger.info ("start to execute ...");

// Do something here

Logger.info ("End Processing ...");
} Catch (exception e ){

Logger.info ("exception happends..." + E. getmessage ());

}
}
}
The above code is very simple. A simple business logic mybizmethod (), of course, we need to record the log, but the code of the business logic is mixed with the log record code, this brings some difficulties to future maintenance, and there are a large number of code duplicates at the same time.
In the same aspect (Focus), there should be priorities and priorities. For example, in the above example, the business logic is obviously more important, but you can also say that the log record is important. Therefore, we should separate the logging code (secondary) from the important business logic. To separate these codes involves a problem. We must know when the commercial logic code will be called, so that we can insert the log record code. To intercept a method, we can use the callback method or dynamic proxy. Dynamic proxies are generally more flexible. Most of the AOP frameworks currently use dynamic proxies.
With AOP, we can intercept the main business logic and [inject] the code that we are not particularly important to. AOP prevents code confusion. (

# Java Column

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.