16th article-java class loading and reflection
-
each sentence : Dare to Bend, is to stand more firmly
-
Beginner's experience : The pursuit of the far-away laughter, the pursuit of far-away companions
-
( Author: jeep/711) [Java Notes | time: 2017-05-12| JAVA class loading and reflection]
1. Class loading
-
The ClassLoader is responsible for loading the. class file (possibly on disk, possibly on the network) into memory and generating the corresponding Java.lang.Class object for it.
-
When the program actively uses a class, if the class has not been loaded into memory, the system will be loaded, connected, initialized three steps to initialize the class, if there is no accident, the JVM will complete the three steps, so sometimes the three steps are collectively referred to as class loading or class initialization.
-
Class loading refers to reading class files into memory and creating a Java.lang.Class object for them, which means that when a program uses any class, the system creates a Java.lang.Class object for it.
2. Source of class data
By using different ClassLoader, you can load the binary data of a class from different sources, usually with the following sources:
-
Loading the class file from the local file system is the way that most of the previous sample programs are loaded.
-
It is also common to load a class file from a jar package, and the database-driven class used in the previous JDBC programming is placed in the jar file, and the JVM can load the class file directly from the jar file
-
The class file is loaded over the network
-
Dynamically compiles a Java source file and executes the load
3. Connection of Classes
When the class is loaded, the system generates a corresponding class object, which is then entered into the connection phase, and the connection phase is responsible for merging the class's binary data into the JRE
Class connection can be divided into the following three stages:
-
Validation: The validation phase is used to verify that the class being loaded has the correct internal structure and is consistent with other classes
-
Preparation: The class preparation phase is responsible for allocating memory for static properties of the class and setting default initial values
-
Parse: Replace a symbolic reference in the binary data of a class with a direct reference
4. Class Loader
The ClassLoader is responsible for loading the. class file (possibly on disk, possibly on the network) into memory and generating the corresponding Java.lang.Class object for it.
When the JVM starts, it forms an initial class loader hierarchy consisting of three classloader:
-
Bootstrap ClassLoader: Root class loader.
-
Extension ClassLoader: Extension class loader.
-
System ClassLoader: Class Loader
5. What is Reflection
Reflection is the mapping of the various components in a Java class into a Java object. For example, a class has information such as member variables, methods, construction methods, packages, and so on, using reflection technology to dissect a class and map each component to a single object.
6.Class class
Class classes are used to represent. class files
How to get the class object corresponding to a class file
1. Class name. class,2. Object. GetClass () 3.class.forname ("Class name")
Class instance object of array type
Class.isarray ()
7.Constructor class
An instance object of the constructor class represents a construction method of a class
Get all the constructor methods for a class, for example:
Constructor [] constructors= class.forname ("java.lang.String"). GetConstructors ();
To get a construction method, for example:
Constructor Constructor = Class.forName ("java.lang.String"). GetConstructor (Stringbuffer.class);
To create an instance object using a construction method:
String str = (string) constructor.newinstance ("abc");
The class Newinstance () method can also create instances of classes that work internally without a parameter, and then create an instance object with a construction method.
String obj = (string) class.forname ("java.lang.String"). newinstance ();
8. Introspection (Introspector)
Two ways to access the JavaBean property
-
Call the Bean's Setxxx or GetXXX method directly
-
Through introspective technology access (the Java.beans package provides an introspective API), introspective technical access also provides two ways
-
Manipulating bean properties through the PropertyDescriptor class
-
The BeanInfo of the Bean object is obtained through the Introspector class, and the descriptor (PropertyDescriptor) of the property is obtained through BeanInfo, and the property descriptor is used to obtain the getter/setter of a property. method, and then call these methods through the reflection mechanism
Introspection-beanutils Toolkit
Apache has developed a set of APIs for manipulating JavaBean, which takes into account a lot of real-world development scenarios, so many programmers use this API to manipulate JavaBean in real-world development to simplify program code writing
Common classes for the Beanutils Toolkit:
9. Reflection Cases
1.package com.mvc.bean;2.import com.mvc.service.imp.userserviceimp;3.import Java.util.resourcebundle;4./**5. * created by jeep-711 6. */7.public class BeanFactory {8. private static ResourceBundle bundle;9. static {10. bundle = Resourcebundle.getbundle ("instance"); //file Contents 11. }12. public static <t> t getinstance (String key, class<t> clazz) {13. try {14. string classname = bundle.getstring (key);//class full path 15. return (T) class.forname (className). newinstance ();//Reflection Mode 16 . } catch (exception e) {17. throw new runtimeexception (e); 18. } 19. }20.}
1.package com.mvc.controller;2.import com.alibaba.fastjson.json;3.import Com.mvc.bean.beanfactory;4.import com.mvc.bean.type;5.import com.mvc.service.itypeservice;6.import com.mvc.service.imp.TypeServiceImpl;7.import org.apache.commons.beanutils.BeanUtils;8.import javax.servlet.servletexception;9.import javax.servlet.http.httpservlet;10.import javax.servlet.http.httpservletrequest;11.import javax.servlet.http.httpservletresponse;12.import java.io.ioexception;13.import java.io.writer;14.import java.util.list;15./**16. * created by jeep-711 17. */18.public class typeservlet extends httpservlet{19. //Reflex 20. Itypeservice itypeservice = beanfactory.getinstance ("Typeservice", ITypeService.class);21. @Override22. protected void doget (httpservletrequest REQ,&NBSP;HTTPSERVLETRESPONSE&NBSP;RESP) throws ServletException, IOException {23. resp.setcontenttype ("Text/html;charset=utf-8");//Set UTF-8 encoding format 24. req.setcharacterencoding ("UTF-8");//Set UTF-8 encoding format 25. string method = req.getparameter ("M");26. if ("Add". Equals (method)) {27. add (REQ,RESP);//Call the Add method 28. }else if ("Update". Equals (method)) {29. update ( REQ,RESP);//Call the Modify method 30. }else if ("Ajax_all". Equals (method)) {31. ajaxall (REQ,RESP);32. }else if ("delete". Equals (method)) {33. delete (REQ,RESP); 34. }else {35. index (REQ,RESP);//Call error method 36. }37. }38.}Beginner (JAVA class loading and reflection advanced stage) Difficulty: ★★★★★ hope that each article can provide readers with help and promotion, which is the original intention of every author Thank you for your reading welcome your message and suggestions
-
Facebook:jeep SevenEleven
-
Twitter: @JEEP7ll
-
Sina official Weibo: @JEEP-711
-
GitHub Blog: Https://github.com/jeep711/jeep711.github.io
-
Blog garden:http://www.cnblogs.com/jeep711/
-
w3c/blog:http://www.w3cschool.cn/jeep711blog/
-
csdn/blog:http://blog.csdn.net/jeep911
-
51cto/blog:http://jeep711.blog.51cto.com/
-
Code Cloud: Http://git.oschina.net/JEEP711/jeep711.github.io
-
Email: [email protected],[email protected]
-
This article is from the "JEEP711" blog, make sure to keep this source http://jeep711.blog.51cto.com/12970787/1955819
16th-java class loading and reflection