Java beginners how to take the first step of AOP -- use Java Dynamic proxy to implement AOP

Source: Internet
Author: User

Currently, the entire development community is focusing on AOP (Aspect Oriented Programing), and a large number of excellent frameworks that support AOP, such as Spring, JAC, and Jboss AOP, have emerged. AOP seems to be a trend for a while. Java beginners can't help but feel deeply that OOP has not been able to learn it, and it has come to AOP. This article is not to explain in theory what is AOP, why do you want to conduct AOP. To learn more about AOP can go to its hometown http://aosd.net to look. Here is just a simple example to show beginners how to implement AOP.

For the sake of simplicity, the example does not use any third-party AOP Framework, but uses the dynamic proxy feature inherent in the Java language to implement AOP.

Let's go back to AOP. AOP is mainly used in logging, Performance Statistics, security control, transaction processing, and other aspects. Its main intention is to clearly divide the log records, Performance Statistics, security control, and other codes from the commercial logic code. We can regard these behaviors as the problems to be solved by the system one by one, is the so-called problem-oriented programming (I wonder if it is inappropriate to translate AOP into question-Oriented Programming ). Through the separation of these behaviors, we hope that they can be configured independently into the business method, and to change these behaviors does not need to affect the business method code.

Assume that the system has completed the business logic functions by a series of BusinessObject, and the system requires that the log be recorded every time the business logic is processed. The specific business logic code is omitted here.

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 here.
System. out. println ("here is business logic ");

Logger.info ("end processing ...");
} Catch (Exception e ){

Logger.info ("exception happends ...");
// Exception handling
}
}
}

The code that processes the business logic and the log record code are mixed here, which brings some difficulties to future maintenance and may cause a lot of code duplication. The identical log Code will appear in every BusinessObject of the system.

According to the idea of AOP, we should separate the log record code. 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. Here we also use dynamic proxy as an example.

JDK and later provide support for dynamic proxies. Programmers implement java. lang. reflect. the InvocationHandler interface provides an execution processor and then uses java. lang. reflect. proxy gets a Proxy object that executes commercial methods. When a commercial method is called, the execution processor is automatically called.

With JDK, all we have to do is provide a log processor.

Public class LogHandler implements InvocationHandler {

Private Logger logger = Logger. getLogger (this. getClass (). getName ());

Private Object delegate;

Public LogHandler (Object delegate ){
This. delegate = delegate;
}

Public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {

Object o = null;
Try {

Logger.info ("method stats..." + method );
O = method. invoke (delegate, args );
Logger.info ("method ends..." + method );

} Catch (Exception e ){
Logger.info ("Exception happends ...");
// Excetpion handling.
}
Return o;
}

}

Now we can remove all the log processing code in BusinessObject.

Public class BusinessObject implements BusinessInterface {

Private Logger logger = Logger. getLogger (this. getClass (). getName ());

Public void processBusiness (){

// Business processing
System. out. println ("here is business logic ");
}
}

The code for the client to call a commercial method is as follows,
BusinessInterface businessImp = new BusinessObject ();

InvocationHandler handler = new LogHandler (businessImp );

BusinessInterface proxy = (BusinessInterface) Proxy. newProxyInstance (
BusinessImp. getClass (). getClassLoader (),
BusinessImp. getClass (). getInterfaces (),
Handler );

Proxy. processBusiness ();

The program output is as follows:
INFO: method stats...
Here is business logic
INFO: method ends...

So far, our first small attempt is complete. We can see that after using AOP, the logging and business logic code are completely separated.

In the future, you only need to modify the log record processor, and the business object itself (BusinessObject) does not need to be modified. In addition, this log record will not cause repeated code. All commercial processing objects can reuse this log processor.

 
Of course, in actual application, this example is too rough. Because JDK's dynamic proxy does not directly support registering multiple InvocationHandler at a time, we need to make some modifications to our business processing methods, both logging and Performance Statistics. Generally, we can define a Handler interface and maintain a queue to store all Handler. When InvocationHandler is triggered, we call our Handler in turn. Fortunately, almost all AOP frameworks currently provide excellent support for this aspect.

Use Spring (http://www.springframework.org /).

I am a beginner in AOP. If you are not interested in this article, you are welcome to make a brick.

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.