当前位置:网站首页>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 …”
边栏推荐
- About the loading of layer web spring layer components, the position of the layer is centered
- JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
- Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
- Deep copy event bus
- Leetcode - Sword finger offer 37, 38
- BOM DOM
- 线性DP AcWing 896. 最长上升子序列 II
- MySQL and PostgreSQL methods to grab slow SQL
- JS6day(DOM结点的查找、增加、删除。实例化时间,时间戳,时间戳的案例,重绘和回流)
- Performance tuning project case
猜你喜欢

kubenetes中port、targetPort、nodePort、containerPort的区别与联系

arcgis js 4. Add pictures to x map

VLAN experiment

Deep Copy Event bus

Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?

JS10day(api 阶段性完结,正则表达式简介,自定义属性,过滤敏感词案例,注册模块验证案例)
![[ybtoj advanced training guidance] judgment overflow [error]](/img/be/bbe357ac2f2a8839afc5af47db88d0.jpg)
[ybtoj advanced training guidance] judgment overflow [error]

模块化 CommonJS ES Module

线性DP AcWing 897. 最长公共子序列

Anti shake throttle
随机推荐
基于STM32的OLED 屏幕驱动
Redis avalanche, penetration, breakdown
3 A VTT端接 稳压器 NCP51200MNTXG资料
考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60
Use MySQL events to regularly perform post seven world line tasks
The second composition template of postgraduate entrance examination English / chart composition, English chart composition is enough
Input box assembly of the shutter package
Go learning notes - multithreading
1380. Lucky numbers in the matrix [two-dimensional array, matrix]
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
. Net, C # basic knowledge
绕过ObRegisterCallbacks需要驱动签名方法
传感器 ADXL335BCPZ-RL7 3轴 加速度计 符合 RoHS/WEEE
计数类DP AcWing 900. 整数划分
哈希表 AcWing 841. 字符串哈希
C#运算符
线性DP AcWing 897. 最长公共子序列
LeetCode—<动态规划专项>剑指 Offer 19、49、60
Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime