在文章开始之前,先来看一张spring IOC加载过程的脑图吧
Spring IOC的加载过程
首先,当我们去new了一个applicationContext,它底层呢就会把我们配置的bean进行扫描,然后创建成一个一个的beanDefinition放在我们的beanDefinitionMap中,此时就有了一切创造bean的原料信息,然后就会去循环beanDefinition,去调用beanfactory.getBean方法,先尝试在一级缓存中获取,获取不到呢就会进行创建,先进行实例化,然后进行依赖注入,最后初始化,放入到一级缓存中.
手写源码
package cn.edu.hunau;
import cn.edu.hunau.service.AService;
import cn.edu.hunau.service.BService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @Author SuJ
* @Date 2024 04 12 15 13.
* 手写spring IOC底层源码来模拟spring如何利用多级缓存解决循环依赖的问题。
**/
public class SuJApplicationContext {
private Map<String, BeanDefinition> beanDefinitionMap = new LinkedHashMap<>();
// 一级缓存 单例池
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>();
public SuJApplicationContext() throws Exception{
// 加载ioc容器 创建所有的bean
refersh();
finishBeanFactoryInitialization();
}
//一个个的创建bean
private void finishBeanFactoryInitialization() {
//循环所有的beanDefinition
beanDefinitionMap.keySet().forEach(
beanName -> {
try {
getBean(beanName);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
);
}
private Object getBean(String beanName) throws InstantiationException, IllegalAccessException {
// 1.尝试在一级缓存中获取
Object bean = getSingleton(beanName);
//如果存在 直接放回
if(bean != null){
return bean;
}
// 2.创建 ---> 实例化
RootBeanDefinition beanDefinition = (RootBeanDefinition)beanDefinitionMap.get(beanName);
Class<?> beanClass = beanDefinition.getBeanClass();
bean = beanClass.newInstance();
//3. 依赖注入
for (Field declaredField : beanClass.getDeclaredFields()){
//当前属性有注解
if(declaredField.getAnnotation(Autowired.class) != null){
String name = declaredField.getName();
Object dependBean = getBean(name);
declaredField.setAccessible(true);
declaredField.set(bean, dependBean);
}
}
//4.初始化
//if(bean instanceof InitializingBean){
// ((InitializingBean)bean).afterPropertiesSet();
//}
//5.放入一级缓存
singletonObjects.put(beanName, bean);
return bean;
}
//获取单例池中的bean
private Object getSingleton(String beanName) {
if(singletonObjects.containsKey(beanName)){
return singletonObjects.get(beanName);
}
return null;
}
//ioc容器加载
public void refersh() throws Exception{
//1.解析配置 支持BeanDefinition
loadBeanDefinitions();
}
/**
*
* 根据配置信息创建BeanDefinition 底层是通过解析配置类注册beandefiniton
*/
private void loadBeanDefinitions(){
// 创建A BeanDefinition
RootBeanDefinition aBeanDefinition = new RootBeanDefinition(AService.class);
//创建B BeanDefinition
RootBeanDefinition bBeanDefinition = new RootBeanDefinition(BService.class);
beanDefinitionMap.put("aService",aBeanDefinition);
beanDefinitionMap.put("bService",bBeanDefinition);
}
}
当我们手写完IOC容器的创建过程,会发现其实在一级缓存就可以解决循环依赖的问题,只需要增加一行代码。
我们可以发现程序正常执行
那为什么spring的设计人员不采取这种方式,而是要通过三级缓存来解决循环依赖的问题呢?
这是因为只通过一级缓存来解决循环依赖问题会造成线程安全问题,例如线程1先实例化A,直接放到一级缓存,这时线程2从一级缓存中获取到了实例,调用B实例的方法,由于没有进行依赖注入,我们的B实例为null,会造成空指针异常。
为了解决这个问题,我们引入了二级缓存,专门用于存储不完整的bean,使用二级缓存获取到的bean作为出口,并且将临界资源锁住(这里借用了单例模式的思想),果然解决了线程安全的问题。
package cn.edu.hunau;
import cn.edu.hunau.service.AService;
import cn.edu.hunau.service.BService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @Author SuJ
* @Date 2024 04 12 15 13.
* 手写spring IOC底层源码来模拟spring如何利用多级缓存解决循环依赖的问题。
**/
public class SuJApplicationContext {
private Map<String, BeanDefinition> beanDefinitionMap = new LinkedHashMap<>();
// 一级缓存 单例池
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>();
// 二级缓存 ----> 并发获取不完整bean------dcl
private final Map<String, Object> earlySingletonObjects = new ConcurrentHashMap<>();
public SuJApplicationContext() throws Exception {
// 加载ioc容器 创建所有的bean
refersh();
finishBeanFactoryInitialization();
}
//一个个的创建bean
private void finishBeanFactoryInitialization() {
//循环所有的beanDefinition
beanDefinitionMap.keySet().forEach(
beanName -> {
try {
getBean(beanName);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
);
}
private Object getBean(String beanName) throws InstantiationException, IllegalAccessException {
// 1.尝试在一级缓存中获取
Object bean = getSingleton(beanName);
//如果存在 直接放回
if (bean != null) {
return bean;
}
synchronized (singletonObjects) {
bean = getSingleton(beanName);
//如果存在 直接返回
if (bean != null) {
return bean;
}
// 2.创建 ---> 实例化
RootBeanDefinition beanDefinition = (RootBeanDefinition) beanDefinitionMap.get(beanName);
Class<?> beanClass = beanDefinition.getBeanClass();
bean = beanClass.newInstance();
earlySingletonObjects.put(beanName, bean);
//3. 依赖注入
for (Field declaredField : beanClass.getDeclaredFields()) {
//当前属性有注解
if (declaredField.getAnnotation(Autowired.class) != null) {
String name = declaredField.getName();
Object dependBean = getBean(name);
declaredField.setAccessible(true);
declaredField.set(bean, dependBean);
}
}
//4.初始化
//if(bean instanceof InitializingBean){
// ((InitializingBean)bean).afterPropertiesSet();
//}
//5.放入一级缓存
singletonObjects.put(beanName, bean);
earlySingletonObjects.remove(beanName); //二级缓存是临时的需要清楚
return bean;
}
}
//获取单例池中的bean
private Object getSingleton(String beanName) {
if (singletonObjects.containsKey(beanName)) {
return singletonObjects.get(beanName);
}
//出口
synchronized (singletonObjects) {
if (earlySingletonObjects.containsKey(beanName)) {
return earlySingletonObjects.get(beanName);
}
}
return null;
}
//ioc容器加载
public void refersh() throws Exception {
//1.解析配置 支持BeanDefinition
loadBeanDefinitions();
}
/**
* 根据配置信息创建BeanDefinition 底层是通过解析配置类注册beandefiniton
*/
private void loadBeanDefinitions() {
// 创建A BeanDefinition
RootBeanDefinition aBeanDefinition = new RootBeanDefinition(AService.class);
//创建B BeanDefinition
RootBeanDefinition bBeanDefinition = new RootBeanDefinition(BService.class);
beanDefinitionMap.put("aService", aBeanDefinition);
beanDefinitionMap.put("bService", bBeanDefinition);
}
}
那三级缓存用来干什么的?
三级缓存主要是处理我们涉及到需要代理的Bean的情况的。一般来说,动态代理需要Bean的初始化过程中进行创建,但是在循环依赖的这种特殊情况下,程序根本无法走到初始化这一步,所以我们需要在实例化后就进行Bean的增强。假如说我们只使用二级缓存(如下图这样写的话),对于需要进行增强的Bean会造成两个问题
1.没有遵循规范(初始化再增强
2.循环依赖多次会创建多次(A和B循环依赖,A和C循环依赖
为了解决这些问题,spring的底层引入了三级缓存(存储一个Bean工厂对象,对于需要做增强的Bean返回代理类,不需要的返回原始类)
package cn.edu.hunau;
import cn.edu.hunau.service.impl.AService;
import cn.edu.hunau.service.impl.BService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @Author SuJ
* @Date 2024 04 12 15 13.
* 手写spring IOC底层源码来模拟spring如何利用多级缓存解决循环依赖的问题。
**/
public class SuJApplicationContext {
private Map<String, BeanDefinition> beanDefinitionMap = new LinkedHashMap<>();
// 一级缓存 单例池
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>();
// 二级缓存 ----> 并发获取不完整bean------dcl
private final Map<String, Object> earlySingletonObjects = new ConcurrentHashMap<>();
//三级缓存
private final Map<String, ObjectFactory> factoriesEarlySingletonObjects = new ConcurrentHashMap<>();
public SuJApplicationContext() throws Exception {
// 加载ioc容器 创建所有的bean
refersh();
finishBeanFactoryInitialization();
}
//一个个的创建bean
private void finishBeanFactoryInitialization() {
//循环所有的beanDefinition
beanDefinitionMap.keySet().forEach(
beanName -> {
try {
getBean(beanName);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
);
}
public Object getBean(String beanName) throws InstantiationException, IllegalAccessException {
// 1.尝试在一级缓存中获取
Object bean = getSingleton(beanName);
//如果存在 直接放回
if (bean != null) {
return bean;
}
synchronized (singletonObjects) {
bean = getSingleton(beanName);
//如果存在 直接返回
if (bean != null) {
return bean;
}
// 2.创建 ---> 实例化
RootBeanDefinition beanDefinition = (RootBeanDefinition) beanDefinitionMap.get(beanName);
Class<?> beanClass = beanDefinition.getBeanClass();
Object beanNew = beanClass.newInstance();
//1.没有遵循规范 2.循环依赖多次会创建多次
Object beanAop = new JdkProxyBeanPostProcessor().getEarlyBeanReference(bean, beanName);
factoriesEarlySingletonObjects.put(beanName, ()->{
return new JdkProxyBeanPostProcessor().getEarlyBeanReference(beanNew,beanName );
});
// 首先将早期引用放入二级缓存
// earlySingletonObjects.put(beanName, beanNew);
//3. 依赖注入
for (Field declaredField : beanClass.getDeclaredFields()) {
//当前属性有注解
if (declaredField.getAnnotation(Autowired.class) != null) {
String name = declaredField.getName();
Object dependBean = getBean(name);
declaredField.setAccessible(true);
declaredField.set(beanNew, dependBean);
}
}
//4.初始化
//if(bean instanceof InitializingBean){
// ((InitializingBean)bean).afterPropertiesSet();
//}
//5.放入一级缓存
singletonObjects.put(beanName, beanNew);
earlySingletonObjects.remove(beanName); //二级缓存是临时的需要清除
factoriesEarlySingletonObjects.remove(beanName); //三级缓存是临时的需要清除
return beanNew;
}
}
private Object getSingleton(String beanName) {
if (singletonObjects.containsKey(beanName)) {
return singletonObjects.get(beanName);
}
//出口 -- 当前是循环依赖
synchronized (singletonObjects) {
if (earlySingletonObjects.containsKey(beanName)) {
return earlySingletonObjects.get(beanName);
}
if (factoriesEarlySingletonObjects.containsKey(beanName)) {
ObjectFactory objectFactory = factoriesEarlySingletonObjects.get(beanName);
// aop
Object object = objectFactory.getObject();
earlySingletonObjects.put(beanName, object); //解决循环依赖多次会创建多次的问题
return object;
}
}
return null;
}
//ioc容器加载
public void refersh() throws Exception {
//1.解析配置 支持BeanDefinition
loadBeanDefinitions();
}
/**
* 根据配置信息创建BeanDefinition 底层是通过解析配置类注册beandefiniton
*/
private void loadBeanDefinitions() {
// 创建A BeanDefinition
RootBeanDefinition aBeanDefinition = new RootBeanDefinition(AService.class);
//创建B BeanDefinition
RootBeanDefinition bBeanDefinition = new RootBeanDefinition(BService.class);
beanDefinitionMap.put("aService", aBeanDefinition);
beanDefinitionMap.put("bService", bBeanDefinition);
}
}
其实三级缓存的思想就是:在实例化后不是直接动态代理,而是其函数式接口放入三级缓存中,出现循环依赖时在进行调用创建代理的函数。
以上是我个人的见解,请大家多指教