参考源

https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click

https://www.bilibili.com/video/BV12Z4y197MU?spm_id_from=333.999.0.0

《Spring源码深度解析(第2版)》

版本

本文章基于 Spring 5.3.15


项目地址

https://gitee.com/liao-hang/hand-write-spring.git

模拟 Spring

注解

自动装配

Autowired

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Autowired { }

组件

Component

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Component { String value() default ""; }

组件扫描

ComponentScan

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ComponentScan { String value() default ""; }

范围

Scope

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Scope { String value() default ""; }

Bean 定义信息

BeanDefinition

public class BeanDefinition {

    /**
* 类型
*/
private Class type; /**
* 范围
*/
private String scope; public Class getType() {
return type;
} public void setType(Class type) {
this.type = type;
} public String getScope() {
return scope;
} public void setScope(String scope) {
this.scope = scope;
}
}

Bean 名称感知

BeanNameAware

public interface BeanNameAware {

    /**
* 设置 Bean 名称
* @param beanName Bean名称
*/
void setBeanName(String beanName); }

Bean 后置处理器

BeanPostProcessor

public interface BeanPostProcessor {

    /**
* 初始化前
* @param beanName Bean名称
* @param bean Bean对象
* @return
*/
Object postProcessBeforeInitialization(String beanName, Object bean); /**
* 初始化后
* @param beanName Bean名称
* @param bean Bean对象
* @return
*/
Object postProcessAfterInitialization(String beanName, Object bean); }

初始化 Bean

InitializationBean

public interface InitializationBean {

    /**
* 属性设置后
*/
void afterPropertiesSet(); }

注解配置应用上下文

AnnotationConfigApplicationContext

public class AnnotationConfigApplicationContext {

