我想问一下, 这个用Proxy, InvocationHandle来处理, 有什么好处么?
谢谢了@
我想问一下, 这个用Proxy, InvocationHandle来处理, 有什么好处么?
谢谢了@
谢了!
interface Simple {
void sayHello();
}
class SimpleImpl implements Simple {
public void sayHello() {
System.out.println("Hello");
}
}
class AOPHandler implements InvocationHandler {
public static String SAYHELLO_METHOD_NAME = "sayHello";
Simple simple = new SimpleImpl();
public static Simple getSimple() {
return (Simple)Proxy.newProxyInstance(SimpleImpl.class.getClassLoader(),
SimpleImpl.class.getInterfaces(),new AOPHandler());
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{
Object obj = null;
//if (SAYHELLO_METHOD_NAME.equals(method.getName())) {
System.out.println("before say hello method");
//} else {
obj = method.invoke(simple, args);
//}
return obj;
}
}
public class ProxyTest {
public static void main(String[] args) {
Simple simple = AOPHandler.getSimple();
simple.sayHello();
}
}
public class ProxyTest {
public static void main(String[] args) {
Simple simple = (Simple)Proxy.newProxyInstance(SimpleImpl.class.getClassLoader(),
SimpleImpl.class.getInterfaces(),new AOPHandler());
simple.sayHello();
}