当前位置:网站首页>Execute any method of any class through reflection
Execute any method of any class through reflection
2022-07-02 12:41:00 【Pastoral ing】
Execute any method of any class through reflection
Java The reflection mechanism of : When the program is running, you can call
Class aClass = Class.forName(className);
Object obj = aClass.newInstance(); Equireflective API Get the internal information of any class ( Field 、 Such method ), And can operate these internal information
We're calling Class.forName(className) When , Will load a Class Object of type , This object is passed in className, That is to say, we can pass this Class Object operation of type className Methods 、 Properties, etc .
1、 Project structure

2、 Two classes for demonstration
package com.zhou.reflect.pet;
/** * @author DELL * @version 1.0 * @Description * @date 2022/5/24 16:58 */
public class Cat {
public void sound(){
System.out.println(" cat ...");
}
}
package com.zhou.reflect.pet;
/** * @author DELL * @version 1.0 * @Description * @date 2022/5/24 16:54 */
public class Dog {
public void sound(){
System.out.println(" Wang Wang Wang ...");
}
}
3、properties
# Here, configure the class path to be called
className=com.zhou.reflect.pet.Cat
# Method to call
methodName=sound
4、 The main implementation
package com.zhou.reflect;
import org.apache.logging.log4j.util.PropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.lang.reflect.Method;
import java.util.Properties;
/** * @author DELL * @version 1.0 * @Description * @date 2022/5/24 17:05 */
public class ReflectFactory {
public static void main(String[] args) {
/* * demand : Do not modify the code of this class , Automatically call any method of any class * */
try {
//1、 obtain resource In the catalog properties
ClassPathResource classPathResource = new ClassPathResource("/application.properties");
Properties properties = PropertiesLoaderUtils.loadProperties(classPathResource);
//2、 Get the contents of the configuration file
String className = properties.getProperty("className");
String methodName = properties.getProperty("methodName");
//3、 Get the object of the class
Class aClass = Class.forName(className);
//4、 Initialize this object
Object obj = aClass.newInstance();
//5、 Get the method of the class
Method method = aClass.getMethod(methodName);
//6、 Execute this method
method.invoke(obj);
} catch (Exception ex) {
System.out.println(ex);
}
}
}
5、 Execution results
Calls configured in the configuration file Cat Of sound Method , The output is “ cat …”
Calls configured in the configuration file dog Of sound Method , The output is “ Wang Wang Wang …”
边栏推荐
- 8 examples of using date commands
- 应用LNK306GN-TL 转换器、非隔离电源
- Dijkstra AcWing 850. Dijkstra求最短路 II
- 浏览器存储方案
- Distributed machine learning framework and high-dimensional real-time recommendation system
- Drools executes string rules or executes a rule file
- Visual studio efficient and practical extension tools and plug-ins
- arcgis js 4. Add pictures to x map
- Redis avalanche, penetration, breakdown
- "As a junior college student, I found out how difficult it is to counter attack after graduation."
猜你喜欢

Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately

Record the range of data that MySQL update will lock

arcgis js 4. Add pictures to x map

浏览器存储方案

Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)

染色法判定二分图 AcWing 860. 染色法判定二分图

线性DP AcWing 895. 最长上升子序列

线性DP AcWing 902. 最短编辑距离

Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime

深拷貝 事件總線
随机推荐
ASP. Net MVC default configuration, if any, jumps to the corresponding program in the specified area
Sweetheart leader: Wang Xinling
包管理工具
模块化 CommonJS ES Module
应用LNK306GN-TL 转换器、非隔离电源
Adding database driver to sqoop of cdh6
浏览器node事件循环
Floyd AcWing 854. Floyd求最短路
JS8day(滚动事件(scroll家族),offset家族,client家族,轮播图案例(待做))
高性能纠删码编码
China traffic sign detection data set
Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
线性DP AcWing 899. 编辑距离
Anti shake throttle
单指令多数据SIMD的SSE/AVX指令集和API
中国交通标志检测数据集
区间DP AcWing 282. 石子合并
Go学习笔记—基于Go的进程间通信
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
LeetCode—剑指 Offer 37、38