Java Dynamic Agent implementation AOP
Brother Lian
The entire development community is currentlyAOP (Aspect oriented programing)praise, there's a lot of supportAOPthe excellentFramework,--Spring, JAC, Jboss AOPand so on. AOPseems to be a fad. Javathe beginner cannot help but feel the emotion,OOPI haven't learned it yet.AOP. This article is not to be elaborated in theory specifically what isAOP,Why do you want toAOP.to learn more about learningAOPcan go to his hometownhttp://aosd.netgo and see. This is just an attempt to show a beginner how to do it with a simple example.AOP.
for the sake of simplicity, the example does not use any third party's AOP Framework, but instead leverages The dynamic proxy capabilities of the Java language itself to implement AOP.
let's go back to AOP itself,AOP is mainly used for logging, performance statistics, security control , transaction processing and other aspects. Its main intention is to log records, performance statistics, security control and so on the code from the business logic code clearly divided, we can take these behaviors one by one as a system to solve the problem, is called the problem-oriented programming ( I do not know AOP whether the problem-oriented programming is defective ). By separating these behaviors, we want to be able to configure them independently into business methods, and changing them doesn't have to affect business method code.
Suppose the system consists of a series of Businessobject The functions of business logic, the system requires logging every time the business logic is processed. Here we omit the specific business logic code.
Public interface Businessinterface {
public void processbusiness ();
}
public class Businessobject implements Businessinterface {
Private Logger Logger = Logger.getlogger (This.getclass (). GetName ());
public void processbusiness () {
try {
Logger.info ("Start to processing ...");
Business logic.
System.out.println ("Here's businesslogic");
Logger.info ("End processing ...");
} catch (Exception e) {
Logger.info ("Exception happends ...");
Exception handling
}
}
}
The combination of code and logging code that handles business logic here poses some difficulties for future maintenance, and also causes a lot of code duplication. The exact same log code will appear in every businessobject of the system .
Java Dynamic Agent Implementation AOP