[b][color=red]先上代码[/color][/b]
package com.dzeay.aop;
public interface IBusiness {
public void excute();
}
package com.dzeay.aop;
public class BusinessImpl implements IBusiness {
public void excute() {
System.out.println("BusinessImpl.excute");
}
}
package com.dzeay.aop;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class LogHandler implements InvocationHandler {
private Object delegate;
public LogHandler(Object delegate) {
this.delegate = delegate;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object obj = null;
try {
System.out.println(method + "\tbegin");
obj = method.invoke(delegate, args);
System.out.println(method + "\tend");
} catch (Exception e) {
}
return obj;
}
}
package com.dzeay.aop;
import java.lang.reflect.Proxy;
public class TestClass {
/**
* @param args
*/
public static void main(String[] args) {
IBusiness businessImp = new BusinessImpl();
IBusiness proxy = (IBusiness) Proxy.newProxyInstance(businessImp.getClass().getClassLoader(), businessImp.getClass().getInterfaces(), new LogHandler(businessImp));
proxy.excute();
}
}