[Dynamic page] (2) Java reflection, dynamic page java reflection
The reflection mechanism of Java is an important feature of the Java language. First, let's take a look at how the official website defines the reflection mechanism.
The translation may not be very accurate. Reflection is a feature of the Java programming language. It requires Java to check or self-check when running programs and perform operations on internal attributes. For example, a Java class can obtain and display all its member names. This kind of ability to check and operate from a Java class may not sound very popular, but it does not exist in other programming languages. For example, there is no way for a program pascia, C, or C # to obtain its defined functions. Another real example of reflection is that in JavaBeans, software components can be operated through constructors. This constructor uses reflection to obtain Java attributes (classes) because they are dynamically loaded.
In other words, to understand the reflection mechanism of Java, it means that in the running state, any class can dynamically obtain the attributes and methods under its class, any method that can be called for any object. The function of dynamically obtaining information and calling methods of objects is called the reflection mechanism of Java. This mechanism is unique to the Java language and is not available in other programming languages.
Currently, many open-source frameworks use reflection mechanisms, such as Hibernate and Struts. But why do we need a reflection mechanism? Think about how we implemented it in the past, and determine a type during compilation. For example, a class or an object is completely written, fixed and written, and then instantiate the object or call a method, this method is called static compilation. So what corresponds to it is dynamic compilation, that is, the Java reflection described in this blog. Its implementation principle is to determine the type at runtime, and then bind the object. This kind of dynamic compilation features greatly exert the powerful functions of the Java language, enhance Java flexibility, and reflect the multi-state application.
If you still cannot understand the above, you can use another method. Java reflection refers to the ability to get its own information when the program is running. in Java, if the class name is given, all the information of the class can be obtained through the reflection mechanism. Think about the reflection mechanism we use in OA. Because we are not sure which object is passed in, whether it is Role, Apartment, or User, we will use the Java reflection mechanism Class <T>, review the current situation. We have three tables, Role, Apartment, and User. Each table has four operations: add, delete, modify, and query. When we didn't understand the Java reflection mechanism, you may have to create different Dao classes based on different tables. For example, we have to create three Dao classes, which repeat the same method in a large number, except that the class names are different.
Now, with the reflection mechanism of Java, we have to jump out of the old circle. Think about how smart humans are, and how to develop tools. Think about how to improve reusability and efficiency. So now we use the reflection mechanism of Java to create only one Dao class, and then write the add, delete, modify, and query method, but leave the type empty, at that time, you only need to input different types of objects and create Dao classes without many tables. The reflection mechanism will automatically help us do this. Just like there is a wine cellar with three wine tanks containing blue wine, green wine and yellow wine. The wine cellar owner puts a wine tank at the door every day to sell wine at the door. The previous practice was: for example, if we sell blue wine today, we will move the wine cylinder dressed in blue wine to the door; when we buy green wine, we will move the wine cylinder of green wine to the door. Later, a guy thought of a way to put an empty cylinder at the door, and an anhe leader was on the cylinder. Today, he just needs to pay attention to the wine cylinder at the door, in this way, you do not need to move the cylinder back and forth. Oh, how stupid the boss is. Of course, this story was fabricated by me. I just want to understand the reflection mechanism of Java.
So in retrospect, the reflection mechanism of Java can achieve dynamic object creation and compilation. For better understanding, let's create an example. Now I am a class teacher. I used to allocate jobs like this. Xiaohong is responsible for sweeping the floor, Xiaolan is responsible for glass, xiaohei is responsible for cleaning the blackboard, xiaohuang is responsible for garbage removal, and everyone is only responsible for their own work, when I see the ground, I have to remember to arrange the red to sweep the floor ......
However, the allocation efficiency is very low, because every time I want to arrange a job, I have to think about which fixed person to arrange to do it. So with the Java reflection mechanism, I changed the way I used to schedule work.
In this way, using the reflection mechanism of Java, you do not know which object you want to create to execute the corresponding work. Now, using the reflection mechanism of Java, during the process of running the program, the system dynamically creates an object and calls any of its methods and attributes to perform its functions. I don't know. Do you understand the Java reflection mechanism? In the next article, I will introduce how to read the class name and attributes of the Jar package through the Java reflection mechanism, so stay tuned!