// 加载class文件为对象
Class<?> clazz = classLoader.loadClass(absolutePath);
// 判断是否有Component注解
if (clazz.isAnnotationPresent(Component.class)) {
// Component注解的value: beanName
Component component = clazz.getAnnotation(Component.class);
String beanName = component.value();
// 存入类型
BeanDefinition beanDefinition = new BeanDefinition();
beanDefinition.setType(clazz); // 这里原来想当然写成了clazz.class 所以后面获取默认的无参构造方法时一直报错 NoSuchMethodException
// 判断是否有scope注解
if (clazz.isAnnotationPresent(Scope.class)) {
// 获取注解内容
Scope scopeAnnotation = clazz.getAnnotation(Scope.class);
beanDefinition.setScope(scopeAnnotation.value());
}else{
beanDefinition.setScope("singleton");
}
beanDefinitionMap.put(beanName ,beanDefinition );
}
记录手写Spring扫描时的踩一个小坑
最新推荐文章于 2024-06-12 09:38:27 发布