package com.erik.spring;
import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.Enumeration;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class ErikApplicationContext {
private Class aClass;
private ConcurrentMap<String, BeanDeinition> concurrentMap = new ConcurrentHashMap<>();
private ConcurrentMap<String, Object> concurrentSingleMap = new ConcurrentHashMap<>();
public ErikApplicationContext(Class aClass) throws IOException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
this.aClass = aClass;
// 扫描
if (aClass.isAnnotationPresent(ComponentScan.class)) {
ClassLoader loader = aClass.getClassLoader();
ComponentScan annotation = (ComponentScan) aClass.getAnnotation(ComponentScan.class);
String value = annotation.value();
String replace = value.replace(".", "/");
URL resource = loader.getResource(replace);
File file = new File(resource.getFile());
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File file1 : files) {
if (file1.getName().endsWith(".class")) {
String s = file1.getName().replace(".class", "");
String newV = value + "." + s;
System.out.println(newV);
Class<?> loadClass = loader.loadClass(newV);
// 判断是否含有Component注解
if (loadClass.isAnnotationPresent(Component.class)) {
Component component = loadClass.getAnnotation(Component.class);
String value1 = component.value();
BeanDeinition deinition = new BeanDeinition();
String scope = "";
if (loadClass.isAnnotationPresent(Scope.class)) {
scope = loadClass.getAnnotation(Scope.class).value();
}else {
scope = "singleton";
}
deinition.setScope(scope);
deinition.setType(loadClass);
concurrentMap.put(value1, deinition);
}
}
}
}
}
// 获取所有的单例对象,封装起来
for (String s : concurrentMap.keySet()) {
BeanDeinition deinition = concurrentMap.get(s);
if (deinition.getScope().equals("singleton")) {
if (concurrentSingleMap.get(s) == null) {
Object bena = createBena(s, deinition);
concurrentSingleMap.put(s, bena);
}
}
}
}
private Object createBena(String beanName, BeanDeinition beanDeinition) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Class type = beanDeinition.getType();
return type.getConstructor().newInstance();
}
public Object getBean(String beanName) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
BeanDeinition o = concurrentMap.get(beanName);
if (o == null) {
throw new NullPointerException();
}
if (o.getScope().equals("singleton")) {
return concurrentSingleMap.get(beanName);
}
// 创建
return createBena(beanName, o);
}
}
源码解读
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 以太坊指令集与操作码解释器 EVM事实是个堆栈机器。指令可能会使用栈上的数值作为参数,也会将值作为结果压入栈中,而...
- @[toc] EventBus前言 本文主要讲解EventBus的源码解析,如果您未听过/使用过EventBus的...
- 1. kubelet代码入口 kubelet的main函数源码入口如下,代码路径为kubernetes/cmd/k...