public class Cyclic {
//一级缓存
public static Map singletonObjects = new ConcurrentHashMap<>(256);
//二级缓存
public static Map earlySingletonObjects = new HashMap<>(16);
//校验对象是否存在循环依赖
public boolean checkBeanCyclic(Object object) throws Exception {
String thisBeanName = object.getClass().getName();
//获取object对象所有属性
Field[] declaredFields = object.getClass().getDeclaredFields();
for (Field f1 : declaredFields) {
//获取属性类型
Class> type = f1.getType();
//属性类型如果是对象继续获取对象属性中的类型,如当前属性类型OrderUser 获取OrderUser中的属性为UserService
Field[] fields2 = type.getDeclaredFields();
//循环遍历当前对象是否包存在依赖关系
for (Field f2 : fields2) {
Class> type2 = f2.getType();
String className2 = type2.getName();
//当前对象名称和依赖属性名称相同
if(object.getClass().getName().equals(className2)){
//一级缓存查询是否存在
if(singletonObjects.containsKey(type.getName())){
System.out.println("当前"+object.getClass().getName()+ "依赖"+f1.getName()+"对象");
Object bean = singletonObjects.get(type.getName());
//判断类型并赋值(需要可以继续添加)
if(thisBeanName.equals(className2)){
String methodName = f1.getName();
methodName="set"+methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
Method Method = object.getClass().getDeclaredMethod(methodName,type);
Method.invoke(object,bean);
singletonObjects.put(thisBeanName,object);
System.out.println(thisBeanName+"完成依赖注入");
}
}else{
//二级缓存查询是否存在
if(earlySingletonObjects.containsKey(type.getName())){
System.out.println("当前"+object.getClass().getName()+ "依赖"+f1.getName()+"对象");
Object bean = earlySingletonObjects.get(type.getName());
//判断类型并赋值(需要可以继续添加)
if(thisBeanName.equals(className2)){
String methodName = f1.getName();
methodName="set"+methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
Method method = object.getClass().getDeclaredMethod(methodName,type);
method.setAccessible(true);
method.invoke(object,bean);
System.out.println(thisBeanName+"完成依赖注入");
singletonObjects.put(thisBeanName,object);
earlySingletonObjects.remove(type.getName());
}
}else {
//添加半成品进二级缓存
earlySingletonObjects.put(thisBeanName,object);
//创建依赖对象
doCreateBean(type.getName());
//回调把userService完成依赖注入
checkBeanCyclic(object);
}
}
}
}
}
return true;
}
public void doCreateBean(String className) throws Exception {
boolean b = singletonObjects.containsKey(className);
if(!b){
Class> cls = Class.forName(className);
Object bean = cls.newInstance();
checkBeanCyclic(bean);
}
}
}
实体
//文件一
public class OrderService {
private UserService userService;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
}
//文件二
public class UserService {
private OrderService orderService;
public OrderService getOrderService() {
return orderService;
}
public void setOrderService(OrderService orderService) {
this.orderService = orderService;
}
}
测试
public static void main(String[] args) throws Exception {
Cyclic cyclic = new Cyclic();
cyclic.doCreateBean("com.yujie.config.service.OrderService");
UserService user = (UserService) Cyclic.singletonObjects.get("com.yujie.config.service.UserService");
OrderService order = (OrderService) Cyclic.singletonObjects.get("com.yujie.config.service.OrderService");
System.out.println(user);
System.out.println(user.getOrderService());
System.out.println(order);
System.out.println(order.getUserService());
}
打印
当前com.yujie.config.service.UserService依赖orderService对象
com.yujie.config.service.UserService完成依赖注入
当前com.yujie.config.service.OrderService依赖userService对象
com.yujie.config.service.OrderService完成依赖注入
com.yujie.config.service.UserService@4d7e1886
com.yujie.config.service.OrderService@3cd1a2f1
com.yujie.config.service.OrderService@3cd1a2f1
com.yujie.config.service.UserService@4d7e1886
总结:循环依赖就是对象a依赖对象b想解决循环依赖只需要两个map即可完成 a对象在创建的过程中发现它依赖b对象,这个时候去一级缓存查询没有找到去二级查询,都没有找到就把这个半成品a放入到二级缓存中,接着对它的依赖对象进行创建, b对象依赖a同样去一级查询没有找到,去二级查询找到了进行赋值 *** 作把a对象赋值给b对象完成注入,接着把二级缓存中的a对象移除掉,把当前完成注入的b对象放入到一级缓存中,在回调方法继续对a对象继续注入的 *** 作去一级查询发现找到了b对象,进行赋值 *** 作完成注入到此结束
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)