IHello.java
package com.brj.springDemo;
public interface IHello {
public abstract void hello();
}
Hello.java
package com.brj.spring;
public class Hello implements IHello {
public void hello() {
System.out.println("Hello!");
}
}
LogHandler.java
package com.brj.spring;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class LogHandler implements InvocationHandler {
private Object target;
public Object bind(Object target) {
this.target = target;
return Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
this);
}
public Object invoke(Object arg0, Method arg1, Object[] arg2)
throws Throwable {
System.out.println("add log before...");
Object result = null;
result = arg1.invoke(target, arg2);
return result;
}
}
Speak.java
package com.brj.spring;
public class Speak {
public void speak(){
System.out.println("speak...");
}
}
Test.java
package com.brj.spring;
public class Test {
public static void main(String[] args) {
LogHandler logHandler = new LogHandler();
IHello proxy = (IHello) logHandler.bind(new Hello());
proxy.hello();
//Speak prox = (Speak) logHandler.bind(new Speak());
//prox.speak();
}
}