当前位置:网站首页>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 …”
边栏推荐
- 1380. Lucky numbers in the matrix [two-dimensional array, matrix]
- 线性DP AcWing 896. 最长上升子序列 II
- When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
- Typora+docsify quick start
- Shuttle encapsulated AppBar
- ASP. Net MVC default configuration, if any, jumps to the corresponding program in the specified area
- Go learning notes - multithreading
- Rust search server, rust quick service finding tutorial
- 深拷貝 事件總線
- Find the common ancestor of any two numbers in a binary tree
猜你喜欢

What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT

Bom Dom

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

LTC3307AHV 符合EMI标准,降压转换器 QCA7005-AL33 PHY

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

JS10day(api 阶段性完结,正则表达式简介,自定义属性,过滤敏感词案例,注册模块验证案例)

VLAN experiment

There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes

BOM DOM

Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
随机推荐
包管理工具
Input box assembly of the shutter package
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
Go学习笔记—基于Go的进程间通信
Adding database driver to sqoop of cdh6
基于STM32的OLED 屏幕驱动
深拷貝 事件總線
Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
async/await 异步函数
百款拿来就能用的网页特效,不来看看吗?
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
Typora+docsify quick start
Is the neural network (pinn) with embedded physical knowledge a pit?
Leetcode - Sword finger offer 59 - I, 59 - II
Lekao: 22 year first-class fire engineer "technical practice" knowledge points
Simple understanding of ThreadLocal
Dijkstra AcWing 850. Dijkstra求最短路 II
堆 AcWing 839. 模拟堆
Mui WebView down refresh pull-up load implementation