package com.lcs.spring.service;
import com.lcs.spring.annotation.Autowired;
import com.lcs.spring.annotation.Component;
import com.lcs.spring.annotation.ComponentScan;
import com.lcs.spring.annotation.Scope;
import java.beans.Introspector;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author zhongliudizhu
*/
public class LcsApplicationContext {
private Class configClass;
private final Map<String, BeanDefination> beanDelineationMap = new ConcurrentHashMap<>();
private final Map<String, Object> singletonsObjects = new ConcurrentHashMap<>();
private final List<BeanPostProcessor> beanDefinitionProcessor = new LinkedList<>();
public LcsApplicationContext(Class configClass) {
this.configClass = configClass;
scan(configClass);
for (Map.Entry<String, BeanDefination> entity : beanDelineationMap.entrySet()) {
String beanName = entity.getKey();
BeanDefination beanDefination = entity.getValue();
// 实例化普通bean
Object object = createBean(beanName, beanDefination);
singletonsObjects.put(beanName, object);
}
}
private Object createBean(String beanName, BeanDefination beanDefination) {
Class clazz = beanDefination.getType();
Object instance = null;
try {
instance = clazz.getConstructor().newInstance();
for (Field field : clazz.getDeclaredFields()) {
if (field.isAnnotationPresent(Autowired.class)) {
field.setAccessible(true);
field.set(instance, getBean(field.getName()));
}
}
// 初始化
if (instance instanceof InitializingBean) {
((InitializingBean) instance).afterPropertiesSet();
}
for (BeanPostProcessor beanPostProcessor : beanDefinitionProcessor) {
instance = beanPostProcessor.postProcessAfterInitialization(instance, beanName);
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return instance;
}
public Object getBean(String beanName) {
if (!beanDelineationMap.containsKey(beanName)) {
throw new NullPointerException();
}
BeanDefination beanDefination = beanDelineationMap.get(beanName);
if (!"singleton".equals(beanDefination.getScope())) {
return createBean(beanName, beanDefination);
} else {
return singletonsObjects.get(beanName);
}
}
private void scan(Class configClass) {
if (configClass.isAnnotationPresent(ComponentScan.class)) {
ComponentScan componentScan = (ComponentScan) configClass.getAnnotation(ComponentScan.class);
String path = componentScan.value().replace(".", "/");
ClassLoader classLoader = LcsApplicationContext.class.getClassLoader();
URL path1 = classLoader.getResource(path);
File classPath = null;
try {
classPath = new File(path1.toURI());
if (classPath.isDirectory()) {
for (File file1 : classPath.listFiles()) {
String absolutePath = file1.getAbsoluteFile().getPath();
String classInfo = absolutePath.substring(absolutePath.lastIndexOf("com"), absolutePath.lastIndexOf(".class"));
classInfo = classInfo.replace(File.separatorChar, '.');
Class clazz = classLoader.loadClass(classInfo);
BeanDefination beanDefination = new BeanDefination();
beanDefination.setType(clazz);
if (clazz.isAnnotationPresent(Component.class)) {
if (BeanPostProcessor.class.isAssignableFrom(clazz)) {
BeanPostProcessor instance = (BeanPostProcessor) clazz.getConstructor().newInstance();
beanDefinitionProcessor.add(instance);
}
Component componentAnnotaion = (Component) clazz.getAnnotation(Component.class);
String beanName = componentAnnotaion.value();
if ("".equals(beanName)) {
// 起到的是转换作用
beanName = Introspector.decapitalize(clazz.getSimpleName());
}
if (clazz.isAnnotationPresent(Scope.class)) {
Scope scope = (Scope) clazz.getAnnotation(Scope.class);
String value = scope.value();
beanDefination.setScope(value);
} else {
beanDefination.setScope("singleton");
}
beanDelineationMap.put(beanName, beanDefination);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
评论0