类1:
package readxml;
public interface BusinessInterface {
public void processBusiness();
}
类2:
package readxml;
import java.util.logging.Logger;
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
// }
// }
private Logger logger = Logger.getLogger(this.getClass().getName());
public void processBusiness() {
// business processing
System.out.println("here is business logic");
}
}
类3:
package readxml;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.logging.Logger;
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...");
}
return o;
}
}
类4:
package readxml;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BusinessInterface businessImp = new BusinessObject();
InvocationHandler handler = new LogHandler(businessImp);
BusinessInterface proxy = (BusinessInterface) Proxy.newProxyInstance(businessImp.getClass().getClassLoader(), businessImp.getClass().getInterfaces(), handler);
proxy.processBusiness();
}
}
关于classloader: