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