Java Dynamic Proxy Implementation aop_jsp programming

Source: Internet
Author: User
The current development Community's oriented of AOP (Aspect-programing) has also spawned a large number of excellent frameworks that support AOP,--Spring, JAC, Jboss AOP, and so on. AOP seems to have become a fad. Java beginners can not help but to make sense, OOP has not learned, but also to AOP. This article is not to elaborate on the theory of what AOP, why AOP. To learn more about AOP you can go to his hometown http://aosd.net to see. This is just an attempt to show beginners how to do AOP with a simple example.

For simplicity, the example does not use any third party AOP Framework, but rather takes advantage of the dynamic proxy capabilities that the Java language itself brings to the implementation of AOP.

Let's go back to AOP itself, where AOP is mainly used for logging, performance statistics, security control, transaction processing, and more. Its main intention is to log records, performance statistics, security control, and so on, the code is clearly divided into business logic code, and we can think of these behaviors as a separate problem for the system, which is called problem-oriented programming (whether AOP translation is not a problem oriented programming). By separating these behaviors, we want to be able to configure them independently into business methods, and changing them does not have to affect the business method code.

Assuming that the system completes the business logic function by a series of businessobject, the system requires a log record for each business logic processing. 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 here.
System.out.println ("Here is business logic");
Logger.info ("End processing ...");
catch (Exception e) {
Logger.info ("Exception happends ...");
Exception handling
}
}
}

The combination of code and logging code that deals with business logic makes it difficult for future maintenance, and can also cause a lot of code duplication. The exact same log code will appear in every businessobject of the system.

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.