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 ...");
}
You mean you want something like this?
That would yield the following console log:
Of course you could print further information or differentiate overloaded methods with the same names by parameter types. Try things like
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.