A little understanding of java reflection technology:

Example reference:

http://how2j.cn/k/reflection/reflection-usage/1111.html#nowhere

concept:

关键词:运行状态;(和编译没有关系)

The JAVA reflection mechanism is that in the running state, for any class, you can know all the properties and methods of the class; for any object, you can call any of its methods and properties; this dynamically obtained information and dynamic call The function of the method of the object is called the reflection mechanism of the java language.

关键词:解剖

To dissect a class, you must first obtain the bytecode file object of the class. The anatomy uses the methods in the Class class. Therefore, the object of the Class type corresponding to each bytecode file must be obtained first.

对象是确实存在的事物;
类:是用来描述对象的。

The class name .class file obtained by reflection technology, this file is an object, and the class name used to describe the object is Class

Scenario:
Why do you need to use reflection technology, or what are the benefits of using reflection technology?
There are three applications: Test.java
Service1.java
Service2.java

1. Write a Java application, such as Test.java; call the method in service1.java at the beginning of the project,

public class Test {
  public static void main(String[] args) {
     new Service1().doService1();
  }
}

Later, the project requirements changed, and new functions need to be added. One method is to modify directly in the Test code and add new code. This operation is not very good, that is, changing the source code is a relatively hurt method. If the later code It changes again, and the source code needs to be changed. To change the source code, two things need to be done. The first step is to understand the previous source code, and the second step is to add functions; it is better to re-write a piece of code, and then use it again;
then we re-write a piece of code , named service2.java; at
this time, if Test.java wants to use service2.java, you must first comment out the calling code of service1, and then modify the code:

public class Test {
    public static void main(String[] args) {
//      new Service1().doService1();
        new Service2().doService2();
    }
}

But as mentioned earlier, this processing still changes the source code of Test.java. Although the new function is encapsulated into service2.java, it still does not change the actual need to add new functions without changing the source code;
this time reflection comes into play.

2. To use the reflection method, first prepare a configuration file, called spring.txt, and put it in the src directory. It stores the name of the class and the name of the method to be called.
In the test class Test, first take out the class name and method name, and then call the method through reflection.

When you need to switch from calling the first business method to calling the second business method, you do not need to modify a line of code or recompile. You only need to modify the configuration file spring.txt and run it again.

This is also the most basic principle of the Spring framework, but it is richer, safer and more robust.
Spring.txt

class=reflection.Service1
method=doService1

Test.java

package reflection;

import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Properties;

public class Test {

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static void main(String[] args) throws Exception {

        //从spring.txt中获取类名称和方法名称
        File springConfigFile = new File("e:\\project\\j2se\\src\\spring.txt");
        Properties springConfig= new Properties();
        springConfig.load(new FileInputStream(springConfigFile));
        String className = (String) springConfig.get("class");
        String methodName = (String) springConfig.get("method");

        //根据类名称获取类对象
        Class clazz = Class.forName(className);
        //根据方法名称,获取方法对象
        Method m = clazz.getMethod(methodName);
        //获取构造器
        Constructor c = clazz.getConstructor();
        //根据构造器,实例化出对象
        Object service = c.newInstance();
        //调用对象的指定方法
        m.invoke(service);

    }
}

Advantages: In this way, we have achieved the purpose of using new functions without changing the source code of Test.java. We only need to change the configuration file, which is still acceptable.

This is where reflection technology is used. Java dissects the code of service1.java and service2.java. Test and java find reflection, and reflection finds the corresponding dissected service1.java and service2.Java, and finds the corresponding code according to the configuration file. , after finding it, the reflection anatomy takes out the method name of the corresponding class.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326700907&siteId=291194637