Java: Dynamic Proxy

Source: Internet
Author: User

Reprinted from Blog: http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html

To obtain the class loader:

1  Public Static void Main (string[] args) {2         Person p1=New person (); 3         System.out.println (P1.getclass (). getClassLoader (). GetClass (). GetName ()); 4     }

Output:

Sun.misc.launcher$appclassloader

In fact, there are three kinds of class loaders in Java.

1) Bootstrap ClassLoader This loader is written in C + + and is rare in general development.

2) Extension ClassLoader is used to load extended classes, generally corresponding to classes in the Jre\lib\ext directory

3) Appclassloader load Classpath The specified class, which is the most commonly used loader. It is also the default loader in Java.

If you want to complete a dynamic proxy, you first need to define a subclass of the Invocationhandler interface, and the agent has completed the specific operation.

Interface:

1  Public Interface Subject {2     String  say (string string,int age ); 3 }

Implementation class:

1  Public classRealsubjectImplementsSubject {2 3 @Override4      PublicString say (String string,intAge ) {5         //TODO auto-generated Method Stub6System.out.println ("" + String +Age );7         return"Well,well,well,well";8     }9 Ten}

Proxy class:

1  Public classMyinvocationhandlerImplementsInvocationhandler {2     PrivateObject obj;3 4      Publicobject bind (Object obj) {5          This. obj =obj;6         returnproxy.newproxyinstance (Obj.getclass (). getClassLoader (), obj7. GetClass (). Getinterfaces (), This);8     }9 Ten @Override One      Publicobject Invoke (Object proxy, Method method, object[] args) A             throwsThrowable { -         //TODO auto-generated Method Stub -Object temp = Method.invoke ( This. obj, args); the         returntemp; -     } -  -}

Test:

1  Public Static void Main (string[] args) {2         New Myinvocationhandler (); 3         Subject sub= (Subject) handler.bind (new  realsubject ()); 4         String Info=sub.say ("CC", +); 5         System.out.println (info); 6     }

Output:

CC21
Well,well,well,well

Life cycle of a class

after a class has been compiled, the next step is to start using the class, and if you want to use a class, you can't do without JVM . In program execution , the JVM is completed by loading, linking, and initializing these 3 steps.

The loading of the class is done through the ClassLoader, and the loader will . Class Binary file loading of files JVM and create a description of the class in the heap area . Java.lang.Class object. Used to encapsulate data. but the same class will only be loaded by the class loader before

A link is the ability to assemble binary data into a functioning state.

links are divided into calibration, preparation, parsing this 3 Stages of

a checksum is used to verify that the binary is suitable for the current JVM (version),

Preparation is the allocation of memory space for static members. and set the default value

Parsing refers to the process of converting code in a constant pool as a direct reference, until all symbolic references can be used by the running program (establishing a complete correspondence relationship)

After completion, the type is initialized, and after initialization the object of the class can be used normally until an object is no longer in use and will be garbage collected. Free up space.

when no reference is directed to the Class object is unloaded to end the life cycle of the class.

Java: Dynamic Proxy

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.