Spring源码:手写SpringDI

我们是在实现了SpringIOC的基础上,进行拓展,IOC实现源码可以查看:手写SpringIOC

文章目录

  • 一、分析
  • 二、实现
    • 1、构造注入
      • 1)分析
      • 2)版本1
        • BeanReference
        • BeanDefinition
        • GenericBeanDefinition
        • DefaultBeanFactory
          • 1、改造构造函数
          • 2、改造静态工厂方法
          • 3、改造工厂方法
          • 4、改造getType
      • 3)版本2
      • 4)版本3
    • 2、属性注入
      • 1)版本1
      • 2)版本2
  • 三、最终完整版本
    • PropertyValue
    • BeanReference
    • BeanDefinition
    • GenericBeanDefinition
    • BeanDefinitionRegistry
    • Beanfactory
    • DefaultBeanFactory
    • PreBuildBeanFactory

一、分析

DI是依赖注入(Dependency Injection)的缩写,是一种通过外部注入来管理组件之间依赖关系的设计模式。

本质就是赋值,给bean对象的成员变量赋值

赋值的两种情况

  • 有参构造函数赋值
  • 属性赋值

值的类型?

  • Java中支持的类型
  • Bean依赖

二、实现

1、构造注入

1)分析

通过构造方法创建实例,需要传入对应参数

  • 一般直接代码实现,直接赋值,P p = new P(xxx)
  • 在IoC中,通过反射

需要获取构造参数,可能有多个,还要保证顺序,怎么存储?

可能的参数有数字、字符串、bean依赖…
可以通过List集合来存储,添加元素的顺序就是构造参数的顺序,但是bean依赖还没有创建对应的对象,需要维护一个自定义对象,来绑定关系

2)版本1

原来已经有无参构造方法,现在我们要添加有参构造方法

BeanReference

新增一个BeanReference类,来说明bean依赖,具体依赖哪个类型的bean

public class BeanReference {private String beanName;private Class<?> type;public BeanReference(String beanName) {this.beanName = beanName;}public BeanReference(Class<?> type) {this.type = type;}public String getBeanName() {return beanName;}public void setBeanName(String beanName) {this.beanName = beanName;}public Class<?> getType() {return type;}public void setType(Class<?> type) {this.type = type;}
}
BeanDefinition

支持构造注入

List<?> getConstructorArgumentValues();
void setConstructorArgumentValues(List<?> constructorArgumentValues);
GenericBeanDefinition

实现支持构造注入

private List<?> constructorArgumentValues;@Override
public List<?> getConstructorArgumentValues() {return constructorArgumentValues;
}
@Override
public void setConstructorArgumentValues(List<?> constructorArgumentValues) {this.constructorArgumentValues = constructorArgumentValues;
}
DefaultBeanFactory
1、改造构造函数

原来的方法,只支持无参构造函数,已经不适用,需要支持有参构造函数

// 构造方法来构造对象:new BeanClass(xxx...)
private Object createInstanceByConstructor(BeanDefinition bd) throws Exception {// 1、获取参数值Object[] args = this.getConstructorArgumentValues(bd);// 2、确定调用什么构造方法来创建实例Constructor constructor = this.determineConstructor(bd, args);return constructor.newInstance(args);
}

获取参数值

private Object[] getConstructorArgumentValues(BeanDefinition bd) throws Exception {List<?> defs = bd.getConstructorArgumentValues();if (CollectionUtils.isEmpty(defs)) {return null;}Object[] values = new Object[defs.size()];int index = 0;for (Object originalValue : defs) {values[index++] = getOneArgumentRealValue(originalValue);}return values;
}

获取真正参数值,主要是处理BeanReference,得到真正的Bean实例