    /**
* 配置的类
*/
private Class configClass; /**
* Bean 后置处理器列表
*/
private List<BeanPostProcessor> beanPostProcessorList = new ArrayList<>(); /**
* Bean 定义信息池
*/
private ConcurrentHashMap<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(); /**
* 单例池
*/
private ConcurrentHashMap<String, Object> singletonObjects = new ConcurrentHashMap<>(); public AnnotationConfigApplicationContext(Class configClass) {
this.configClass = configClass; /*
扫描 -> BeanDefinition -> beanDefinitionMap
*/
// 是否有 @ComponentScan 注解
if (configClass.isAnnotationPresent(ComponentScan.class)) {
ComponentScan componentScan = (ComponentScan) configClass.getAnnotation(ComponentScan.class);
// 注解中的路径
String path = componentScan.value();
// 处理路径
path = path.replace(".", "/");
// 类加载器
ClassLoader classLoader = AnnotationConfigApplicationContext.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 fileName = f.getAbsolutePath();
// 是否为 class 文件
if (fileName.endsWith(".class")) {
// 类名 cn\sail\test\文件名
String className = fileName.substring(fileName.indexOf("cn"), fileName.indexOf(".class"));
// 处理路径
className = className.replace("\\", ".");
try {
Class<?> clazz = classLoader.loadClass(className);
// 是否有 @Component 注解
if (clazz.isAnnotationPresent(Component.class)) {
// 接口和类是否相同或者是否为其超类或超接口
if (BeanPostProcessor.class.isAssignableFrom(clazz)) {
BeanPostProcessor instance = (BeanPostProcessor) clazz.newInstance();
beanPostProcessorList.add(instance);
}
Component component = clazz.getAnnotation(Component.class);
String beanName = component.value();
if ("".equals(beanName)) {
beanName = Introspector.decapitalize(clazz.getSimpleName());
}
BeanDefinition beanDefinition = new BeanDefinition();
beanDefinition.setType(clazz);
// 是否有 @Scope 注解
if (clazz.isAnnotationPresent(Scope.class)) {
// 如果有,以传入的模式为准
Scope scope = clazz.getAnnotation(Scope.class);
beanDefinition.setScope(scope.value());
} else {
// 如果没有,默认为单例模式
beanDefinition.setScope("singleton");
}
beanDefinitionMap.put(beanName, beanDefinition);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
} // 实例化单例 Bean
for (String beanName : beanDefinitionMap.keySet()) {
BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
// 如果为单例模式,将 BeanDefinition 放入单例池中,下次直接从池中取,以保证单例
if ("singleton".equals(beanDefinition.getScope())) {
Object bean = createBean(beanName, beanDefinition);
singletonObjects.put(beanName, bean);
}
}
} /**
* 创建 Bean
* @param beanName Bean 名称
* @param beanDefinition Bean 定义信息
* @return Bean 对象
*/
private Object createBean(String beanName, BeanDefinition beanDefinition) {
Class clazz = beanDefinition.getType();
try {
Object instance = clazz.getConstructor().newInstance();
// 依赖注入
for (Field f : clazz.getDeclaredFields()) {
// 是否有 @Autowired 注解
if (f.isAnnotationPresent(Autowired.class)) {
// 忽略访问修饰符限制
f.setAccessible(true);
// 注入
f.set(instance, getBean(f.getName()));
}
}
// Aware
if (instance instanceof BeanNameAware) {
((BeanNameAware) instance).setBeanName(beanName);
}
// 初始化前
for (BeanPostProcessor beanPostProcessor : beanPostProcessorList) {
instance = beanPostProcessor.postProcessBeforeInitialization(beanName, instance);
}
// 初始化
if (instance instanceof InitializationBean) {
((InitializationBean) instance).afterPropertiesSet();
}
// 初始化后
for (BeanPostProcessor beanPostProcessor : beanPostProcessorList) {
instance = beanPostProcessor.postProcessAfterInitialization(beanName, instance);
}
return instance;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return null;
} public Object getBean(String beanName) {
BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
if (beanDefinition == null) {
throw new NullPointerException();
} else {
String scope = beanDefinition.getScope();
if ("singleton".equals(scope)) {
Object bean = singletonObjects.get(beanName);
// 如果单例池中没有对应 Bean,创建 Bean 后再放入池中
if (bean == null) {
bean = createBean(beanName, beanDefinition);
singletonObjects.put(beanName, bean);
}
return bean;
} else {
// 如果不是单例模式,直接创建 Bean
return createBean(beanName, beanDefinition);
}
}
} }

使用 Spring

应用配置

AppConfig

@ComponentScan("cn.sail.test")
public class AppConfig { }

用户服务

UserInterface

public interface UserInterface {

    /**
* 测试
*/
void test(); }

用户服务实现类

UserService

@Component
public class UserService implements UserInterface{ @Autowired
private OrderService orderService; @Override
public void test() {
System.out.println(orderService);
} }

排序服务

OrderService

@Component
public class OrderService { }

Bean 后置处理器

MyBeanPostProcessor

@Component
public class MyBeanPostProcessor implements BeanPostProcessor { /**
* 初始化前
*
* @param beanName
* @param bean
* @return
*/
@Override
public Object postProcessBeforeInitialization(String beanName, Object bean) {
if ("userService".equals(beanName)) {
System.out.println("1111");
}
return bean;
} /**
* 初始化后
*
* @param beanName
* @param bean
* @return
*/
@Override
public Object postProcessAfterInitialization(String beanName, Object bean) {
if ("userService".equals(beanName)) {
Object proxyInstance = Proxy.newProxyInstance(MyBeanPostProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("切面逻辑");
return method.invoke(bean, args);
}
});
return proxyInstance;
}
return bean;
}
}

测试

Test

public class Test {

    public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
UserInterface userService = (UserInterface) applicationContext.getBean("userService");
userService.test();
} }

Spring源码 20 手写模拟源码的更多相关文章

  1. 手写koa-static源码,深入理解静态服务器原理

    这篇文章继续前面的Koa源码系列,这个系列已经有两篇文章了: 第一篇讲解了Koa的核心架构和源码:手写Koa.js源码 第二篇讲解了@koa/router的架构和源码:手写@koa/router源码 ...

  2. 【Spring系列】- 手写模拟Spring框架

    简单模拟Spring 生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! 前言 上次已经学习了 ...

  3. 《四 spring源码》手写springioc框架

