Invocationhandler interface
public interface InvocationHandler{ public Object invoke(Object proxy,Method method,Object[] args)throws Throwable }
Parameter description:
Object proxy: The objects being proxied
Method: Methods to invoke
Object args[]: Parameters required for method invocation
proxy class
The proxy class is an action class that specifically completes the proxy, which can be used to dynamically generate an implementation class for one or more interfaces, which provides the following operations:
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException
Parameter description:
ClassLoader Loader: Class loader
Class<?>[] Interfaces: Get all the interfaces
Invocationhandler h: Get subclass instance of Invocationhandler interface
Dynamic Agent:
PackageCom.Itmyhome;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Proxy;InterfaceIhello{Defining interfacesPublicvoidSayHello();}ClassHelloimplImplementsIhello{@OverridePublicvoidSayHello(){TODO auto-generated Method StubSystem.Out.println("Hello Itmyhome");}}ClassMyinvocationhandlerImplementsInvocationhandler{PrivateObjectObj;Real Theme ClassPublicObjectBind(ObjectObj){This.Obj=Obj;ReturnProxy.Newproxyinstance(Obj.GetClass().getClassLoader(),Obj.GetClass().Getinterfaces(),This);}@OverridePublicObjectInvoke(ObjectProxy,MethodMethod,Object[]Args)ThrowsThrowable{System.Out.println("***before***");ObjectTemp=Method.Invoke(Obj,Args);System.Out.println("***after***");ReturnTemp;}}public class t {public static void main (string [] args) throws exception{ihello h = (ihello< Span class= "O" >) new myinvocationhandler (). (new helloimpl ()); h. (); } } /span>
Copyright NOTICE: This article uses the BY-NC-SA agreement to authorize, reproduced the Wheat Field Technical blog article please indicate the source
Original address: Http://itmyhome.com/2015/03/java-study-notes-dynamic-proxy
Java Learning Note 14--Dynamic Agent