当前位置:网站首页>Implementing a Simple Framework for Managing Object Information Using Reflection
Implementing a Simple Framework for Managing Object Information Using Reflection
2022-07-31 19:56:00 【Xiaoyu love of programming】
Implement a managed object using the reflection information of simple framework
看实现的效果:
And then we run the code,It can according to our configuration file statement instances of classes and properties to create the corresponding
Change the configuration file information:
对应的结果:
实现步骤:
首先通过Class类的**getResourceAsStream()**Methods to obtain a file input stream
Properties properties = new Properties();
InputStream resourceAsStream = MySpring.class.getResourceAsStream(PROPRETIES_PATH);
首先通过PropertiesThe information in the class to load the file——包含类的全限定名、属性值
properties.load(resourceAsStream);
String className = properties.getProperty("className");
Set<Object> keySet = properties.keySet();
Use reflection to load the file specified class,With instance
Class<?> loadClass = Class.forName(className);
Object o = loadClass.newInstance();
By reflecting access to all the properties in the class,并进行赋值
Field[] fields = loadClass.getDeclaredFields();
Arrays.stream(fields).filter(field -> keySet.contains(field.getName())).forEach(field -> {
field.setAccessible(true);
try {
field.set(o, properties.getProperty(field.getName()));
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
});
代码整合
我的Person类
package com.zky.pojo;
/** * @authoer:zky * @createDate:2022/7/30 * @description: */
public class Person {
private String name;
private String age;
private String address;
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", address='" + address + '\'' +
'}';
}
}
我的School类
package com.zky.pojo;
/** * @authoer:zky * @createDate:2022/7/30 * @description: */
public class School {
private String name;
private String level;
@Override
public String toString() {
return "School{" +
"name='" + name + '\'' +
", level='" + level + '\'' +
'}';
}
}
实现类
package com.zky.myframe;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Predicate;
/** * @authoer:zky * @createDate:2022/7/30 * @description: */
public class MySpring {
public static final String PROPRETIES_PATH = "application.propreties";
public static void main(String[] args) throws Exception {
Object o = beanFactory();
System.out.println(o);
}
public static Object beanFactory() throws Exception {
//读取配置文件的信息
Properties properties = new Properties();
InputStream resourceAsStream = MySpring.class.getResourceAsStream(PROPRETIES_PATH);
properties.load(resourceAsStream);
String className = properties.getProperty("className");
//TODO Modified to automatically retrieve attributes information
Set<Object> keySet = properties.keySet();
//Using the reflection to load the corresponding class
Class<?> loadClass = Class.forName(className);
Object o = loadClass.newInstance();
Field[] fields = loadClass.getDeclaredFields();
Arrays.stream(fields).filter(field -> keySet.contains(field.getName())).forEach(field -> {
field.setAccessible(true);
try {
field.set(o, properties.getProperty(field.getName()));
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
});
return o;
}
}
待优化:
Currently attribute support onlyString类型的属性,Other properties such as I have time to write~ 感觉不是很好弄.
Now can only manage a object,Multiple objects to be true
边栏推荐
猜你喜欢
财务盈利、偿债能力指标
The whole network is on the verge of triggering, and the all-round assistant for content distribution from media people - Rongmeibao
Realization of character makeup
Short-circuit characteristics and protection of SiC MOSFETs
统计UTF-8字符串中的字符函数
MySQL---Subqueries
高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询
C# 之 扑克游戏 -- 21点规则介绍和代码实现
Three. Introduction to js
1161. Maximum Sum of Elements in Layer: Hierarchical Traversal Application Problems
随机推荐
spark报错OutOfMemory「建议收藏」
BM5 merge k sorted linked lists
mysql的备份表的几种方法
The whole network is on the verge of triggering, and the all-round assistant for content distribution from media people - Rongmeibao
How to change npm to Taobao mirror [easy to understand]
[PIMF] OpenHarmony Thesis Club - Inventory of the open source Hongmeng tripartite library [3]
Architecture Battalion Module 8 Homework
The principle of ReentrantLock (to be continued)
Architect 04 - Application Service Encryption Design and Practice
Go record - slice
【AcWing】The 62nd Weekly Match 【2022.07.30】
-xms -xmx(information value)
获取抖音视频详情 API
每月一书(202207):《Swift编程权威指南》
rj45对接头千兆(百兆以太网接口定义)
Realization of character makeup
关注!海泰方圆加入《个人信息保护自律公约》
中文编码的设置与action方法的返回值
1161. 最大层内元素和 : 层序遍历运用题
返回一个零长度的数组或者空的集合,不要返回null