Java Learning Note 14--Dynamic Agent

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.