当前位置:网站首页>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
看实现的效果:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pF8lOM4l-1659182788166)(D:\note\笔记仓库\图片\image-20220730195726510.png)]](/img/55/2599a43429251b9f96097ec2be1679.png)
And then we run the code,It can according to our configuration file statement instances of classes and properties to create the corresponding
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1dGinWU0-1659182788175)(D:\note\笔记仓库\图片\image-20220730195744424.png)]](/img/0e/bbb2d001e8166551c939bba113abb1.png)
Change the configuration file information:![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qmaBKpw9-1659182788176)(D:\note\笔记仓库\图片\image-20220730200147432.png)]](/img/32/a53f95f839f07551814212c7782932.png)
对应的结果:
实现步骤:
首先通过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
边栏推荐
猜你喜欢

财务盈利、偿债能力指标

Made with Flutter and Firebase!counter application

How programmers learn open source projects, this article tells you

顺序表的实现

Chinese encoding Settings and action methods return values

35 MySQL interview questions and diagrams, this is also easy to understand

MySQL - single function
![[PIMF] OpenHarmony Thesis Club - Inventory of the open source Hongmeng tripartite library [3]](/img/8c/22e083d2a9a4a6f983b985fe454893.png)
[PIMF] OpenHarmony Thesis Club - Inventory of the open source Hongmeng tripartite library [3]

Getting Started with Tkinter

1161. Maximum Sum of Elements in Layer: Hierarchical Traversal Application Problems
随机推荐
ResNet的基础:残差块的原理
leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]
请问我的这段sql中sql语法哪里出了错
MySQL - multi-table query
架构实战营模块八作业
京东获取商品历史价格信息 API
mysql的备份表的几种方法
性能优化:记一次树的搜索接口优化思路
【愚公系列】2022年07月 Go教学课程 025-递归函数
Architect 04 - Application Service Encryption Design and Practice
嵌入式开发没有激情了,正常吗?
JD.com searches for products by keyword API
Write a database document management tool based on WPF repeating the wheel (1)
【Yugong Series】July 2022 Go Teaching Course 023-List of Go Containers
求n以内的素数
&lt;artifactId&gt;ojdbc8&lt;/artifactId&gt;「建议收藏」
rj45 to the connector Gigabit (Fast Ethernet interface definition)
MySQL---排序与分页
Bika LIMS 开源LIMS集—— SENAITE的使用(检测流程)
Bika LIMS open source LIMS set - use of SENAITE (detection process)