The proxy class for the JDK dynamic agent must be implemented on the interface. If you want to proxy the class, use the Cglib proxy.
Define an interface first:
Public Interface Character { publicvoid Show ();}
Next, define a class:
Public class Implements character{ @Override publicvoid Show () { // TODO auto-generated method stub System.out.println ("This is A"); } }
Define the proxy class to implement the Invocationhandler interface:
ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Proxy; Public classJdkproxyImplements invocationhandler{PrivateObject Delegate; Publicobject bind (object delegate) { This. Delegate =delegate;
//using reflection mechanism to generate a proxy object returnProxy.newproxyinstance (Delegate.getclass (). getClassLoader (), Delegate.getclass (). Getinterfaces (), This); } @Override Publicobject Invoke (Object proxy, Method method, object[] args)throwsThrowable {//TODO auto-generated Method StubObject result =NULL;
System.out.println ("Jdkproxy Invoke");
//Here is the method that called the target class, so you can insert other transaction logic before and after the method executes, and you can change the parameters passed in and the result returned =Method.invoke (delegate, args); returnresult; }}
Example:
Public Static void Main (string[] args) { = (Character) ((new jdkproxy ()). Bind (new A ())); C.show (); }
Printed results:
Jdkproxy Invoke This is A
JDK Dynamic Proxy Example