手写Spring源码-1.ApplicationContext
- 自定义Component注解
- 自定义ComponentScan注解
- 自定义Scope注解
- 自定义BeanDefinition类
- 自定义ApplicationContext类
1.自定义Component注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Component {
String value();
}
2.自定义ComponentScan注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ComponentScan {
String value();
}
3.自定义Scope注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Scope {
String value();
}
4.自定义BeanDefinition类
public class BeanDefinition {
private Class clazz;
private String scope;
public Class getClazz() {
return clazz;
}
public void setClazz(Class clazz) {
this.clazz = clazz;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
}
5.自定义ApplicationContext类
5.1流程图
![流程图](https://i-blog.csdnimg.cn/blog_migrate/890e6162cec21a5fd2443479c9320d9e.jpeg#pic_center)
5.2.代码
package com.spring;
import java.io.File;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class TanminApplicationContext {
private Class configClass;
private ConcurrentHashMap<String,Object> singletonObjects=new ConcurrentHashMap<>();
private ConcurrentHashMap<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>();
public TanminApplicationContext(Class configClass) throws ClassNotFoundException {
this.configClass = configClass;
scan(configClass);
for (Map.Entry<String,BeanDefinition> entry:beanDefinitionMap.entrySet()){
String beanName = entry.getKey();
BeanDefinition beanDefinition = entry.getValue();
if (beanDefinition.getScope().equals("singleton")){
Object bean = createBean(beanDefinition);
singletonObjects.put(beanName,bean);
}
}
}
public Object createBean(BeanDefinition beanDefinition){
Class clazz = beanDefinition.getClazz();
try {
Object instance = clazz.getDeclaredConstructor().newInstance();
return instance;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return null;
}
private void scan(Class configClass) throws ClassNotFoundException {
ComponentScan componentScanAnnotation = (ComponentScan) configClass.getDeclaredAnnotation(ComponentScan.class);
String path = componentScanAnnotation.value();
ClassLoader classLoader = TanminApplicationContext.class.getClassLoader();
URL resource = classLoader.getResource("com/tanmin/service");
File file = new File(resource.getFile());
if (file.isDirectory()){
File[] files = file.listFiles();
for (File f : files) {
String filename = f.getAbsolutePath();
if (filename.endsWith(".class")) {
String className = filename.substring(filename.indexOf("com"), filename.indexOf(".class"));
className = className.replace("\\", ".");
Class<?> clazz = classLoader.loadClass(className);
if (clazz.isAnnotationPresent(Component.class)){
BeanDefinition beanDefinition = new BeanDefinition();
beanDefinition.setClazz(clazz);
Component componentAnnotation = clazz.getDeclaredAnnotation(Component.class);
String beanName = componentAnnotation.value();
if (clazz.isAnnotationPresent(Scope.class)){
Scope scopeAnnotation = clazz.getDeclaredAnnotation(Scope.class);
beanDefinition.setScope(scopeAnnotation.value());
}else {
beanDefinition.setScope("singleton");
}
beanDefinitionMap.put(beanName,beanDefinition);
}
}
}
}
}
public Object getBean(String beanName) {
if (beanDefinitionMap.containsKey(beanName)){
BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
if(beanDefinition.getScope().equals("singleton")){
Object o = singletonObjects.get(beanName);
return o;
}else {
Object bean = createBean(beanDefinition);
return bean;
}
}else {
throw new NullPointerException();
}
}
}