private Object getOneArgumentRealValue(Object originalValue) throws Exception {Object realValue = null;if (originalValue == null) {return realValue;}if (originalValue instanceof BeanReference) {//  处理bean引用BeanReference br = (BeanReference) originalValue;if (StringUtils.isNotBlank(br.getBeanName())) {realValue = this.getBean(br.getBeanName());} else if (br.getType() != null) {realValue = this.getBean(br.getType());}} else if (originalValue instanceof Object[]) {// 处理数组中的bean引用Object[] objects = (Object[]) originalValue;for (int i = 0; i < objects.length; i++) {if (objects[i] instanceof BeanReference) {objects[i] = getOneArgumentRealValue(objects[i]);}}realValue = objects;} else if (originalValue instanceof Collection) {// 处理集合中的bean引用if (originalValue instanceof Set) {Set set = (Set) originalValue;Iterator iterator = set.iterator();Set newSet = new HashSet();while (iterator.hasNext()) {Object next = iterator.next();if (next instanceof BeanReference) {iterator.remove();// set.add(getOneArgumentRealValue(next));newSet.add(getOneArgumentRealValue(next));}}set.addAll(newSet);realValue = set;} else if (originalValue instanceof List) {List list = (List) originalValue;for (int i = 0; i < list.size(); i++) {if (list.get(i) instanceof BeanReference) {list.set(i, getOneArgumentRealValue(list.get(i)));}}realValue = list;} else {realValue = originalValue;}} /*else if (originalValue instanceof Map) {// TODO 处理Map中的bean引用} */ else {realValue = originalValue;}return realValue;
}

确定调用什么构造方法来创建实例,先精确匹配(根据参数的类型进行精确匹配查找),再参数匹配(获得所有的构造方法,遍历,先判断参数数量,再依次比对形参类型与实参类型)

private Constructor<?> determineConstructor(BeanDefinition bd, Object[] args) throws Exception {// 无参数,就调用无参构造方法即可if (args == null) {return bd.getBeanClass().getConstructor(null);}Constructor<?> constructor = null;// 有参数,先精确匹配,匹配不到,再模糊匹配// 1、精确匹配Class<?>[] parameterTypes = new Class[args.length];for (int i = 0; i < args.length; i++) {parameterTypes[i] = args[i].getClass();}try {constructor = bd.getBeanClass().getConstructor(parameterTypes);} catch (Exception e) {// 这里可能找不到会报错,不用处理}// 2、参数匹配if (constructor == null) {Constructor<?>[] constructors = bd.getBeanClass().getConstructors();outer:for (Constructor<?> constructorTemp : constructors) {Class<?>[] tempParameterTypes = constructorTemp.getParameterTypes();// 参数列表个数一样if (tempParameterTypes.length == args.length) {for (int i = 0; i < tempParameterTypes.length; i++) {// 判断tempParameterTypes是否是其父类或者相同类if (!tempParameterTypes[i].isAssignableFrom(args[i].getClass())) {// 参数类型不一样,跳到下一个构造方法continue outer;}}constructor = constructorTemp;break outer;}}}// 3、还没找到,抛出异常提示if (constructor == null) {throw new Exception(bd + "不存在对应的构造方法");}return constructor;
}

如果我们是通过静态工厂方法或者成员工厂方法的方式来处理的,其实和构造参数依赖处理差不多

2、改造静态工厂方法
// 静态工厂方法:BeanClass.factoryMethodName(xxx...)
private Object createInstanceByStaticFactoryMethod(BeanDefinition bd) throws Exception {// 1、获取参数值Object[] args = this.getConstructorArgumentValues(bd);// 2、确定调用什么方法来执行获取示例Class<?> beanClass = bd.getBeanClass();Method m = this.determineMethod(bd, beanClass, args);return m.invoke(beanClass, args);
}

确定调用什么方法来创建实例,先精确匹配,后参数匹配

private Method determineMethod(BeanDefinition beanDefinition, Class<?> beanClass, Object[] args) throws Exception {Method targetMethod = null;String factoryMethodName = beanDefinition.getFactoryMethodName();// 1、精确匹配Class<?>[] parameterTypes = new Class[args.length];for (int i = 0; i < args.length; i++) {parameterTypes[i] = args[i].getClass();}try {targetMethod = beanClass.getMethod(factoryMethodName, parameterTypes);} catch (Exception e) {// 这里可能找不到会报错,不用处理,走后续的模糊匹配即可}// 2、参数匹配if (targetMethod == null) {outer:for (Method method : beanClass.getMethods()) {if (Objects.equals(factoryMethodName, method.getName())) {continue;}Class<?>[] tempparameterTypes = method.getParameterTypes();if (parameterTypes.length == args.length) {for (int i = 0; i < parameterTypes.length; i++) {if (!tempparameterTypes[i].getClass().isAssignableFrom(args[i].getClass())) {continue outer;}targetMethod = method;break outer;}}}}// 3、还没找到,抛出异常提示if (targetMethod == null) {throw new Exception(beanDefinition + "不存在对应的方法");}return targetMethod;
}
3、改造工厂方法
// 工厂bean方式来构造对象:new FactoryBeanName().factoryMethodName(xxx...)
private Object createInstanceByFactoryBean(BeanDefinition bd) throws Exception {// 1、获取工厂beanObject factoryBean = this.doGetBean(bd.getFactoryBeanName());Class<?> factoryBeanClass = factoryBean.getClass();// 2、获取参数值Object[] args = this.getConstructorArgumentValues(bd);// 3、确定调用什么方法来执行获取示例Method m = this.determineMethod(bd, factoryBeanClass, args);return m.invoke(factoryBean, args);
}
4、改造getType

主要改造静态工厂方法和工厂方法,支持有参构造函数

@Override
public Class<?> getType(String name) throws Exception {BeanDefinition beanDefinition = this.getBeanDefinition(name);Class<?> beanClass = beanDefinition.getBeanClass();List<?> constructorArgumentValues = beanDefinition.getConstructorArgumentValues();if (beanClass != null) {// 如果是静态工厂方法,需要获取工厂创建的对象if (StringUtils.isNotBlank(beanDefinition.getFactoryMethodName())) {return beanClass.getDeclaredMethod(beanDefinition.getFactoryMethodName(), this.listConverClassArr(constructorArgumentValues)).getReturnType();} else {return beanClass;}} else {// 其它情况是工厂方法的处理Method factoryMethod = beanDefinition.getFactoryMethod();if (factoryMethod != null) {return factoryMethod.getReturnType();} else {beanClass = getType(beanDefinition.getFactoryBeanName());return beanClass.getDeclaredMethod(beanDefinition.getFactoryMethodName(), this.listConverClassArr(constructorArgumentValues)).getReturnType();}}
}private Class<?>[] listConverClassArr(List<?> constructorArgumentValues) throws Exception {Class<?>[] res = new Class[constructorArgumentValues.size()];Class<?> type = null;if (constructorArgumentValues.isEmpty()) {return res;}for (int i = 0; i < constructorArgumentValues.size(); i++) {Object arg = constructorArgumentValues.get(i);if (arg instanceof cn.forlan.aop.beans.BeanReference) {cn.forlan.aop.beans.BeanReference beanReference = (cn.forlan.aop.beans.BeanReference) arg;if (StringUtils.isNotBlank(beanReference.getBeanName())) {type = this.getType(beanReference.getBeanName());} else {type = beanReference.getType();}} else {type = arg.getClass();}res[i] = type;}return res;
}

3)版本2

对于原型bean,可以缓存之前已经找到的构造方法和工厂方法

BeanDefinition

/*** 加缓存*/
Constructor<?> getConstructor();
void setConstructor(Constructor<?> constructor);
Method getFactoryMethod();
void setFactoryMethod(Method factoryMethod);

GenericBeanDefinition

private Constructor<?> constructor;
private Method factoryMethod;@Override
public Constructor<?> getConstructor() {return constructor;
}
@Override
public void setConstructor(Constructor<?> constructor) {this.constructor = constructor;
}
@Override
public Method getFactoryMethod() {return factoryMethod;
}
@Override
public void setFactoryMethod(Method factoryMethod) {this.factoryMethod = factoryMethod;
}

调整BeanFactory实现类DefaultBeanFactory

determineConstructor增加缓存逻辑

// 对于原型bean,从第二次开始获取bean实例时,可直接获得第一次缓存的构造方法。
constructor = bd.getConstructor();
if (constructor != null) {return constructor;
}if (bd.isPrototype()) {// 对于原型bean,可以缓存找到的构造方法,方便下次构造实例对象bd.setConstructor(constructor);
}

determineMethod增加缓存逻辑

// 对于原型bean,从第二次开始获取方法时,可直接获得第一次缓存的方法
targetMethod = beanDefinition.getFactoryMethod();
if (targetMethod != null) {return targetMethod;
}// 对于原型bean,可以缓存找到的方法,方便下次构造实例对象
if (beanDefinition.isPrototype()) {beanDefinition.setFactoryMethod(targetMethod);
}

4)版本3

主要解决循坏依赖的问题,对于构造注入,解决不了,但可以给异常提示,Spring中也是抛异常
存储创建对象,必须是ThreadLocal,避免多个线程的数据混乱,要线程隔离

