Use the Java proxy mechanism for log output

Source: Internet
Author: User

Spring's AOP is based on Java's dynamic proxy. Learning the Java Dynamic proxy mechanism helps you gradually understand the idea of AOP.

The traditional log output method is to write the log recording code in each business logic method. In this way, there will be many similar log output codes in the program, resulting in great coupling. So what method can be used to separate the business logic from the code of the output log and reuse the log information code? The proxy mechanism of Java can improve this problem.

First, write a log information proxy class, which implements the interface invocationhandler, then write an interface and implement this interface, and write specific logic code in the implementation class, finally, write a test class for the interface to view the test results. The procedure is as follows:

(1) Compile a log information proxy class logproxy, which implements the interface invocationhandler and can output logs to any interface. The logproxy. Java code is as follows:

View code

/**
*
*/
Package com.jp. Action;

Import java. Lang. Reflect. invocationhandler;
Import java. Lang. Reflect. method;
Import java. Lang. Reflect. proxy;

Import org. Apache. log4j. level;
Import org. Apache. log4j. Logger;

/**
* @ Author JP
*
*/
// The proxy class implements the invocationhandler interface.
Public class logproxy implements invocationhandler {

/*
* (Non-javadoc)
*
* @ See java. Lang. Reflect. invocationhandler # invoke (Java. Lang. object,
* Java. Lang. Reflect. method, java. Lang. object [])
*/
Private logger = logger. getlogger (this. getclass (). getname ());
Private object delegate;

// Bind a proxy object
Public object BIND (Object delegate ){
This. Delegate = delegate;
Return proxy. newproxyinstance (delegate. getclass (). getclassloader (),
Delegate. getclass (). getinterfaces (), this );
}

// Programming for interfaces
@ Override
Public object invoke (Object proxy, method, object [] ARGs)
Throws throwable {
// Todo auto-generated method stub
Object result = NULL;
Try {
Logger. Log (level. info, argS [0] + "start to review data ...");
Result = method. Invoke (delegate, argS );
Logger. Log (level. info, argS [0] + "End of data review ");
} Catch (exception e ){
// Todo auto-generated Catch Block
Logger. Log (level. info, E. tostring ());
}

Return result;
}

}

(2) define an interface: timebookinterface. Java

View code

package com.jp.impl;

/**
* @author JP
*
*/
public interface TimeBookInterface {
public void doAuditing(String name);

}

(3) The timebook class implements the preceding interface. The doauditing () method is specific service logic processing.

View code

package com.jp.action;

import org.apache.log4j.Logger;

import com.jp.impl.TimeBookInterface;

/**
* @author JP
*
*/
public class TimeBook implements TimeBookInterface{
private Logger logger=Logger.getLogger(this.getClass().getName());

@Override
public void doAuditing(String name){
System.out.println(name+" is doing something about auditing...");
}

}

(4) Compile the test program testhelloworld. Java and use the log proxy logproxy to output logs. The testhelloworld. Java code is as follows:

View code

Package com.jp. test;
Import com.jp. Action. logproxy;
Import com.jp. Action. timebook;
Import com.jp. impl. timebookinterface;

Public class testhelloworld {

Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Logproxy = new logproxy ();
Timebookinterface timebookproxy = (timebookinterface) logproxy. BIND (New timebook ());
Timebookproxy. doauditing ("James ");
}
}

(5) run the test program to obtain the log information output through the logproxy class.

This method is also applicable to other classes, which truly achieves the separation of business logic and output log information code.

 

 

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.