I am having trouble in using jdk dynamic proxy

359 views Asked by At

Before using spring aop and cglib, Now I replaced a simple example.I found that executing method sayHello1 () and sayHello2 () both output "before" and "after" Oh my god, it's very difficult, Do you understand what I am talking about? I am going crazy now. T.T

public interface HelloWorld {
    void sayHello1(String say);
    void sayHello2(String say);
}
public static class HelloWorldImpl implements HelloWorld {
    @Override
    public void sayHello1(String say) { System.out.println(say); }
    @Override
    public void sayHello2(String say) { System.out.println(say); }
}
public static class Invocation implements InvocationHandler {
    private final Object target;

    public Invocation(Object target) { this.target = target; }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("before..."); // TODO method before
        Object object = method.invoke(target, args);
        System.out.println("after..."); // TODO method after
        return object;
    }
}
public static void main(String[] args) {
    HelloWorld helloWorld = (HelloWorld) Proxy.newProxyInstance(
        ClassLoader.getSystemClassLoader(),
        new Class[] { HelloWorld.class },
        new Invocation(new HelloWorldImpl())
    );
    helloWorld.sayHello1("Hello World1 ...");
    helloWorld.sayHello2("Hello World2 ...");
}
1

There are 1 answers

0
kriegaex On BEST ANSWER

You mean you want something like this?

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  System.out.println("Instrumenting " + method);
  Object result;
  switch (method.getName()) {
    case "sayHello1":
      System.out.println("before A");
      result = method.invoke(target, args);
      System.out.println("after A");
      break;
    case "sayHello2":
      System.out.println("before B");
      // Let's change the argument just for fun
      args[0] = "changed argument";
      result = method.invoke(target, args);
      System.out.println("after B");
      break;
    default:
      result = method.invoke(target, args);
  }
  return result;
}

That would yield the following console log:

Instrumenting public abstract void de.scrum_master.spring.q62001911.HelloWorld.sayHello1(java.lang.String)
before A
Hello World1 ...
after A
Instrumenting public abstract void de.scrum_master.spring.q62001911.HelloWorld.sayHello2(java.lang.String)
before B
changed argument
after B

Of course you could print further information or differentiate overloaded methods with the same names by parameter types. Try things like

method.getParameterTypes();
method.getParameterCount();
method.getReturnType();

Is this tedious? Yeah, it is, but still straightforward. And it being tedious is the reason why AspectJ or Spring AOP with their elegant pointcut + advice model are so much easier to use because they did the work already and hide the inner complexity from you.