package org.example;
import org.example.Annotations.Autowired;
import org.example.Annotations.Component;
import org.example.Annotations.ComponentScan;
import org.example.Annotations.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
import javax.swing.plaf.IconUIResource;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
public class AnnotationApplicationContext implements ApplicationContext {
//定义Map,用于存放bean对象
private Map<String, Object> beanFactory = new HashMap<>();
//创建IOC容器类时执行的方法
public AnnotationApplicationContext(Class configClass) throws InstantiationException, IllegalAccessException {
//处理@Configuration的逻辑(没有实质性的内容,只是要求必须有配置类)
try {
//获取@Configuration类型的对象
Configuration configuration = (Configuration) configClass.getAnnotation(Configuration.class);
//如果对象为空,则引发异常
if (configuration.annotationType().toGenericString().equals("")) {
//没有具体逻辑
}
} catch (Exception e) {
System.out.println("找不到配置类");
System.exit(-1);
}
//处理@ComponentScan的逻辑
/*
总的来说,就是扫描某一路径下的所有类,如果有的类被Component注解,则创建其实例(一个bean),加入beanFactory中
如果有的类中的对象(一个Field类型的数据)被@Autowired注解标注,则用beanFactory中同类型的bean替代该对象
*/
try {
ComponentScan componentScan = (ComponentScan) configClass.getAnnotation(ComponentScan.class);
//根据componentScanValue的值来获取要扫描的路径
String componentScanValue = componentScan.value().toString();
String path = "";
if (!componentScanValue.equals(""))
{
String[] splitValue = componentScanValue.split("\\.");
for (int i = 0; i < splitValue.length; i++) {
path += splitValue[i] + "/";
}
path = "classpath*:" + path + "**/*.class";
}
else {
path = "classpath*:org/example/**/*.class";
}
//扫描路径,获取所有的class
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources(path);
for (Resource res : resources) {
//获取类名
String clsName = new SimpleMetadataReaderFactory().getMetadataReader(res).getClassMetadata().getClassName();
//获取类对象
Class thisClass = Class.forName(clsName);
//判断当前类是否被Component注解标注
Component component = (Component) thisClass.getAnnotation(Component.class);
if (component != null) //说明该类被Component注解标注
{
Constructor c = thisClass.getConstructor();
Object o = c.newInstance(); //创建该类的实例
beanFactory.put(o.getClass().getSimpleName(), o);//加入beanFactory中
}
//处理@Autowired注解
for (Object bean : beanFactory.values()) { //查看beanFactory中所有的bean
Class beanClass = bean.getClass();
Field[] beanFields = beanClass.getDeclaredFields(); //获取bean的fields
for (Field field : beanFields) {
if (field.isAnnotationPresent(Autowired.class)) { //如果被Autowired注解标注
Object beanD = beanFactory.get(field.getType().getSimpleName()); //获取beanFactory中的bean
field.setAccessible(true); //关闭安全检查
field.set(bean, beanD); //用beanFactory中的bean来代替当前bean
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("没有ComponentScan注解");
System.exit(-1);
}
// Field[] configFields = configClass.getDeclaredFields();
// //处理标注为Bean的逻辑 @Component注解处理
// for (Field field : configFields) {
// if (field.isAnnotationPresent(Component.class))
// {
// Object beanComponent = field.getType().newInstance();
// beanFactory.put(field.getType().getSimpleName(),beanComponent);
// }
// }
// //处理自动注入的逻辑 @Autowired处理
// for (Object bean:beanFactory.values())
// {
// Class beanClass = bean.getClass();
// Field[] beanFields = beanClass.getDeclaredFields();
// for (Field field : beanFields)
// {
// if (field.isAnnotationPresent(Autowired.class))
// {
// Object beanD = beanFactory.get(field.getType().getSimpleName());
// field.setAccessible(true);
// field.set(bean,beanD);
// }
// }
// }
}
//getBean方法,获取某个bean(通过class)
@Override
public Object getBean(Class beanClass) {
return beanFactory.get(beanClass.getSimpleName());
}
}

THEWHY
- 粉丝: 5305
最新资源
- 基于STM32F407芯片和OV7725摄像头的实时图像处理系统-实现视频流图像二值化-颜色识别-九宫格坐标定位-目标跟踪-计算机视觉应用-嵌入式开发-使用RGB转HSV色彩空间转.zip
- 基于PaddlePaddle框架的MNIST手写数字识别系统-深度学习-卷积神经网络-图像分类-数据增强-模型优化-可视化分析-北京交通大学人工智能课程大作业-支持CPU和GPU训.zip
- 深入理解计算机系统第二版中文学习笔记与代码实践-计算机系统原理-信息表示处理-程序机器级表示-处理器体系结构-程序性能优化-存储器层次结构-链接机制-异常控制流-虚拟内存-系统级I.zip
- collie库使用,图片像素识别
- (源码)基于Vue和Webpack的模板项目.zip
- Java技术书籍资源汇总项目-包含Java核心技术卷并发编程网络编程架构设计性能优化等领域的经典书籍-为Java开发者提供便捷的电子书下载服务-涵盖Java基础多线程N.zip
- (源码)基于ESP32的远程控制机器人项目.zip
- (源码)基于Arduino的LTECATM1NBIoT温度上传系统.zip
- (源码)基于Android的宠物应用.zip
- Python 离线orc图像内容识别
- (源码)基于Arduino的ESP32 Rick Roll广播系统.zip
- (源码)基于Arduino的无线电台系统.zip
- (源码)基于PIC18LF26K80微控制器的BMW CAN模块模拟系统.zip
- (源码)基于NS3框架的SlicedLoRaWAN模块.zip
- 基于Python开发的智能人脸识别与图像捕捉系统-使用OpenCV实现实时摄像头监控-自动检测并识别画面中的人脸特征-支持多角度人脸捕捉和识别-可应用于门禁系统考勤管理安防监控等场.zip
- (源码)基于Spring Boot框架的通用前端权限管理系统.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