    手写SpringIOCXML版本 /** * 手写Spring专题 XML方式注入bean * * * */ public class ClassPathXmlApplicationContext { ...

  4. 《四 spring源码》手写springmvc

    手写SpringMVC思路 1.web.xml加载  为了读取web.xml中的配置,我们用到ServletConfig这个类,它代表当前Servlet在web.xml中的配置信息.通过web.xml ...

  5. 面试必会之ArrayList源码分析&手写ArrayList

    简介 ArrayList是我们开发中非常常用的数据存储容器之一,其底层是数组实现的,我们可以在集合中存储任意类型的数据,ArrayList是线程不安全的,非常适合用于对元素进行查找,效率非常高. 线程 ...

  6. 手写Redux-Saga源码

    上一篇文章我们分析了Redux-Thunk的源码,可以看到他的代码非常简单,只是让dispatch可以处理函数类型的action,其作者也承认对于复杂场景,Redux-Thunk并不适用,还推荐了Re ...

  7. Spring事务原理分析--手写Spring事务

    一.基本概念和原理 1.Spring事务 基于AOP环绕通知和异常通知的 2.Spring事务分为编程式事务.声明事务.编程事务包括注解方式和扫包方式(xml) Spring事务底层使用编程事务(自己 ...

  8. Spring源码分析 手写简单IOC容器

    Spring的两大特性就是IOC和AOP. IOC Container,控制反转容器,通过读取配置文件或注解,将对象封装成Bean存入IOC容器待用,程序需要时再从容器中取,实现控制权由程序员向程序的 ...

  9. 源码分析 | 手写mybait-spring核心功能(干货好文一次学会工厂bean、类代理、bean注册的使用)

    作者:小傅哥 博客:https://bugstack.cn - 汇总系列原创专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言介绍 一个知识点的学习过程基本分为:运行helloworld ...

随机推荐

  1. 第06组Alpha冲刺 (4/6)

    目录 1.1 基本情况 1.2 冲刺概况汇报 1.郝雷明 2.曹兰英 3. 方梓涵 4.鲍凌函 5.董翔云 6.杜筱 7.黄少丹 8.曾丽莉 9. 詹鑫冰 10.吴沅静 1.3 冲刺成果展示 1.1 ...

  2. 爷青回,canal 1.1.6来了,几个重要特性和bug修复

    刚刚在群里看到消息说,时隔一年,canal 1.1.6正式release了,赶紧上去看看有什么新特性. (居然才发布了6个小时,前排围观) 1.什么是canal canal [kə'næl],译意为水 ...

  3. SAM[详细~bushi]

    基础性质概念 后缀自动机:S的SAM是个DAG,每个节点叫状态,每条带字符ch边表示+ch转移,从开始节点往下,任何一条路径都会对应一个S的子串. 不过为什么要叫"后缀"自动机呢? ...

  4. 解惑unittest框架中导入HTMLTestRunner模块后正常运行却无法生成HTML报告问题

    1.HTMLTestRunner介绍 HTMLTestRunner是一个第三方的unittest HTML报告库,用于python单元测试框架的TestRunner.它是生成一个HTML报告,以一目了 ...

  5. 『忘了再学』Shell基础 — 28、AWK中条件表达式说明

    目录 1.AWK的条件表达 2.条件表达式说明 (1)BEGIN (2)END (3)关系运算符 (4)说明AWK中条件表达式的执行过程 (5)AWK中使用正则表达式 (6)A~B练习 1.AWK的条 ...

  6. 怎么理解相互独立事件?真的是没有任何关系的事件吗?《考研概率论学习之我见》 -by zobol

    1.从条件概率的定义来看独立事件的定义 2.从古典概率的定义来看独立事件的定义 3.P(A|B)和P(A)的关系是什么? 4.由P(AB)=P(A)P(B)推出"独立" 5.从韦恩 ...

  7. C语言学习之我见-strcat()字符拼接函数(有缺陷)

    strcat()函数,用于两个字符串的拼接. (1)函数原型: char * strcat(char *Dest,const char * Source); (2)头文件: #include < ...

  8. vue封装手机验证码

    // 获取验证码 let endMsRes = new Date().getTime() + 45000; localStorage.setItem("myEndTime", JS ...

  9. WPF开发随笔收录-自定义图标控件

    一.前言 1.在以前自学的过程中,软件需要使用到图标的时候,总是第一个想法是下载一个图片来充当图标使用,但实际得出来的效果会出现模糊的现象.后来网上学习了字体图标库的用法,可以在阿里云矢量图网站那里将 ...

  10. Python快速下载商品数据,并连接数据库,保存数据

    开发环境 python 3.8 pycharm 2021.2 专业版 代码实现 发送请求 获取数据 解析数据(筛选数据) 保存数据 连接数据库 开始代码 请求数据 # 伪装 headers = { ' ...