手写Spring框架源码
立即下载
资源介绍:
支持高质量中文博客,希望下载者一键三连,支持知识免费分享,你的支持是我的动力!
package org.cheney.summer;
import org.cheney.summer.annotation.Autowired;
import org.cheney.summer.annotation.Component;
import org.cheney.summer.annotation.ComponentScan;
import org.cheney.summer.annotation.Scope;
import java.beans.Introspector;
import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
/**
* Hello world!
*/
public class SummerApplicationContext {
private Class configClass;
private ConcurrentHashMap beanDefinitionMap =
new ConcurrentHashMap<>();
private ConcurrentHashMap singletonBeanMap =
new ConcurrentHashMap<>();
private ArrayList beanPostProcessorList =
new ArrayList<>();
public SummerApplicationContext(Class configClass) {
this.configClass = configClass;
if (configClass.isAnnotationPresent(ComponentScan.class)) {
ComponentScan scanAnnotation = (ComponentScan) configClass.getAnnotation(ComponentScan.class);
String pathValue = scanAnnotation.value(); // org.cheney.service
String[] paths = pathValue.split(",");
for (String path : paths) {
path = path.replace(".", "/");
ClassLoader classLoader = SummerApplicationContext.class.getClassLoader();
URL resource = classLoader.getResource(path);
File file = new File(resource.getFile());
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
String absolutePath = f.getAbsolutePath();
if (absolutePath.endsWith(".class")) {
// 判断目录下的.class文件是否有 @Component注解
String className = absolutePath.substring(absolutePath.indexOf("org"), absolutePath.indexOf(".class"));
className = className.replace("\\", ".");
// System.out.println(className);
try {
Class> clazz = classLoader.loadClass(className);
if (clazz.isAnnotationPresent(Component.class)) {
// 扫描到了 @Component 注解,生成一个 BeanDefinition 对象
BeanDefinition beanDefinition = new BeanDefinition();
beanDefinition.setType(clazz);
if (clazz.isAnnotationPresent(Scope.class)) {
String value = clazz.getAnnotation(Scope.class).value();
beanDefinition.setScope(value);
} else {
beanDefinition.setScope("singleton");
}
String beanName = clazz.getAnnotation(Component.class).value();
if ("".equals(beanName)) {
// beanName = getBeanName(clazz);
beanName = Introspector.decapitalize(clazz.getSimpleName());
}
beanDefinitionMap.put(beanName, beanDefinition);
// 前置 后置 处理器
if (BeanPostProcessor.class.isAssignableFrom(clazz)) {
BeanPostProcessor instance = (BeanPostProcessor) clazz.newInstance();
beanPostProcessorList.add(instance);
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
}
}
}
// 将beanDefinition中的Singleton进行实例化
for (String beanName : beanDefinitionMap.keySet()) {
BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
if ("singleton".equals(beanDefinition.getScope())) {
Object bean = createBean(beanName, beanDefinition);
singletonBeanMap.put(beanName, bean);
}
}
}
private Object createBean(String beanName, BeanDefinition beanDefinition) {
Class clazz = beanDefinition.getType();
try {
Object bean = clazz.getConstructor().newInstance();
// 实现依赖注入
Field[] fields = clazz.getDeclaredFields();
for (Field f : fields) {
if (f.isAnnotationPresent(Autowired.class)) {
f.setAccessible(true);
f.set(bean, getBean(f.getName()));
}
}
// Aware 回调
if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(beanName);
}
// BeanPostProcessor 初始化前,前置处理器
for (BeanPostProcessor processor : beanPostProcessorList) {
processor.postProcessorBeforeInitialization(beanName,bean);
}
// 初始化
if (bean instanceof InitializingBean) {
((InitializingBean) bean).afterPropertiesSet();
}
// BeanPostProcessor 初始化后,后置处理器
for (BeanPostProcessor processor : beanPostProcessorList) {
processor.postProcessorAfterInitialization(beanName,bean);
}
return bean;
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
public Object getBean(String beanName) {
BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
if (null == beanDefinition) {
throw new NullPointerException("Summer容器中不存在叫做" + beanName + "的Bean");
}
if (beanDefinition.getScope().equals("singleton")) {
Object bean = singletonBeanMap.get(beanName);
if (null == bean) {
bean = createBean(beanName, beanDefinition);
singletonBeanMap.put(beanName, bean);
}
return bean;
} else {
return createBean(beanName, beanDefinition);
}
}
}
资源文件列表:
summer.zip 大约有150个文件