gitlub地址:仓库地址spring源码
spring boot基于spring的底层去实现,在springboot进行启动时候手写启动类和tomcat,加载启动类的注解进行扫描加载全部的bean。讲数据发送给tomcat后,tomcat才能将接口的信息进行展示出来。这边就不手写了。
package com.trench.springUtil;
import com.trench.interfa.*;
import com.trench.service.BeanNameAware;
import com.trench.service.BeanPostProcessor;
import com.trench.service.InitializingBean;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
public class TrenchApplicationContest {
private Class configClass;
private ConcurrentHashMap<String,Object> concurrentHashMap=new ConcurrentHashMap<>();
private ConcurrentHashMap<String,BeanDefintion> beanDefintionConcurrentHashMap=new ConcurrentHashMap<>();
private List<BeanPostProcessor> beanPostProcessorList=new ArrayList<>();
public TrenchApplicationContest(Class configClass) {
this.configClass = configClass;
scan(configClass);
preInstantita();
}
private void preInstantita() {
for (Map.Entry<String,BeanDefintion> entry :beanDefintionConcurrentHashMap.entrySet()){
String beanName=entry.getKey();
BeanDefintion beanDefintion=entry.getValue();
if (beanDefintion.getScope().equals("singleton")){
Object bean = saveBean(beanName,beanDefintion);
concurrentHashMap.put(beanName,bean);
}
}
}
//创建bean
public Object saveBean(String beanName,BeanDefintion beanDefintion) {
Class aClass = beanDefintion.getaClass();
AtomicReference<Object> instance = new AtomicReference<>();
try {
instance.set(aClass.getDeclaredConstructor().newInstance());
//依赖注入
insteadBean(aClass, instance);
beanInstance(beanName, instance);
return instance.get();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e){
e.printStackTrace();
}catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private void beanInstance(String beanName, AtomicReference<Object> instance) throws Exception {
。。。。。。。。。。核心代码请到gitlub查看
});
}
private void insteadBean(Class aClass, AtomicReference<Object> instance) throws IllegalAccessException {
for (Field declareField: aClass.getDeclaredFields()){
if (declareField.isAnnotationPresent(Autowired.class)||
declareField.isAnnotationPresent(Resource.class)){
。。。。。。。。。。核心代码请到gitlub查看
declareField.setAccessible(true);
declareField.set(instance.get(),bean);
}
}
}
private void scan(Class configClass) {
//解析配置类
ComponenSan componenSan=(ComponenSan) configClass.getDeclaredAnnotation(ComponenSan.class);
//路径
String path=componenSan.value();
。。。。。。。。。。核心代码请到gitlub查看
}
private void classNameAndPost(ClassLoader classLoader, File[] files) {
for (File fi: files){
try {
String absolutePath = fi.getAbsolutePath();
if (absolutePath.endsWith(".class")) {
。。。。。。。。。。核心代码请到gitlub查看
}
} catch (ClassNotFoundException | NoSuchMethodException classNotFoundException) {
classNotFoundException.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
private void isAnnotation(Class<?> aClass) {
if (aClass.isAnnotationPresent(Component.class)) {
//生成bean,解析bean判断是单一还是prototype
Component annotation = aClass.getDeclaredAnnotation(Component.class);
。。。。。。。。。。核心代码请到gitlub查看
beanDefintionConcurrentHashMap.put(beanName,beanDefintion);
}
}
public Object getBean(String beanName){
if (beanDefintionConcurrentHashMap.containsKey(beanName)){
。。。。。。。。。。核心代码请到gitlub查看
}else {
throw new RuntimeException("不存在bean");
}
}
}
package com.trench.impl;
import com.trench.service.BeanPostProcessor;
import com.trench.interfa.Component;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
@Component
public class BeanPostProcessorServer implements BeanPostProcessor {
@Override
public Object postProcessorBeforeInitialization(Object bean, String beanName) {
return bean;
}
@Override
public Object postProcessorAfterInitialization(Object bean, String beanName) {
if (beanName!=null) {
return Proxy.newProxyInstance(BeanPostProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(bean, args);
}
});
}
return bean;
}
}
package com.trench.interfa;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.FIELD})
public @interface Autowired {
}
package com.trench.interfa;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ComponenSan {
String value() default "";
}
package com.trench.interfa;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.FIELD})
public @interface Resource {
}
package com.trench;
import com.trench.impl.AppConfig;
import com.trench.springUtil.TrenchApplicationContest;
public class Application {
public static void main(String[] args) {
TrenchApplicationContest contest=new TrenchApplicationContest(AppConfig.class);
System.out.println(contest.getBean("userServer"));
}
}