主要改造doGetBean方法,创建前,记录正在创建的,创建完成,移除

// 存储创建中的对象
private ThreadLocal<Set<String>> buildingBeansRecordor = new ThreadLocal<>();

创建前,记录正在创建的

// 检测构造参数循环依赖
Set<String> buildingBeans = this.buildingBeansRecordor.get();
if (buildingBeans == null) {buildingBeans = new HashSet<>();this.buildingBeansRecordor.set(buildingBeans);
}
if (buildingBeans.contains(beanName)) {throw new Exception(beanName + " 循环依赖!" + buildingBeans);
}
// 记录正在创建的Bean
buildingBeans.add(beanName);

创建完成,移除

// 创建好实例后,移除创建中记录
buildingBeans.remove(beanName);

2、属性注入

1)版本1

创建好对象后,对属性进行赋值

使用实体类PropertyValue来记录

public class PropertyValue {private String name;private Object value;public PropertyValue(String name, Object value) {super();this.name = name;this.value = value;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Object getValue() {return value;}public void setValue(Object value) {this.value = value;}}

BeanDefinition实现

List<PropertyValue> getPropertyValues();
void setPropertyValues(List<PropertyValue> propertyValues);

GenericBeanDefinition,实现类声明set和get方法

private List<PropertyValue> propertyValues;@Override
public List<PropertyValue> getPropertyValues() {return propertyValues;
}@Override
public void setPropertyValues(List<PropertyValue> propertyValues) {this.propertyValues = propertyValues;
}

调整DefaultBeanFactory实现,让doCreateInstance支持属性依赖,新增setPropertyDIValues方法

private Object doCreateInstance(BeanDefinition bd) throws Exception {Class<?> beanClass = bd.getBeanClass();Object instance = null;if (beanClass != null) {if (StringUtils.isBlank(bd.getFactoryMethodName())) {// 构造方法来构造对象instance = this.createInstanceByConstructor(bd);} else {// 静态工厂方法instance = this.createInstanceByStaticFactoryMethod(bd);}} else {// 工厂bean方式来构造对象instance = this.createInstanceByFactoryBean(bd);}// 支持属性依赖this.setPropertyDIValues(bd, instance);// 执行初始化方法this.doInit(bd, instance);return instance;
}private void setPropertyDIValues(BeanDefinition bd, Object instance) throws Exception {if (CollectionUtils.isEmpty(bd.getPropertyValues())) {return;}for (PropertyValue pv : bd.getPropertyValues()) {if (StringUtils.isBlank(pv.getName())) {continue;}Class<?> clazz = instance.getClass();Field p = clazz.getDeclaredField(pv.getName());// 允许访问所有,包括privatep.setAccessible(true);// 可能是引用对象,需要获取真正的值p.set(instance, this.getOneArgumentRealValue(pv.getValue()));}
}

2)版本2

属性依赖中也存在循环依赖问题,我们这个版本就是来解决循坏依赖的问题
在这里插入图片描述

解决:

  • 在非IoC场景很好解决
ForlanA a = new ForlanA();
ForlanB b = new ForlanB();
a.setForlanB(b);
b.setForlanA(a);
  • 在IoC中,可以通过提前暴露对象来解决

因为实例化和属性赋值分开了,可以通过提前暴露解决,实际上在属性注入时,已经有对象,只不过没有暴露出来,提前暴露出来即可

使用结构

private ThreadLocal<Map<String,Object>> earlyExposeBuildingBeans = new ThreadLocal<>();

主要改造的是doGetBean,新增getFromEarlyExposeBuildingBeans,拿到就直接返回,doCreateInstance新增传参beanName

private Object doGetBean(String beanName) throws Exception {Objects.requireNonNull(beanName, "beanName不能为空");Object instance = singletonBeanMap.get(beanName);if (instance != null) {return instance;}instance = this.getFromEarlyExposeBuildingBeans(beanName);if (instance != null) { //这是属性依赖时的循环引用,返回提前暴露的实例return instance;}BeanDefinition beanDefinition = this.getBeanDefinition(beanName);Objects.requireNonNull(beanDefinition, "beanDefinition不能为空");// 检测构造参数循环依赖Set<String> buildingBeans = this.buildingBeansRecordor.get();if (buildingBeans == null) {buildingBeans = new HashSet<>();this.buildingBeansRecordor.set(buildingBeans);}if (buildingBeans.contains(beanName)) {throw new Exception(beanName + " 循环依赖!" + buildingBeans);}// 记录正在创建的BeanbuildingBeans.add(beanName);if (beanDefinition.isSingleton()) {synchronized (singletonBeanMap) {instance = singletonBeanMap.get(beanName);if (instance == null) {instance = doCreateInstance(beanName, beanDefinition);singletonBeanMap.put(beanName, instance);}}} else {instance = doCreateInstance(beanName, beanDefinition);}// 创建好实例后,移除创建中记录buildingBeans.remove(beanName);return instance;
}private Object getFromEarlyExposeBuildingBeans(String beanName) {Map<String, Object> earlyExposeBuildingBeansMap = earlyExposeBuildingBeans.get();return earlyExposeBuildingBeansMap == null ? null : earlyExposeBuildingBeansMap.get(beanName);
}

改造doCreateInstance,支持beanName传参,在属性赋值前后加逻辑,提前暴露,赋值后,移除暴露对象

private void doEarlyExposeBuildingBeans(String beanName, Object instance) {Map<String,Object> earlyExposeBuildingBeansMap = earlyExposeBuildingBeans.get();if(earlyExposeBuildingBeansMap == null) {earlyExposeBuildingBeansMap = new HashMap<>();earlyExposeBuildingBeans.set(earlyExposeBuildingBeansMap);}earlyExposeBuildingBeansMap.put(beanName,instance);
}
private Object doCreateInstance(String beanName, BeanDefinition bd) throws Exception {Class<?> beanClass = bd.getBeanClass();Object instance = null;if (beanClass != null) {if (StringUtils.isBlank(bd.getFactoryMethodName())) {// 构造方法来构造对象instance = this.createInstanceByConstructor(bd);} else {// 静态工厂方法instance = this.createInstanceByStaticFactoryMethod(bd);}} else {// 工厂bean方式来构造对象instance = this.createInstanceByFactoryBean(bd);}// 赋值前暴露半成品对象this.doEarlyExposeBuildingBeans(beanName, instance);// 支持属性依赖this.setPropertyDIValues(bd, instance);// 赋完值移除this.removeEarlyExposeBuildingBeans(beanName);// 执行初始化方法this.doInit(bd, instance);return instance;
}private void doEarlyExposeBuildingBeans(String beanName, Object instance) {Map<String,Object> earlyExposeBuildingBeansMap = earlyExposeBuildingBeans.get();if (earlyExposeBuildingBeansMap == null) {earlyExposeBuildingBeansMap = new HashMap<>();earlyExposeBuildingBeans.set(earlyExposeBuildingBeansMap);}earlyExposeBuildingBeansMap.put(beanName, instance);
}private void removeEarlyExposeBuildingBeans(String beanName) {earlyExposeBuildingBeans.get().remove(beanName);
}

三、最终完整版本

在这里插入图片描述

PropertyValue

public class PropertyValue {private String name;private Object value;public PropertyValue(String name, Object value) {super();this.name = name;this.value = value;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Object getValue() {return value;}public void setValue(Object value) {this.value = value;}}

BeanReference

/*** 用于依赖注入中描述bean依赖*/
public class BeanReference {private String beanName;private Class<?> type;public BeanReference(String beanName) {this.beanName = beanName;}public BeanReference(Class<?> type) {this.type = type;}public String getBeanName() {return beanName;}public void setBeanName(String beanName) {this.beanName = beanName;}public Class<?> getType() {return type;}public void setType(Class<?> type) {this.type = type;}
}

BeanDefinition

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.List;/*** bean定义接口*/
public interface BeanDefinition {String SCOPE_SINGLETON = "singleton";String SCOPE_PROTOTYPE = "prototype";/*** 类*/Class<?> getBeanClass();void setBeanClass(Class<?> beanClass);/*** Scope*/void setScope(String scope);String getScope();/*** 是否单例*/boolean isSingleton();/*** 是否原型*/boolean isPrototype();/*** 工厂bean名*/String getFactoryBeanName();void setFactoryBeanName(String factoryBeanName);/*** 工厂方法名*/String getFactoryMethodName();void setFactoryMethodName(String factoryMethodName);/*** 初始化方法*/String getInitMethodName();void setInitMethodName(String initMethodName);/*** 销毁方法*/String getDestroyMethodName();void setDestroyMethodName(String destroyMethodName);/*** 是否为主要自动候选对象*/boolean isPrimary();void setPrimary(boolean primary);/*** 校验bean定义的合法性,BeanClass和FactoryMethodName只能存在一个,定义了FactoryMethodName,必须指定FactoryMethodName*/default boolean validate() {// 没定义类if (this.getBeanClass() == null) {// 工厂bean或工厂方法都没定义,则不合法if (StringUtils.isBlank(getFactoryBeanName()) || StringUtils.isBlank(getFactoryMethodName())) {return false;}}// 定义了类,又定义工厂bean,不合法if (this.getBeanClass() != null && StringUtils.isNotBlank(getFactoryBeanName())) {return false;}return true;}/*** 支持有参构造注入*/List<?> getConstructorArgumentValues();void setConstructorArgumentValues(List<?> constructorArgumentValues);/*** 加缓存*/Constructor<?> getConstructor();void setConstructor(Constructor<?> constructor);Method getFactoryMethod();void setFactoryMethod(Method factoryMethod);/*** 支持属性依赖*/List<PropertyValue> getPropertyValues();void setPropertyValues(List<PropertyValue> propertyValues);}

GenericBeanDefinition

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.List;public class GenericBeanDefinition implements BeanDefinition {public static final String SCOPE_DEFAULT = "";private Class<?> beanClass;private String scope = SCOPE_DEFAULT;private String factoryBeanName;private String factoryMethodName;private String initMethodName;private String destroyMethodName;private boolean primary;private List<?> constructorArgumentValues;private Constructor<?> constructor;private Method factoryMethod;private List<PropertyValue> propertyValues;@Overridepublic Class<?> getBeanClass() {return beanClass;}@Overridepublic void setBeanClass(Class<?> beanClass) {this.beanClass = beanClass;}@Overridepublic String getScope() {return scope;}@Overridepublic void setScope(String scope) {this.scope = scope;}/*** 默认是单例*/@Overridepublic boolean isSingleton() {return SCOPE_SINGLETON.equals(this.scope) || SCOPE_DEFAULT.equals(this.scope);}@Overridepublic boolean isPrototype() {return SCOPE_PROTOTYPE.equals(this.scope);}@Overridepublic String getFactoryBeanName() {return factoryBeanName;}@Overridepublic void setFactoryBeanName(String factoryBeanName) {this.factoryBeanName = factoryBeanName;}@Overridepublic String getFactoryMethodName() {return factoryMethodName;}@Overridepublic void setFactoryMethodName(String factoryMethodName) {this.factoryMethodName = factoryMethodName;}@Overridepublic String getInitMethodName() {return initMethodName;}@Overridepublic void setInitMethodName(String initMethodName) {this.initMethodName = initMethodName;}@Overridepublic String getDestroyMethodName() {return destroyMethodName;}@Overridepublic void setDestroyMethodName(String destroyMethodName) {this.destroyMethodName = destroyMethodName;}@Overridepublic boolean isPrimary() {return primary;}@Overridepublic void setPrimary(boolean primary) {this.primary = primary;}@Overridepublic List<?> getConstructorArgumentValues() {return constructorArgumentValues;}@Overridepublic void setConstructorArgumentValues(List<?> constructorArgumentValues) {this.constructorArgumentValues = constructorArgumentValues;}@Overridepublic Constructor<?> getConstructor() {return constructor;}@Overridepublic void setConstructor(Constructor<?> constructor) {this.constructor = constructor;}@Overridepublic Method getFactoryMethod() {return factoryMethod;}@Overridepublic void setFactoryMethod(Method factoryMethod) {this.factoryMethod = factoryMethod;}@Overridepublic List<PropertyValue> getPropertyValues() {return propertyValues;}@Overridepublic void setPropertyValues(List<PropertyValue> propertyValues) {this.propertyValues = propertyValues;}@Overridepublic String toString() {return "GenericBeanDefinition{" +"beanClass=" + beanClass +", factoryBeanName='" + factoryBeanName + '\'' +'}';}
}

BeanDefinitionRegistry

public interface BeanDefinitionRegistry {void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) throws Exception;BeanDefinition getBeanDefinition(String beanName);boolean containsBeanDefinition(String beanName);
}

Beanfactory

import java.util.Map;public interface Beanfactory {Object getBean(String name) throws Exception;Class<?> getType(String name) throws Exception;<T> T getBean(Class<T> requiredType) throws Exception;<T> Map<String,T> getBeansOfType(Class<T> type)throws Exception;
}

DefaultBeanFactory

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import java.io.Closeable;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;@Slf4j
public class DefaultBeanFactory implements BeanDefinitionRegistry, Beanfactory, Closeable {// 存储结构:存beanName和beanDefinition的映射关系protected Map<String, BeanDefinition> beanDefintionMap = new ConcurrentHashMap<>(256);private Map<String, Object> singletonBeanMap = new ConcurrentHashMap<>(256);private Map<Class<?>, Set<String>> typeNameMap = new ConcurrentHashMap<>(256);// 存储创建中的对象private ThreadLocal<Set<String>> buildingBeansRecordor = new ThreadLocal<>();// 提前暴露对象private ThreadLocal<Map<String, Object>> earlyExposeBuildingBeans = new ThreadLocal<>();@Overridepublic void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) throws Exception {Objects.requireNonNull(beanName, "注册bean需要提供beanName");Objects.requireNonNull(beanDefinition, "注册bean需要提供beanDefinition");// 校验给入的bean是否合法if (!beanDefinition.validate()) {throw new RuntimeException("名字为[" + beanName + "] 的bean定义不合法:" + beanDefinition);}// Spring中默认是不可覆盖(抛异常),可通过参数 spring.main.allow-bean-definition-overriding: true 来允许覆盖if (this.containsBeanDefinition(beanName)) {throw new RuntimeException("名字为[" + beanName + "] 的bean定义已存在:" + this.getBeanDefinition(beanName));}beanDefintionMap.put(beanName, beanDefinition);this.registerTypeNameMap();}public void registerTypeNameMap() throws Exception {for (String name : beanDefintionMap.keySet()) {Class<?> type = this.getType(name);// 注册本类this.registerTypeNameMap(type, name);// 注册父类:Spring不支持,我们这里也模拟注释掉,不开放// this.registerSuperClassTypeNaemMap(type, name);// 注册实现的接口this.registerInterfaceTypeNaemMap(type, name);}}private void registerTypeNameMap(Class<?> type, String name) {Set<String> beanNames = typeNameMap.get(type);if (beanNames == null) {beanNames = new HashSet<>();typeNameMap.put(type, beanNames);}beanNames.add(name);}private void registerSuperClassTypeNaemMap(Class<?> type, String name) {Class<?> superclass = type.getSuperclass();if (superclass != null && !superclass.equals(Object.class)) {// 注册本类this.registerTypeNameMap(superclass, name);// 注册父类this.registerSuperClassTypeNaemMap(superclass, name);// 注册实现的接口this.registerInterfaceTypeNaemMap(superclass, name);}}private void registerInterfaceTypeNaemMap(Class<?> type, String name) {Class<?>[] interfaces = type.getInterfaces();for (Class<?> anInterface : interfaces) {this.registerTypeNameMap(anInterface, name);this.registerInterfaceTypeNaemMap(anInterface, name);}}@Overridepublic BeanDefinition getBeanDefinition(String beanName) {return beanDefintionMap.get(beanName);}@Overridepublic boolean containsBeanDefinition(String beanName) {return beanDefintionMap.containsKey(beanName);}@Overridepublic Object getBean(String name) throws Exception {return this.doGetBean(name);}@Overridepublic Class<?> getType(String name) throws Exception {BeanDefinition beanDefinition = this.getBeanDefinition(name);Class<?> beanClass = beanDefinition.getBeanClass();List<?> constructorArgumentValues = beanDefinition.getConstructorArgumentValues();if (beanClass != null) {// 如果是静态工厂方法,需要获取工厂创建的对象if (StringUtils.isNotBlank(beanDefinition.getFactoryMethodName())) {return beanClass.getDeclaredMethod(beanDefinition.getFactoryMethodName(), this.listConverClassArr(constructorArgumentValues)).getReturnType();} else {return beanClass;}} else {// 其它情况是工厂方法的处理Method factoryMethod = beanDefinition.getFactoryMethod();if (factoryMethod != null) {return factoryMethod.getReturnType();} else {beanClass = getType(beanDefinition.getFactoryBeanName());return beanClass.getDeclaredMethod(beanDefinition.getFactoryMethodName(), this.listConverClassArr(constructorArgumentValues)).getReturnType();}}}private Class<?>[] listConverClassArr(List<?> constructorArgumentValues) throws Exception {Class<?>[] res = new Class[constructorArgumentValues.size()];Class<?> type = null;if (constructorArgumentValues.isEmpty()) {return res;}for (int i = 0; i < constructorArgumentValues.size(); i++) {Object arg = constructorArgumentValues.get(i);if (arg instanceof cn.forlan.aop.beans.BeanReference) {cn.forlan.aop.beans.BeanReference beanReference = (cn.forlan.aop.beans.BeanReference) arg;if (StringUtils.isNotBlank(beanReference.getBeanName())) {type = this.getType(beanReference.getBeanName());} else {type = beanReference.getType();}} else {type = arg.getClass();}res[i] = type;}return res;}@Overridepublic <T> T getBean(Class<T> requiredType) throws Exception {Set<String> beanNames = typeNameMap.get(requiredType);if (null == beanNames) {return null;}if (beanNames.size() == 1) {String beanName = beanNames.iterator().next();return (T) this.getBean(beanName);} else {String primaryBeanName = null;for (String beanName : beanNames) {BeanDefinition beanDefinition = this.getBeanDefinition(beanName);if (beanDefinition != null && beanDefinition.isPrimary()) {if (primaryBeanName == null) {primaryBeanName = beanName;} else {throw new RuntimeException(requiredType + "类存在多个Primary,无法确定唯一一个Bean");}}}if (primaryBeanName != null) {return (T) this.getBean(primaryBeanName);} else {throw new RuntimeException(requiredType + "类未找到对应的Bean");}}}@Overridepublic <T> Map<String, T> getBeansOfType(Class<T> type) throws Exception {Set<String> beanNames = typeNameMap.get(type);if (null == beanNames) {return null;}Map<String, T> nameBeanMap = new HashMap<String, T>();for (String beanName : beanNames) {nameBeanMap.put(beanName, (T) this.getBean(beanName));}return nameBeanMap;}private Object doGetBean(String beanName) throws Exception {Objects.requireNonNull(beanName, "beanName不能为空");Object instance = singletonBeanMap.get(beanName);if (instance != null) {return instance;}instance = this.getFromEarlyExposeBuildingBeans(beanName);if (instance != null) { //这是属性依赖时的循环引用,返回提前暴露的实例return instance;}BeanDefinition beanDefinition = this.getBeanDefinition(beanName);Objects.requireNonNull(beanDefinition, "beanDefinition不能为空");// 检测构造参数循环依赖Set<String> buildingBeans = this.buildingBeansRecordor.get();if (buildingBeans == null) {buildingBeans = new HashSet<>();this.buildingBeansRecordor.set(buildingBeans);}if (buildingBeans.contains(beanName)) {throw new Exception(beanName + " 循环依赖!" + buildingBeans);}// 记录正在创建的BeanbuildingBeans.add(beanName);if (beanDefinition.isSingleton()) {synchronized (singletonBeanMap) {instance = singletonBeanMap.get(beanName);if (instance == null) {instance = doCreateInstance(beanName, beanDefinition);singletonBeanMap.put(beanName, instance);}}} else {instance = doCreateInstance(beanName, beanDefinition);}// 创建好实例后,移除创建中记录buildingBeans.remove(beanName);return instance;}private Object getFromEarlyExposeBuildingBeans(String beanName) {Map<String, Object> earlyExposeBuildingBeansMap = earlyExposeBuildingBeans.get();return earlyExposeBuildingBeansMap == null ? null : earlyExposeBuildingBeansMap.get(beanName);}private Object doCreateInstance(String beanName, BeanDefinition bd) throws Exception {Class<?> beanClass = bd.getBeanClass();Object instance = null;if (beanClass != null) {if (StringUtils.isBlank(bd.getFactoryMethodName())) {// 构造方法来构造对象instance = this.createInstanceByConstructor(bd);} else {// 静态工厂方法instance = this.createInstanceByStaticFactoryMethod(bd);}} else {// 工厂bean方式来构造对象instance = this.createInstanceByFactoryBean(bd);}// 赋值前暴露半成品对象this.doEarlyExposeBuildingBeans(beanName, instance);// 支持属性依赖this.setPropertyDIValues(bd, instance);// 赋完值移除this.removeEarlyExposeBuildingBeans(beanName);// 执行初始化方法this.doInit(bd, instance);return instance;}private void doEarlyExposeBuildingBeans(String beanName, Object instance) {Map<String,Object> earlyExposeBuildingBeansMap = earlyExposeBuildingBeans.get();if (earlyExposeBuildingBeansMap == null) {earlyExposeBuildingBeansMap = new HashMap<>();earlyExposeBuildingBeans.set(earlyExposeBuildingBeansMap);}earlyExposeBuildingBeansMap.put(beanName, instance);}private void setPropertyDIValues(BeanDefinition bd, Object instance) throws Exception {if (CollectionUtils.isEmpty(bd.getPropertyValues())) {return;}for (PropertyValue pv : bd.getPropertyValues()) {if (StringUtils.isBlank(pv.getName())) {continue;}Class<?> clazz = instance.getClass();Field p = clazz.getDeclaredField(pv.getName());// 允许访问所有,包括privatep.setAccessible(true);// 可能是引用对象,需要获取真正的值p.set(instance, this.getOneArgumentRealValue(pv.getValue()));}}private void removeEarlyExposeBuildingBeans(String beanName) {earlyExposeBuildingBeans.get().remove(beanName);}// 构造方法来构造对象:new BeanClass(xxx...)private Object createInstanceByConstructor(BeanDefinition bd) throws Exception {// 1、获取参数值Object[] args = this.getConstructorArgumentValues(bd);// 2、确定调用什么构造方法来创建实例Constructor constructor = this.determineConstructor(bd, args);return constructor.newInstance(args);}private Object[] getConstructorArgumentValues(BeanDefinition bd) throws Exception {List<?> defs = bd.getConstructorArgumentValues();if (CollectionUtils.isEmpty(defs)) {return null;}Object[] values = new Object[defs.size()];int index = 0;for (Object originalValue : defs) {values[index++] = getOneArgumentRealValue(originalValue);}return values;}private Object getOneArgumentRealValue(Object originalValue) throws Exception {//获取真正参数值,主要是处理BeanReference,得到真正的Bean实例Object realValue = null;if (originalValue == null) {return realValue;}if (originalValue instanceof BeanReference) {//  处理bean引用BeanReference br = (BeanReference) originalValue;if (StringUtils.isNotBlank(br.getBeanName())) {realValue = this.getBean(br.getBeanName());} else if (br.getType() != null) {realValue = this.getBean(br.getType());}} else if (originalValue instanceof Object[]) {// 处理数组中的bean引用Object[] objects = (Object[]) originalValue;for (int i = 0; i < objects.length; i++) {if (objects[i] instanceof BeanReference) {objects[i] = getOneArgumentRealValue(objects[i]);}}realValue = objects;} else if (originalValue instanceof Collection) {// 处理集合中的bean引用if (originalValue instanceof Set) {Set set = (Set) originalValue;Iterator iterator = set.iterator();Set newSet = new HashSet();while (iterator.hasNext()) {Object next = iterator.next();if (next instanceof BeanReference) {iterator.remove();// set.add(getOneArgumentRealValue(next));newSet.add(getOneArgumentRealValue(next));}}set.addAll(newSet);realValue = set;} else if (originalValue instanceof List) {List list = (List) originalValue;for (int i = 0; i < list.size(); i++) {if (list.get(i) instanceof BeanReference) {list.set(i, getOneArgumentRealValue(list.get(i)));}}realValue = list;} else {realValue = originalValue;}} /*else if (originalValue instanceof Map) {// TODO 处理Map中的bean引用} */ else {realValue = originalValue;}return realValue;}private Constructor<?> determineConstructor(BeanDefinition bd, Object[] args) throws Exception {// 无参数,就调用无参构造方法即可if (args == null) {return bd.getBeanClass().getConstructor(null);}Constructor<?> constructor = null;// 对于原型bean,从第二次开始获取bean实例时,可直接获得第一次缓存的构造方法。constructor = bd.getConstructor();if (constructor != null) {return constructor;}// 有参数,先精确匹配,匹配不到,再模糊匹配// 1、精确匹配Class<?>[] parameterTypes = new Class[args.length];for (int i = 0; i < args.length; i++) {parameterTypes[i] = args[i].getClass();}try {constructor = bd.getBeanClass().getConstructor(parameterTypes);} catch (Exception e) {// 这里可能找不到会报错,不用处理}// 2、模糊匹配if (constructor == null) {Constructor<?>[] constructors = bd.getBeanClass().getConstructors();outer:for (Constructor<?> constructorTemp : constructors) {Class<?>[] tempParameterTypes = constructorTemp.getParameterTypes();// 参数列表个数一样if (tempParameterTypes.length == args.length) {for (int i = 0; i < tempParameterTypes.length; i++) {// 判断tempParameterTypes是否是其父类或者相同类if (!tempParameterTypes[i].isAssignableFrom(args[i].getClass())) {// 参数类型不一样,跳到下一个构造方法continue outer;}}constructor = constructorTemp;break outer;}}}// 3、还没找到,抛出异常提示if (constructor == null) {throw new Exception(bd + "不存在对应的构造方法");}if (bd.isPrototype()) {// 对于原型bean,可以缓存找到的构造方法,方便下次构造实例对象bd.setConstructor(constructor);}return constructor;}// 静态工厂方法:BeanClass.factoryMethodName(xxx...)private Object createInstanceByStaticFactoryMethod(BeanDefinition bd) throws Exception {// 1、获取参数值Object[] args = this.getConstructorArgumentValues(bd);// 2、确定调用什么方法来执行获取示例Class<?> beanClass = bd.getBeanClass();Method m = this.determineMethod(bd, beanClass, args);return m.invoke(beanClass, args);}private Method determineMethod(BeanDefinition beanDefinition, Class<?> beanClass, Object[] args) throws Exception {Method targetMethod = null;// 对于原型bean,从第二次开始获取方法时,可直接获得第一次缓存的方法targetMethod = beanDefinition.getFactoryMethod();if (targetMethod != null) {return targetMethod;}// 1、精确匹配Class<?>[] parameterTypes = new Class[args.length];for (int i = 0; i < args.length; i++) {parameterTypes[i] = args[i].getClass();}String factoryMethodName = beanDefinition.getFactoryMethodName();try {targetMethod = beanClass.getMethod(factoryMethodName, parameterTypes);} catch (Exception e) {// 这里可能找不到会报错,不用处理,走后续的模糊匹配即可}// 2、模糊匹配if (targetMethod == null) {outer:for (Method method : beanClass.getMethods()) {if (Objects.equals(factoryMethodName, method.getName())) {continue;}Class<?>[] tempparameterTypes = method.getParameterTypes();if (parameterTypes.length == args.length) {for (int i = 0; i < parameterTypes.length; i++) {if (!tempparameterTypes[i].getClass().isAssignableFrom(args[i].getClass())) {continue outer;}targetMethod = method;break outer;}}}}// 3、还没找到,抛出异常提示if (targetMethod == null) {throw new Exception(beanDefinition + "不存在对应的方法");}// 对于原型bean,可以缓存找到的方法,方便下次构造实例对象if (beanDefinition.isPrototype()) {beanDefinition.setFactoryMethod(targetMethod);}return targetMethod;}// 工厂bean方式来构造对象:new FactoryBeanName().factoryMethodName(xxx...)private Object createInstanceByFactoryBean(BeanDefinition bd) throws Exception {// 1、获取工厂beanObject factoryBean = this.doGetBean(bd.getFactoryBeanName());Class<?> factoryBeanClass = factoryBean.getClass();// 2、获取参数值Object[] args = this.getConstructorArgumentValues(bd);// 3、确定调用什么方法来执行获取示例Method m = this.determineMethod(bd, factoryBeanClass, args);return m.invoke(factoryBean, args);}private void doInit(BeanDefinition bd, Object instance) throws Exception {// 执行初始化方法if (StringUtils.isNotBlank(bd.getInitMethodName())) {Method m = instance.getClass().getMethod(bd.getInitMethodName(), null);m.invoke(instance, null);}}@Overridepublic void close() {for (Map.Entry<String, BeanDefinition> beanDefinitionEntry : beanDefintionMap.entrySet()) {String beanName = beanDefinitionEntry.getKey();BeanDefinition beanDefinition = beanDefinitionEntry.getValue();if (beanDefinition.isSingleton() && singletonBeanMap.containsKey(beanName)) {Object instance = this.singletonBeanMap.get(beanName);try {Method m = instance.getClass().getMethod(beanDefinition.getDestroyMethodName(), null);m.invoke(instance, null);} catch (Exception e) {log.error("执行名字为[" + beanName + "] 的bean销毁方法异常", e);}}}}
}

PreBuildBeanFactory

import lombok.extern.slf4j.Slf4j;
import java.util.Map;@Slf4j
public class PreBuildBeanFactory extends DefaultBeanFactory{public void preInstantiateSingletons() throws Exception {synchronized (this.beanDefintionMap) {for (Map.Entry<String, BeanDefinition> entry : this.beanDefintionMap.entrySet()) {String name = entry.getKey();BeanDefinition beanDefinition = entry.getValue();if (beanDefinition.isSingleton()) {this.getBean(name);if (log.isDebugEnabled()) {log.debug("preInstantiate: name=" + name + " " + beanDefinition);}}}}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/719349.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ES入门六:Suggesters Api实践

都是负担在很多app上&#xff0c;当我们输入某些内容时候&#xff0c;它会立即做一些补全操作&#xff0c;如果我想实现上述的需求&#xff0c;我们就可以使用ES提供的Suggesters Api。那Suggesters是如何做到的那&#xff1f;简单来说&#xff0c;Suggesters会将输入的文本拆分…

【网站项目】167固定资产管理系统

&#x1f64a;作者简介&#xff1a;拥有多年开发工作经验&#xff0c;分享技术代码帮助学生学习&#xff0c;独立完成自己的项目或者毕业设计。 代码可以私聊博主获取。&#x1f339;赠送计算机毕业设计600个选题excel文件&#xff0c;帮助大学选题。赠送开题报告模板&#xff…

谁才是“内卷”之王?众多洗地机品牌哪家清洁力最强?清洁最干净?

在如今快节奏的生活中&#xff0c;家庭清洁工作愈发显得繁琐而耗时。添可洗地机凭借其高效的一体化清洁功能和智能化操作&#xff0c;为现代家庭生活带来了极大的便利。面对众多款品牌洗地机型号&#xff0c;消费者不禁会问&#xff1a;哪家洗地机清洁力最强&#xff1f;在性能…

解决tomcat双击startup.bat一闪而过的问题

这种问题可能是tomcat找不到你的jdk环境配置路径 1、首先在tomcat的bin文件夹找到startup.bat 和catalina.bat两个文件 2、startup.bat用记事本打开 在末尾添加pause 3、保存修改&#xff0c;双击startup.bat如果出现这种问题&#xff0c;就是找不到jdk路径 4、用记事本打开ca…

STM32CubeMX实战教程: TIM6、TIM7 - 基本定时器

目录 一、基本定时器的作用 二、常用型号的TIM时钟频率 三、CubeMX配置 四、编写执行代码 一、基本定时器的作用 基本定时器&#xff0c;主要用于实现定时和计数功能。作用包括&#xff1a; 定时功能&#xff1a;可以产生周期性的中断&#xff0c;用于实现定时任务。例如&…

什么是Docker容器?

Docker是一种轻量级的虚拟化技术&#xff0c;同时是一个开源的应用容器运行环境搭建平台&#xff0c;可以让开发者以便捷方式打包应用到一个可移植的容器中&#xff0c;然后安装至任何运行Linux或Windows等系统的服务器上。相较于传统虚拟机&#xff0c;Docker容器提供轻量化的…

linux安装mysql5.7

linux安装mysql5.7 一、下载mysql5.7二、解压包介绍三、上传包到linux四、卸载mariadb五、安装mysql六、修改权限七、启动mysql八、使用过navicat创作不易&#xff0c;笔记不易&#xff0c;如觉不错&#xff0c;请三连&#xff0c;谢谢~~ 一、下载mysql5.7 去mysql官方下载&am…

MES系统在离散制造企业中的功能解析

随着信息技术的快速发展和制造业的转型升级&#xff0c;MES在离散制造企业中的作用日益凸显。MES系统不仅提高了生产效率和产品质量&#xff0c;还优化了资源配置&#xff0c;增强了企业的市场竞争力。 一、生产管理功能 MES系统能够实时监控生产现场的各种数据&#xff0c;包…

二叉搜索树题目:将有序数组转换为二叉搜索树

文章目录 题目标题和出处难度题目描述要求示例数据范围 解法思路和算法证明代码复杂度分析 题目 标题和出处 标题&#xff1a;将有序数组转换为二叉搜索树 出处&#xff1a;108. 将有序数组转换为二叉搜索树 难度 4 级 题目描述 要求 给定整数数组 nums \texttt{nums}…

15 easy 141. 环形链表

法1&#xff1a;快慢指针法&#xff1a; //给你一个链表的头节点 head &#xff0c;判断链表中是否有环。 // // 如果链表中有某个节点&#xff0c;可以通过连续跟踪 next 指针再次到达&#xff0c;则链表中存在环。 为了表示给定链表中的环&#xff0c;评测系统内部使用整数…

Python爬虫副业真的可行吗?

首先回答你&#xff0c;是可行的&#xff0c;python爬虫能当副业&#xff0c;副业的方式比较多&#xff0c;等下我会讲几种。 那学到哪个层次可以接单呢&#xff1f;主要看你是接什么样的单&#xff0c;爬一些资料&#xff0c;视频这种简单的学一两个月就没什么问题&#xff0…

第一天 走进Docker的世界

第一天 走进Docker的世界 介绍docker的前世今生&#xff0c;了解docker的实现原理&#xff0c;以Django项目为例&#xff0c;带大家如何编写最佳的Dockerfile构建镜像。通过本章的学习&#xff0c;大家会知道docker的概念及基本操作&#xff0c;并学会构建自己的业务镜像&…

一文读懂Persistence One- 如何将Restaking带入Cosmos

Persistence One正在将Restaking引入Cosmos。用户将能够通过pSTAKE、Stride、Quicksilver和Milkyway将Liquid Staked Tokens&#xff08;如ATOM、TIA、DYDX等&#xff09;存入Persistence One&#xff0c;对其进行Restaking&#xff0c;从而安全地连接更多区块链&#xff0c;首…

MySQL:数据库中有哪些锁

1、全局锁 加上全局锁后整个数据库就处于只读状态了&#xff0c;这时其他线程执行以下操作&#xff0c;都会被阻塞&#xff1a; 对数据的增删改操作&#xff0c;比如 insert、delete、update等语句&#xff1b;对表结构的更改操作&#xff0c;比如 alter table、drop table 等…

Android APK包反编译为java文件教程

方法 流程&#xff1a; test.apk -> smali文件 -> dex文件 -> jar文件 ->java 文件 将APK包解压为 smail文件 下载 apktool工具 apktool.jar 将 test.apk 和 apktool.jar放同一目录下&#xff0c;并执行以下命令 java -jar apktool.jar d -f xxx.apk -o xxx(解…

【如何像网吧一样弄个游戏菜单在家里】

GGmenu 个人家庭版游戏、应用管理 桌面图标管理器

[环境配置]ssh连接报错“kex_exchange_identification: read: Connection reset by peer”

已经被VScode ssh毒死好几次了&#xff0c;都是执行命令意外中断&#xff0c;然后又VSCode里连不上、本机Terminal也连不上了。。。 重启远程服务器&#xff0c;VSCode可以连上了&#xff0c; 系统ssh还是不行&#xff0c;报错“kex_exchange_identification: read: Connecti…

Java学习笔记002——类的修饰符

在Java语言中&#xff0c;类的访问修饰符决定了其它类能够访问该类的方式。类有如下4种访问修饰符&#xff0c;在创建类时用于类的声明&#xff1a; 1、public: 当一个类被声明为public时&#xff0c;它可以从任何其他类中被访问&#xff0c;无论这些类位于哪个包中。通常&am…

数字化转型导师坚鹏:BLM证券公司数字化转型战略

BLM证券公司数字化转型战略 ——以BLM模型为核心&#xff0c;实现知行果合一 课程背景&#xff1a; 很多证券公司存在以下问题&#xff1a; 不知道如何系统地制定证券公司数字化转型战略&#xff1f; 不清楚其它证券公司数字化转型战略是如何制定的&#xff1f; 不知道…

Redis 淘汰策略、持久化、高可用

淘汰策略 只有 redis 内存空间已满并且往里面写新数据&#xff0c;才会触发淘汰策略。通过 expire / / /pexpire 让 key-value 过期&#xff0c;从而让 redis 清除这个 key-value。value 的数据结构typedef struct redisObject {unsigned tpye:4;unsigned encoding:4;// 判断哪…