Java代码
public interface IHello {
public void hello(String name);
}
Java代码
public class HelloImpl implements IHello{
@Override
public void hello(String name) {
Systemoutprintln(Hello: + name);
}
}
Java代码
package proxy;
import javalangreflectInvocationHandler;
import javalangreflectMethod;
import javalangreflectProxy;
import orgapachelogjLevel;
import orgapachelogjLogger;
public class LogHandler implements InvocationHandler {
private Logger logger = LoggergetLogger(thisgetClass()getName());
private Object delegate;
public Object bind(Object delegate) {
thisdelegate = delegate;
return ProxynewProxyInstance(delegategetClass()getClassLoader() delegategetClass()getInterfaces() this);
}
@Override
public Object invoke(Object proxy Method method Object[] args) throws Throwable {
Object result = null;
log(method starts + method);
result = methodinvoke(delegate args);
loggerlog(LevelINFOmethods ends + method);
return result;
}
private void log(String message) {
loggerlog(LevelINFOmessage);
}
public static void main(String[] args) {
LogHandler logHandler = new LogHandler();
IHello helloProxy = (IHello)logHandlerbind(new HelloImpl());
helloProxyhello(ssssssssssssss);
}
}